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.

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:



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

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

#days(short = true) ⇒ Array

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:



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 do |label, index|
    {value: (index + 1).to_s, label: label}
  end
end

#easter(year = nil) ⇒ Date

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

Parameters:

  • year (Fixnum|NilClass) (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:



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

#months(short = true) ⇒ Array

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:



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 do |label, index|
    {value: (index + 1).to_s.rjust(2, "0"), label: label}
  end
end

#valid?(value, format = "%F %T") ⇒ Boolean

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


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

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

Parameters:

  • offset (Fixnum) (defaults to: 10)

    The width of the range.

  • also_future (Boolean) (defaults to: true)

    If return also future years.

  • reference (Fixnum|NilClass) (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)

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

Returns:

  • (Array)

    A range of years. Every entry is



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