Class: DtTools

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/objs/dt/dt_tools.rb

Class Method Summary collapse

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_weekObject

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?

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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