Module: ActiveSupport::CoreExtensions::Time::Calculations::ClassMethods

Defined in:
lib/active_support/core_ext/time/calculations.rb

Instance Method Summary collapse

Instance Method Details

#days_in_month(month, year = nil) ⇒ Object

Return the number of days in the given month. If a year is given, February will return the correct number of days for leap years. Otherwise, this method will always report February as having 28 days.



22
23
24
25
26
27
28
29
30
# File 'lib/active_support/core_ext/time/calculations.rb', line 22

def days_in_month(month, year=nil)
  if month == 2
    !year.nil? && (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) ?  29 : 28
  elsif month <= 7
    month % 2 == 0 ? 30 : 31
  else
    month % 2 == 0 ? 31 : 30
  end
end

#local_time(*args) ⇒ Object

wraps class method time_with_datetime_fallback with utc_or_local == :local



48
49
50
# File 'lib/active_support/core_ext/time/calculations.rb', line 48

def local_time(*args)
  time_with_datetime_fallback(:local, *args)
end

#time_with_datetime_fallback(utc_or_local, year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0) ⇒ Object

Returns a new Time if requested year can be accommodated by Ruby’s Time class (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture); otherwise returns a DateTime



35
36
37
38
39
40
# File 'lib/active_support/core_ext/time/calculations.rb', line 35

def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
  ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
rescue
  offset = utc_or_local.to_sym == :local ? ::DateTime.local_offset : 0
  ::DateTime.civil(year, month, day, hour, min, sec, offset)
end

#utc_time(*args) ⇒ Object

wraps class method time_with_datetime_fallback with utc_or_local == :utc



43
44
45
# File 'lib/active_support/core_ext/time/calculations.rb', line 43

def utc_time(*args)
  time_with_datetime_fallback(:utc, *args)
end