Module: Useful::RubyExtensionsFromRails::Time::ClassMethods

Defined in:
lib/useful/ruby_extensions_from_rails/time.rb

Instance Method Summary collapse

Instance Method Details

#currentObject

Returns Time.now



54
55
56
# File 'lib/useful/ruby_extensions_from_rails/time.rb', line 54

def current
  ::Time.now
end

#days_in_month(month, year = now.year) ⇒ Object

Return the number of days in the given month. If no year is specified, it will use the current year.



31
32
33
34
# File 'lib/useful/ruby_extensions_from_rails/time.rb', line 31

def days_in_month(month, year = now.year)
  return 29 if month == 2 && ::Date.gregorian_leap?(year)
  COMMON_YEAR_DAYS_IN_MONTH[month]
end

#local_time(*args) ⇒ Object

Wraps class method time_with_datetime_fallback with utc_or_local set to :local.



49
50
51
# File 'lib/useful/ruby_extensions_from_rails/time.rb', line 49

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



39
40
41
# File 'lib/useful/ruby_extensions_from_rails/time.rb', line 39

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)
end

#utc_time(*args) ⇒ Object

Wraps class method time_with_datetime_fallback with utc_or_local set to :utc.



44
45
46
# File 'lib/useful/ruby_extensions_from_rails/time.rb', line 44

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