Module: Cowtech::Extensions::DateTime::ClassMethods
- Defined in:
- lib/cowtech-extensions/datetime.rb
Overview
General methods.
Instance Method Summary collapse
-
#custom_format(key) ⇒ String
Lookups a custom datetime format.
-
#days(short = true) ⇒ Array
Returns strings representations of days.
-
#easter(year = nil) ⇒ Date
Returns the Easter (according to Gregorian calendar) date for the year.
-
#find_timezone(name = true, dst_label = nil) ⇒ TimeZone
Find a zone by its name.
-
#is_valid?(value, format = "%F %T") ⇒ Boolean
Checks if the date is valid against to a specific format.
-
#list_timezones(with_dst = true, dst_label = nil) ⇒ Array
Returns a list of names of all timezones.
-
#months(short = true) ⇒ Array
Returns strings representations of months.
-
#parameterize_zone(tz, with_offset = true) ⇒ String
Returns a string representation of a timezone.
-
#rationalize_offset(offset) ⇒ Rational
Returns an offset in rational value.
-
#timezones ⇒ Array
Returns all the availabe timezones.
-
#unparameterize_zone(tz, as_string = false, dst_label = nil) ⇒ String|TimeZone
Finds a parameterized timezone.
-
#years(offset = 10, also_future = true, reference = nil, as_objects = false) ⇒ Array
Returns a range of years.
Instance Method Details
#custom_format(key) ⇒ String
Lookups a custom datetime format.
152 153 154 |
# File 'lib/cowtech-extensions/datetime.rb', line 152 def custom_format(key) ::Cowtech::Extensions.settings.date_formats.fetch(key.to_sym, key).ensure_string end |
#days(short = true) ⇒ Array
Returns strings representations of days.
20 21 22 23 24 25 26 |
# File 'lib/cowtech-extensions/datetime.rb', line 20 def days(short = true) days = ::Cowtech::Extensions.settings.date_names[short ? :short_days : :long_days] (1..7).to_a.collect { |i| {:value => i.to_s, :label=> days[i - 1]} } end |
#easter(year = nil) ⇒ Date
Returns the Easter (according to Gregorian calendar) date for the year.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/cowtech-extensions/datetime.rb', line 125 def easter(year = nil) year = ::Date.today.year if !year.is_integer? # Compute using Anonymouse Gregorian Algorithm: http://en.wikipedia.org/wiki/Computus#Anonymous_Gregorian_algorithm a = year % 19 b = (year / 100.0).floor c = year % 100 d = (b / 4.0).floor e = b % 4 f = ((b + 8) / 25.0).floor g = ((b - f + 1) / 3.0).floor h = ((19 * a) + b - d - g + 15) % 30 i = (c / 4.0).floor k = c % 4 l = (32 + (2 * e) + (2 * i) - h - k) % 7 m = ((a + (11 * h) + (22 * l)) / 451.0).floor day = ((h + l - (7 * m) + 114) % 31) + 1 month = ((h + l - (7 * m) + 114) / 31.0).floor ::Date.civil(year, month, day) end |
#find_timezone(name = true, dst_label = nil) ⇒ TimeZone
Find a zone by its name.
84 85 86 |
# File 'lib/cowtech-extensions/datetime.rb', line 84 def find_timezone(name = true, dst_label = nil) ::ActiveSupport::TimeZone.find(name, dst_label) end |
#is_valid?(value, format = "%F %T") ⇒ Boolean
Checks if the date is valid against to a specific format.
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/cowtech-extensions/datetime.rb', line 162 def is_valid?(value, format = "%F %T") rv = true format = self.custom_format(format) begin ::DateTime.strptime(value.ensure_string, format) rescue => e rv = false end rv end |
#list_timezones(with_dst = true, dst_label = nil) ⇒ Array
Returns a list of names of all timezones.
75 76 77 |
# File 'lib/cowtech-extensions/datetime.rb', line 75 def list_timezones(with_dst = true, dst_label = nil) ::ActiveSupport::TimeZone.list_all(with_dst, dst_label) end |
#months(short = true) ⇒ Array
Returns strings representations of months.
33 34 35 36 37 38 |
# File 'lib/cowtech-extensions/datetime.rb', line 33 def months(short = true) months = ::Cowtech::Extensions.settings.date_names[short ? :short_months : :long_months] (1..12).collect { |i| {:value => i.to_s.rjust(2, "0"), :label=> months.at(i - 1)} } end |
#parameterize_zone(tz, with_offset = true) ⇒ String
Returns a string representation of a timezone.
DateTime.parameterize_zone(ActiveSupport::TimeZone["Pacific Time (US & Canada)"])
# => "-0800@pacific-time-us-canada"
97 98 99 |
# File 'lib/cowtech-extensions/datetime.rb', line 97 def parameterize_zone(tz, with_offset = true) ::ActiveSupport::TimeZone::parameterize_zone(tz, with_offset) end |
#rationalize_offset(offset) ⇒ Rational
Returns an offset in rational value.
116 117 118 |
# File 'lib/cowtech-extensions/datetime.rb', line 116 def rationalize_offset(offset) ::ActiveSupport::TimeZone.rationalize_offset(offset) end |
#timezones ⇒ Array
Returns all the availabe timezones.
66 67 68 |
# File 'lib/cowtech-extensions/datetime.rb', line 66 def timezones ::ActiveSupport::TimeZone.all end |
#unparameterize_zone(tz, as_string = false, dst_label = nil) ⇒ String|TimeZone
Finds a parameterized timezone.
108 109 110 |
# File 'lib/cowtech-extensions/datetime.rb', line 108 def unparameterize_zone(tz, as_string = false, dst_label = nil) ::ActiveSupport::TimeZone::unparameterize_zone(tz, as_string, dst_label) end |
#years(offset = 10, also_future = true, reference = nil, as_objects = false) ⇒ Array
Returns a range of years.
Date.years(3, false, 2010)
# => [2007, 2008, 2009, 2010]
Date.years(1, true, 2010, true)
# => [{:value=>2009, :label=>2009}, {:value=>2010, :label=>2010}, {:value=>2011, :label=>2011}]
58 59 60 61 |
# File 'lib/cowtech-extensions/datetime.rb', line 58 def years(offset = 10, also_future = true, reference = nil, as_objects = false) y = reference || ::Date.today.year (y - offset..(also_future ? y + offset : y)).collect { |year| as_objects ? {:value => year, :label => year} : year } end |