module Lazier::DateTime::ClassMethods

General methods.

Public Instance Methods

custom_format(key) click to toggle source

Lookups a custom datetime format. @see Settings#setup_date_formats

@param key [Symbol] The name of the format to search. @return [String] The format or the name itself (if the format has not been found).

# File lib/lazier/datetime.rb, line 63
def custom_format(key)
  ::Lazier.settings.date_formats.fetch(key, key)
end
days(short = true) click to toggle source

Returns strings representations of days. @see Settings#setup_date_names

@param short [Boolean] If return the abbreviated representations. @return [Array] Return string representations of days.

# 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) click to toggle source

Returns the Easter (according to Gregorian calendar) date for the year. @see en.wikipedia.org/wiki/Computus#Anonymous_Gregorian_algorithm

@param year [Fixnum|NilClass] The year to compute the date for. Defaults to the current year. @return [Date] The Easter date for the year.

# 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) click to toggle source

Returns strings representations of months. @see Settings#setup_date_names

@param short [Boolean] If return the abbreviated representations. @return [Array] Return string representations of months.

# 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") click to toggle source

Checks if the date is valid against to a specific format. @see DateTime#custom_format

@param value [String] The value to check. @param format [String] The format to check the value against. @return [Boolean] `true` if the value is valid against the format, `false` otherwise.

# 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) click to toggle source

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

@param offset [Fixnum] The width of the range. @param also_future [Boolean] If return also future years. @param reference [Fixnum|NilClass] The ending (or middle, if `also_future` is `true`) value of the range. Defaults to the current year. @param as_objects [Boolean] Whether to return years in hashes with `:value` and `label` keys. @return [Array] A range of years. Every entry is

# 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