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
-
#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(2013, 6, 1).in_months(2011)
# => 18
222 223 224 |
# File 'lib/lazier/datetime.rb', line 222 def in_months(base = nil) (year - (base || ::Date.today.year)) * 12 + month end |
#local_lstrftime(format = nil) ⇒ String
Formats a datetime in the current timezone, looking up also custom formats.
255 256 257 |
# File 'lib/lazier/datetime.rb', line 255 def local_lstrftime(format = nil) (respond_to?(:in_time_zone) ? in_time_zone : self).lstrftime(format) end |
#local_strftime(format = nil) ⇒ String
Formats a datetime in the current timezone.
246 247 248 |
# File 'lib/lazier/datetime.rb', line 246 def local_strftime(format = nil) (respond_to?(:in_time_zone) ? in_time_zone : self).strftime(::DateTime.custom_format(format)) end |
#lstrftime(format = nil) ⇒ String
Formats a datetime, looking up also custom formats.
238 239 240 |
# File 'lib/lazier/datetime.rb', line 238 def lstrftime(format = nil) strftime(::DateTime.custom_format(format.to_s).gsub(/(?<!%)(%[ab])/i) {|mo| localize_time_component(mo) }) end |
#padded_month ⇒ String
Returns the current month number with leading 0.
229 230 231 |
# File 'lib/lazier/datetime.rb', line 229 def padded_month month.indexize end |
#utc_time ⇒ UTC::Time
Returns the UTC::Time representation of the current datetime.
209 210 211 |
# File 'lib/lazier/datetime.rb', line 209 def utc_time utc.to_time end |