Module: Lazier::DateTime::ClassMethods

Defined in:
lib/lazier/datetime.rb

Overview

General methods.

Instance Method Summary collapse

Instance Method Details

#custom_format(key) ⇒ String

Lookups a custom datetime format.



139
140
141
# File 'lib/lazier/datetime.rb', line 139

def custom_format(key)
  ::Lazier.settings.date_formats.fetch(key, key)
end

#days(short = true) ⇒ Array

Returns strings representations of days.



19
20
21
22
23
# File 'lib/lazier/datetime.rb', line 19

def days(short = true)
  ::Lazier.settings.date_names[short ? :short_days : :long_days].collect.with_index {|label, index|
    {value: (index + 1).to_s, label: label}
  }
end

#easter(year = nil) ⇒ Date

Returns the Easter (according to Gregorian calendar) date for the year.



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lazier/datetime.rb', line 121

def easter(year = nil)
  year = ::Date.today.year if !year.is_integer?

  # Compute using Anonymous Gregorian Algorithm: http://en.wikipedia.org/wiki/Computus#Anonymous_Gregorian_algorithm
  data = easter_start(year)
  data = easter_part_1(data)
  data = easter_part_2(year, data)
  data = easter_part_3(year, data)
  day, month = easter_end(data)

  ::Date.civil(year, month, day)
end

#find_timezone(name = true, dst_label = nil) ⇒ TimeZone

Find a zone by its name.



80
81
82
# File 'lib/lazier/datetime.rb', line 80

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.

See Also:

  • DateTime#custom_format


149
150
151
# File 'lib/lazier/datetime.rb', line 149

def is_valid?(value, format = "%F %T")
  !(::DateTime.strptime(value, self.custom_format(format)) rescue nil).nil?
end

#list_timezones(with_dst = true, dst_label = nil) ⇒ Array

Returns a list of names of all timezones.



71
72
73
# File 'lib/lazier/datetime.rb', line 71

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.



30
31
32
33
34
# File 'lib/lazier/datetime.rb', line 30

def months(short = true)
  ::Lazier.settings.date_names[short ? :short_months : :long_months].collect.with_index {|label, index|
    {value: (index + 1).to_s.rjust(2, "0"), label: label}
  }
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"


93
94
95
# File 'lib/lazier/datetime.rb', line 93

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.



112
113
114
# File 'lib/lazier/datetime.rb', line 112

def rationalize_offset(offset)
  ::ActiveSupport::TimeZone.rationalize_offset(offset)
end

#timezonesArray

Returns all the availabe timezones.



62
63
64
# File 'lib/lazier/datetime.rb', line 62

def timezones
  ::ActiveSupport::TimeZone.all
end

#unparameterize_zone(tz, as_string = false, dst_label = nil) ⇒ String|TimeZone

Finds a parameterized timezone.

See Also:

  • DateTime#parameterize_zone


104
105
106
# File 'lib/lazier/datetime.rb', line 104

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}]


54
55
56
57
# File 'lib/lazier/datetime.rb', line 54

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