Module: Lazier::DateTime
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/lazier/datetime.rb
Overview
Extensions for date and time objects.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary (collapse)
-
- (Fixnum) in_months(base = nil)
Returns the number of months passed between the beginning of the base year and the current date.
-
- (String) local_lstrftime(format = nil)
Formats a datetime in the current timezone, looking up also custom formats.
-
- (String) local_strftime(format = nil)
Formats a datetime in the current timezone.
-
- (String) lstrftime(format = nil)
Formats a datetime, looking up also custom formats.
-
- (String) padded_month
Returns the current month number with leading 0.
-
- (UTC::Time) utc_time
Returns the UTC::Time representation of the current datetime.
Instance Method Details
- (Fixnum) in_months(base = nil)
Returns the number of months passed between the beginning of the base year and the current date.
DateTime.civil(2013, 6, 1).in_months(2011)
# => 18
229 230 231 232 |
# File 'lib/lazier/datetime.rb', line 229 def in_months(base = nil) base ||= ::Date.today.year ((self.year) - base) * 12 + self.month end |
- (String) local_lstrftime(format = nil)
Formats a datetime in the current timezone, looking up also custom formats.
266 267 268 |
# File 'lib/lazier/datetime.rb', line 266 def local_lstrftime(format = nil) (self.respond_to?(:in_time_zone) ? self.in_time_zone : self).lstrftime(format) end |
- (String) local_strftime(format = nil)
Formats a datetime in the current timezone.
257 258 259 |
# File 'lib/lazier/datetime.rb', line 257 def local_strftime(format = nil) (self.respond_to?(:in_time_zone) ? self.in_time_zone : self).strftime(::DateTime.custom_format(format)) end |
- (String) lstrftime(format = nil)
Formats a datetime, looking up also custom formats.
246 247 248 249 250 251 |
# File 'lib/lazier/datetime.rb', line 246 def lstrftime(format = nil) rv = nil names = ::Lazier.settings.date_names substitutions = {"%a" => names[:short_days][self.wday], "%A" => names[:long_days][self.wday], "%b" => names[:short_months][self.month - 1], "%B" => names[:long_months][self.month - 1]} self.strftime(::DateTime.custom_format(format).ensure_string.gsub(/(?<!%)(%[ab])/i) {|mo| substitutions[mo] }) end |
- (String) padded_month
Returns the current month number with leading 0.
237 238 239 |
# File 'lib/lazier/datetime.rb', line 237 def padded_month self.month.to_s.rjust(2, "0") end |
- (UTC::Time) utc_time
Returns the UTC::Time representation of the current datetime.
215 216 217 218 |
# File 'lib/lazier/datetime.rb', line 215 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 |