Module: Lazier::DateTime::ClassMethods
- Defined in:
- lib/lazier/datetime.rb
Overview
General methods.
Instance Method Summary (collapse)
-
- (String) custom_format(key)
Lookups a custom datetime format.
-
- (Array) days(short = true)
Returns strings representations of days.
-
- (Date) easter(year = nil)
Returns the Easter (according to Gregorian calendar) date for the year.
-
- (Array) months(short = true)
Returns strings representations of months.
-
- (Boolean) valid?(value, format = "%F %T")
Checks if the date is valid against to a specific format.
-
- (Array) years(offset: 10, also_future: true, reference: nil, as_objects: false)
Returns a range of years.
Instance Method Details
- (String) custom_format(key)
Lookups a custom datetime format.
63 64 65 |
# File 'lib/lazier/datetime.rb', line 63 def custom_format(key) ::Lazier.settings.date_formats.fetch(key, key) end |
- (Array) days(short = true)
Returns strings representations of days.
18 19 20 21 22 |
# File 'lib/lazier/datetime.rb', line 18 def days(short = true) ::Lazier.settings.date_names[short ? :short_days : :long_days].map.with_index {|label, index| {value: (index + 1).to_s, label: label} } end |
- (Date) easter(year = nil)
Returns the Easter (according to Gregorian calendar) date for the year.
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/lazier/datetime.rb', line 85 def easter(year = nil) year = ::Date.today.year unless year.integer? # Compute using Anonymous Gregorian Algorithm: http://en.wikipedia.org/wiki/Computus#Anonymous_Gregorian_algorithm data = easter_start(year) data = easter_divide(data) data = easter_aggregate(year, data) data = easter_prepare(year, data) day, month = easter_end(data) ::Date.civil(year, month, day) end |
- (Array) months(short = true)
Returns strings representations of months.
29 30 31 32 33 |
# File 'lib/lazier/datetime.rb', line 29 def months(short = true) ::Lazier.settings.date_names[short ? :short_months : :long_months].map.with_index {|label, index| {value: (index + 1).to_s.rjust(2, "0"), label: label} } end |
- (Boolean) valid?(value, format = "%F %T")
Checks if the date is valid against to a specific format.
73 74 75 76 77 78 |
# File 'lib/lazier/datetime.rb', line 73 def valid?(value, format = "%F %T") ::DateTime.strptime(value, custom_format(format)) true rescue false end |
- (Array) years(offset: 10, also_future: true, reference: nil, as_objects: false)
Returns a range of years.
ruby
Date.years(3, false, 2010)
# => [2007, 2008, 2009, 2010]
ruby
Date.years(1, true, 2010, true)
# => [{:value=>2009, :label=>2009}, {:value=>2010, :label=>2010}, {:value=>2011, :label=>2011}]
53 54 55 56 |
# File 'lib/lazier/datetime.rb', line 53 def years(offset: 10, also_future: true, reference: nil, as_objects: false) y = reference || ::Date.today.year (y - offset..(also_future ? y + offset : y)).map { |year| as_objects ? {value: year, label: year} : year } end |