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
-
#format(format = nil, custom: true, change_time_zone: false) ⇒ String
Formats a datetime, eventually looking up also custom formats and/or moving to the current timezone.
-
#months_since_year(base = nil) ⇒ Fixnum
Returns the number of months passed between the beginning of the base year and the current date.
-
#padded_month ⇒ String
Returns the current month number with a leading zero if needed.
Instance Method Details
#format(format = nil, custom: true, change_time_zone: false) ⇒ String
Formats a datetime, eventually looking up also custom formats and/or moving to the current timezone.
171 172 173 174 175 |
# File 'lib/lazier/datetime.rb', line 171 def format(format = nil, custom: true, change_time_zone: false) target = change_time_zone && respond_to?(:in_time_zone) ? in_time_zone : self format = custom ? ::DateTime.custom_format(format.to_s).gsub(/(?<!%)(%[ab])/i) { |mo| localize_time_component(mo) } : format.to_s target.strftime(format) end |
#months_since_year(base = nil) ⇒ Fixnum
Returns the number of months passed between the beginning of the base year and the current date.
Example:
DateTime.civil(2013, 6, 1).in_months(2011)
# => 18
153 154 155 |
# File 'lib/lazier/datetime.rb', line 153 def months_since_year(base = nil) (year - (base || ::Date.today.year)) * 12 + month end |
#padded_month ⇒ String
Returns the current month number with a leading zero if needed.
160 161 162 |
# File 'lib/lazier/datetime.rb', line 160 def padded_month month.indexize end |