Module: Cowtech::Extensions::DateTime
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/cowtech-extensions/datetime.rb
Overview
Extensions for date and time objects.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#in_months(base = nil) ⇒ Fixnum
Returns the number of months passed between the beginning of the base year and the current date.
-
#local_lstrftime(format = nil) ⇒ String
Formats a datetime in the current timezone, looking up also custom formats.
-
#local_strftime(format = nil) ⇒ String
Formats a datetime in the current timezone.
-
#lstrftime(format = nil) ⇒ String
Formats a datetime, looking up also custom formats.
-
#padded_month ⇒ String
Returns the current month number with leading 0.
-
#utc_time ⇒ UTC::Time
Returns the UTC::Time representation of the current datetime.
Instance Method Details
#in_months(base = nil) ⇒ Fixnum
Returns the number of months passed between the beginning of the base year and the current date.
DateTime.civil(2012, 6, 1).in_months(2011)
# => 18
194 195 196 197 |
# File 'lib/cowtech-extensions/datetime.rb', line 194 def in_months(base = nil) base ||= ::Date.today.year ((self.year) - base) * 12 + self.month end |
#local_lstrftime(format = nil) ⇒ String
Formats a datetime in the current timezone, looking up also custom formats.
255 256 257 |
# File 'lib/cowtech-extensions/datetime.rb', line 255 def local_lstrftime(format = nil) (self.respond_to?(:in_time_zone) ? self.in_time_zone : self).lstrftime(format) end |
#local_strftime(format = nil) ⇒ String
Formats a datetime in the current timezone.
246 247 248 |
# File 'lib/cowtech-extensions/datetime.rb', line 246 def local_strftime(format = nil) (self.respond_to?(:in_time_zone) ? self.in_time_zone : self).strftime(::DateTime.custom_format(format)) end |
#lstrftime(format = nil) ⇒ String
Formats a datetime, looking up also custom formats.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/cowtech-extensions/datetime.rb', line 211 def lstrftime(format = nil) rv = nil names = ::Cowtech::Extensions.settings.date_names final_format = ::DateTime.custom_format(format).ensure_string.gsub(/(%{1,2}:?[abz])/i) do |match| mrv = match # Handling of %z is to fix ruby 1.8 bug in OSX: http://bugs.ruby-lang.org/issues/2396 if match !~ /^%%/ then case match when "%a" mrv = names[:short_days][self.wday] when "%A" mrv = names[:long_days][self.wday] when "%b" mrv = names[:short_months][self.month - 1] when "%B" mrv = names[:long_months][self.month - 1] when "%z" mrv = ::Cowtech::Extensions.is_ruby_18? ? self.formatted_offset(false) : nil when "%:z" mrv = ::Cowtech::Extensions.is_ruby_18? ? self.formatted_offset(true) : nil end end mrv ? mrv.sub("%", "%%") : match end self.strftime(final_format) end |
#padded_month ⇒ String
Returns the current month number with leading 0.
202 203 204 |
# File 'lib/cowtech-extensions/datetime.rb', line 202 def padded_month self.month.to_s.rjust(2, "0") end |
#utc_time ⇒ UTC::Time
Returns the UTC::Time representation of the current datetime.
180 181 182 183 |
# File 'lib/cowtech-extensions/datetime.rb', line 180 def utc_time ua = (self.respond_to?(:utc) ? self : self.to_datetime).utc ::Time.utc(ua.year, ua.month, ua.day, ua.hour, ua.min, ua.sec) end |