Class: DtTools
- Inherits:
-
Object
- Object
- DtTools
- Defined in:
- lib/gloo/objs/dt/dt_tools.rb
Class Method Summary collapse
-
.add(date, modifier = "1 day") ⇒ Object
Given a date, add the modifier to it.
-
.beginning_of_month(t) ⇒ Object
Returns the beginning of the month for the given time.
-
.beginning_of_week ⇒ Object
Get the beginning of the week.
-
.end_of_month(t) ⇒ Object
Returns the end of the month for the given time.
-
.get_utc_offset(t) ⇒ Object
——————————————————————— Begin/end Helpers ———————————————————————.
-
.in_next_ten_days?(dt) ⇒ Boolean
Is the date in the next 10 days?.
-
.is_dt_type?(obj) ⇒ Boolean
Is the given object a base Date Time object? True for DateTime and Time.
-
.is_future?(dt) ⇒ Boolean
Is the date in the future?.
-
.is_past?(dt) ⇒ Boolean
Is the date in the past?.
-
.is_this_week?(dt) ⇒ Boolean
Is the given date this week?.
-
.is_today?(dt) ⇒ Boolean
Is the given date today?.
-
.is_tomorrow?(dt) ⇒ Boolean
Is the given date tomorrow?.
-
.is_yesterday?(dt) ⇒ Boolean
Is the given date yesterday?.
-
.sub(dt, modifier = "1 day") ⇒ Object
Given a date, subtract the modifier from it.
Class Method Details
.add(date, modifier = "1 day") ⇒ Object
Given a date, add the modifier to it. The modifier takes the form of “1 day” or “2 weeks”.
33 34 35 36 37 38 39 40 |
# File 'lib/gloo/objs/dt/dt_tools.rb', line 33 def self.add date, modifier="1 day" # Split out amount and unit amount, unit = modifier.split(' ') # converts "1 day" to 1.day duration = amount.to_i.send( unit ) return date + duration end |
.beginning_of_month(t) ⇒ Object
Returns the beginning of the month for the given time.
146 147 148 149 |
# File 'lib/gloo/objs/dt/dt_tools.rb', line 146 def self.beginning_of_month t utc_offset = get_utc_offset t Time.new(t.year, t.month, 1, 0, 0, 0, utc_offset) end |
.beginning_of_week ⇒ Object
Get the beginning of the week.
63 64 65 |
# File 'lib/gloo/objs/dt/dt_tools.rb', line 63 def self.beginning_of_week return Time.now.beginning_of_week( start_day = :sunday ) end |
.end_of_month(t) ⇒ Object
Returns the end of the month for the given time.
154 155 156 157 |
# File 'lib/gloo/objs/dt/dt_tools.rb', line 154 def self.end_of_month t utc_offset = get_utc_offset t Time.new(t.year, t.month, t.end_of_month.day, 23, 59, 59, utc_offset) end |
.get_utc_offset(t) ⇒ Object
Begin/end Helpers
138 139 140 141 |
# File 'lib/gloo/objs/dt/dt_tools.rb', line 138 def self.get_utc_offset t utc_offset = ActiveSupport::TimeZone[ t.zone ].utc_offset return utc_offset end |
.in_next_ten_days?(dt) ⇒ Boolean
Is the date in the next 10 days?
70 71 72 73 |
# File 'lib/gloo/objs/dt/dt_tools.rb', line 70 def self.in_next_ten_days?( dt ) return false if DtTools.is_past?( dt ) dt < 10.days.from_now.end_of_day end |
.is_dt_type?(obj) ⇒ Boolean
Is the given object a base Date Time object? True for DateTime and Time
18 19 20 21 22 |
# File 'lib/gloo/objs/dt/dt_tools.rb', line 18 def self.is_dt_type? obj return true if obj.is_a? ::DateTime return true if obj.is_a? ::Time return false end |
.is_future?(dt) ⇒ Boolean
Is the date in the future?
85 86 87 |
# File 'lib/gloo/objs/dt/dt_tools.rb', line 85 def self.is_future?( dt ) dt > Time.now.end_of_day end |
.is_past?(dt) ⇒ Boolean
Is the date in the past?
78 79 80 |
# File 'lib/gloo/objs/dt/dt_tools.rb', line 78 def self.is_past?( dt ) dt < Time.now.beginning_of_day end |
.is_this_week?(dt) ⇒ Boolean
Is the given date this week?
125 126 127 128 129 130 131 |
# File 'lib/gloo/objs/dt/dt_tools.rb', line 125 def self.is_this_week?( dt ) return false if dt.blank? dt = Chronic.parse( dt ) if dt.is_a?( String ) return false if dt <= ::Time.now.beginning_of_week( start_day = :sunday ) return false if dt >= ::Time.now.end_of_week( start_day = :sunday ) return true end |
.is_today?(dt) ⇒ Boolean
Is the given date today?
92 93 94 95 96 97 98 |
# File 'lib/gloo/objs/dt/dt_tools.rb', line 92 def self.is_today?( dt ) return false if dt.blank? dt = Chronic.parse( dt ) if dt.is_a? String return false if dt <= ::Time.now.beginning_of_day return false if dt >= ::Time.now.end_of_day return true end |
.is_tomorrow?(dt) ⇒ Boolean
Is the given date tomorrow?
103 104 105 106 107 108 109 |
# File 'lib/gloo/objs/dt/dt_tools.rb', line 103 def self.is_tomorrow?( dt ) return false if dt.blank? dt = Chronic.parse( dt ) if dt.is_a? String return false if dt <= ( ::Time.now.beginning_of_day + 1.day ) return false if dt >= ( ::Time.now.end_of_day + 1.day ) return true end |
.is_yesterday?(dt) ⇒ Boolean
Is the given date yesterday?
114 115 116 117 118 119 120 |
# File 'lib/gloo/objs/dt/dt_tools.rb', line 114 def self.is_yesterday?( dt ) return false if dt.blank? dt = Chronic.parse( dt ) if dt.is_a? String return false if dt <= ( ::Time.now.beginning_of_day - 1.day ) return false if dt >= ( ::Time.now.end_of_day - 1.day ) return true end |
.sub(dt, modifier = "1 day") ⇒ Object
Given a date, subtract the modifier from it. The modifier takes the form of “1 day” or “2 weeks”.
46 47 48 49 50 51 52 53 |
# File 'lib/gloo/objs/dt/dt_tools.rb', line 46 def self.sub dt, modifier="1 day" # Split out amount and unit amount, unit = modifier.split(' ') # converts "1 day" to 1.day duration = amount.to_i.send( unit ) return dt - duration end |