Class: Time
- Inherits:
-
Object
- Object
- Time
- Defined in:
- lib/sg_common/core_ext/time/sg_time.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#beginning_of_sg_day(time_zone) ⇒ Object
Originally #beginning_of_sg_day was available only for Date objects.
- #dt ⇒ Object
- #dth ⇒ Object
- #end_of_sg_day(time_zone) ⇒ Object
- #sg_day(time_zone) ⇒ Object
- #utc_time_offset(time_zone) ⇒ Object
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.
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 |
#dt ⇒ Object
8 9 10 |
# File 'lib/sg_common/core_ext/time/sg_time.rb', line 8 def dt strftime('%Y%m%d') end |
#dth ⇒ Object
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 |