Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/sg_common/core_ext/time/sg_time.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_sg_day(time_zone) ⇒ Object



43
44
45
# File 'lib/sg_common/core_ext/time/sg_time.rb', line 43

def self.current_sg_day(time_zone)
  now.utc.sg_day(time_zone)
end

.next_sg_day(time_zone) ⇒ Object



47
48
49
# File 'lib/sg_common/core_ext/time/sg_time.rb', line 47

def self.next_sg_day(time_zone)
  current_sg_day(time_zone) + 1.day
end

Instance Method Details

#beginning_of_sg_day(time_zone) ⇒ Object

Originally #beginning_of_sg_day was available only for Date objects. Due to common usage of Datetime for storing dates in the database, it was required to cast Time to Date before using #beginning_of_sg_day. For convenience #beginning_of_sg_day was added to Time as well.

One **important caveat** is actual time of a day is ignored by this function and this function should be called only for days at midnight.

Parameters:

  • time_zone

    a string, one of ActiveSupport::TimeZone::MAPPING

Returns:

  • beginning of SG day in UTC (e.g. 9/10 for US, 3/4 for italy depending on daylight saving)

Raises:

  • ArgumentError if Time is not at midnight



32
33
34
35
36
37
# File 'lib/sg_common/core_ext/time/sg_time.rb', line 32

def beginning_of_sg_day(time_zone)
  if self.utc.seconds_since_midnight != 0
    fail ArgumentError, "#beginning_of_sg_day can be used only for dates! (Time #{self} has to be at midnight)"
  end
  self.utc.to_date.beginning_of_sg_day(time_zone)
end

#dtObject



8
9
10
# File 'lib/sg_common/core_ext/time/sg_time.rb', line 8

def dt
  strftime('%Y%m%d')
end

#dthObject



12
13
14
# File 'lib/sg_common/core_ext/time/sg_time.rb', line 12

def dth
  strftime('%Y%m%d%H')
end

#end_of_sg_day(time_zone) ⇒ Object



39
40
41
# File 'lib/sg_common/core_ext/time/sg_time.rb', line 39

def end_of_sg_day(time_zone)
  self.utc.to_date.end_of_sg_day(time_zone)
end

#sg_day(time_zone) ⇒ Object



4
5
6
# File 'lib/sg_common/core_ext/time/sg_time.rb', line 4

def sg_day(time_zone)
  (self - (Date::SG_TIME_OFFSET.hours - self.in_time_zone(time_zone).utc_offset)).utc.to_date
end

#utc_time_offset(time_zone) ⇒ Object



16
17
18
# File 'lib/sg_common/core_ext/time/sg_time.rb', line 16

def utc_time_offset(time_zone)
  self.sg_day(time_zone).utc_time_offset(time_zone)
end