Module: Lazier::DateTime::ClassMethods

Defined in:
lib/lazier/datetime.rb

Overview

General methods.

Instance Method Summary (collapse)

Instance Method Details

- (String) custom_format(key)

Lookups a custom datetime format.

Parameters:

  • key (Symbol)

    The name of the format to search.

Returns:

  • (String)

    The format or the name itself (if the format has not been found).

See Also:



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

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

- (Array) days(short = true)

Returns strings representations of days.

Parameters:

  • short (Boolean) (defaults to: true)

    If return the abbreviated representations.

Returns:

  • (Array)

    Return string representations of days.

See Also:



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

def days(short = true)
  days = ::Lazier.settings.date_names[short ? :short_days : :long_days]
  (1..7).to_a.collect { |i|
    {value: i.to_s, label: days[i - 1]}
  }

end

- (Date) easter(year = nil)

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

Parameters:

  • year (Fixnum) (defaults to: nil)

    The year to compute the date for. Defaults to the current year.

Returns:

  • (Date)

    The Easter date for the year.

See Also:



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

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, b, c = easter_independent(year)
  x, e, i, k = easter_dependent(b, c)
  day, month = easter_summarize(easter_finalize(a, x, e, i, k))

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

- (TimeZone) find_timezone(name = true, dst_label = nil)

Find a zone by its name.

Parameters:

  • name (String) (defaults to: true)

    The zone name.

  • dst_label (String) (defaults to: nil)

    Label for the DST indication. Defaults to (DST).

Returns:

  • (TimeZone)

    A timezone or nil if no zone was found.



83
84
85
# File 'lib/lazier/datetime.rb', line 83

def find_timezone(name = true, dst_label = nil)
  ::ActiveSupport::TimeZone.find(name, dst_label)
end

- (Boolean) is_valid?(value, format = "%F %T")

Checks if the date is valid against to a specific format.

Parameters:

  • value (String)

    The value to check.

  • format (String) (defaults to: "%F %T")

    The format to check the value against.

Returns:

  • (Boolean)

    true if the value is valid against the format, false otherwise.

See Also:

  • DateTime#custom_format


150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/lazier/datetime.rb', line 150

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

- (Array) list_timezones(with_dst = true, dst_label = nil)

Returns a list of names of all timezones.

Parameters:

  • with_dst (Boolean) (defaults to: true)

    If include DST version of the zones.

  • dst_label (String) (defaults to: nil)

    Label for the DST indication. Defaults to (DST).

Returns:

  • (Array)

    A list of names of timezones.



74
75
76
# File 'lib/lazier/datetime.rb', line 74

def list_timezones(with_dst = true, dst_label = nil)
  ::ActiveSupport::TimeZone.list_all(with_dst, dst_label)
end

- (Array) months(short = true)

Returns strings representations of months.

Parameters:

  • short (Boolean) (defaults to: true)

    If return the abbreviated representations.

Returns:

  • (Array)

    Return string representations of months.

See Also:



32
33
34
35
36
37
# File 'lib/lazier/datetime.rb', line 32

def months(short = true)
  months = ::Lazier.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

- (String) parameterize_zone(tz, with_offset = true)

Returns a string representation of a timezone.

DateTime.parameterize_zone(ActiveSupport::TimeZone["Pacific Time (US & Canada)"])
# => "-0800@pacific-time-us-canada"

Parameters:

  • tz (TimeZone)

    The zone to represent.

  • with_offset (Boolean) (defaults to: true)

    If to include offset into the representation.

Returns:

  • (String)

    A string representation which can be used for searches.



96
97
98
# File 'lib/lazier/datetime.rb', line 96

def parameterize_zone(tz, with_offset = true)
  ::ActiveSupport::TimeZone::parameterize_zone(tz, with_offset)
end

- (Rational) rationalize_offset(offset)

Returns an offset in rational value.

Parameters:

  • offset (Fixnum)

    The offset to convert.

Returns:

  • (Rational)

    The converted offset.



115
116
117
# File 'lib/lazier/datetime.rb', line 115

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

- (Array) timezones

Returns all the availabe timezones.

Returns:

  • (Array)

    All the zone available.



65
66
67
# File 'lib/lazier/datetime.rb', line 65

def timezones
  ::ActiveSupport::TimeZone.all
end

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

Finds a parameterized timezone.

Parameters:

  • tz (String)

    The zone to unparameterize.

  • as_string (Boolean) (defaults to: false)

    If return just the zone name.

  • dst_label (String) (defaults to: nil)

    Label for the DST indication. Defaults to (DST).

Returns:

  • (String|TimeZone)

    The found timezone or nil if the zone is not valid.

See Also:

  • DateTime#parameterize_zone


107
108
109
# File 'lib/lazier/datetime.rb', line 107

def unparameterize_zone(tz, as_string = false, dst_label = nil)
  ::ActiveSupport::TimeZone::unparameterize_zone(tz, as_string, dst_label)
end

- (Array) years(offset = 10, also_future = true, reference = nil, as_objects = false)

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

Parameters:

  • offset (Fixnum) (defaults to: 10)

    The width of the range.

  • also_future (Boolean) (defaults to: true)

    If return also future years.

  • reference (Fixnum) (defaults to: nil)

    The ending (or middle, if also_future is true) value of the range. Defaults to the current year.

  • as_objects (Boolean) (defaults to: false)

    If to return years in hashes with :value and label keys.

Returns:

  • (Array)

    A range of years. Every entry is



57
58
59
60
# File 'lib/lazier/datetime.rb', line 57

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