Class: WorkingTime::DateTime

Inherits:
DateTime
  • Object
show all
Defined in:
lib/working_time/date.rb

Overview

Lets just modify the basic DateTime class

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.now(sg = ITALY) ⇒ Object



63
# File 'lib/working_time/date.rb', line 63

def self.now  (sg=ITALY) WorkingTime::Time.now.__send__(:to_datetime).new_start(sg) end

Instance Method Details

#+(n) ⇒ Object

override addition

Raises:

  • (TypeError)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/working_time/date.rb', line 114

def + (n)
  case n
  when Numeric; return self.class.new!(@ajd + n, @of, @sg)
  when WorkingTime::Hour

    cur_hour = self.hour
    hours_remaining = n.to_i

    min  = self.min
    sec  = self.sec

    date = Date.new(self.year,self.mon,self.day)

    # if our initial value is before the start of the work day, set the cur_hour to the start of the workday
    if cur_hour < WORKING_HOURS[0]
      cur_hour = WORKING_HOURS[0]
    # if our initial value is after the end of the current work day, set the cur_hour to the start of the NEXT workday
    elsif cur_hour > WORKING_HOURS[-1]
      cur_hour = WORKING_HOURS[0]
      date = date.next
    end

    #puts "Incrementing #{self}"
    # Add hours to the current hour, incrementing the day until we run out of hours
    while hours_remaining > 0
      until cur_hour == WORKING_HOURS[-1] || hours_remaining < 1
        hours_remaining -= 1
        cur_hour        += 1
        #puts "Cur: #{cur_hour} / Rem: #{hours_remaining}"
      end
      if hours_remaining > 0
        hours_remaining -= 1
        cur_hour = WORKING_HOURS[0]
        # go to the next day
        date = date.next
        #puts "Rolling Date: #{date} #{cur_hour}"
      end
    end
    return self.class.new(date.year, date.mon, date.day, cur_hour, min, sec, @of, @sg)
  end
  raise TypeError, 'expected numeric or hour'
end

#close_of_businessObject



83
84
85
# File 'lib/working_time/date.rb', line 83

def close_of_business
  self.class.new(self.year, self.mon, self.day, WORKING_HOURS[-1], 0, 0, @of, @sg)
end

#close_of_next_weekObject



93
94
95
96
97
# File 'lib/working_time/date.rb', line 93

def close_of_next_week
  date = Date.commercial(year, cweek + 1, cwday)
  date = date.next until date.cwday == WORKING_DAYS[-1]
  self.class.new(date.year, date.mon, date.day, WORKING_HOURS[-1], 0, 0, @of, @sg)
end

#close_of_weekObject



87
88
89
90
91
# File 'lib/working_time/date.rb', line 87

def close_of_week
  date = Date.new(self.year, self.mon, self.day)
  date = date.next until date.cwday == WORKING_DAYS[-1]
  self.class.new(date.year, date.mon, date.day, WORKING_HOURS[-1], 0, 0, @of, @sg)
end

#hours_leftObject

Returns the number of hours left in the day.



66
67
68
69
# File 'lib/working_time/date.rb', line 66

def hours_left
  remaining = WorkingTime::Interval.new(self, close_of_business)
  return remaining.duration
end

#is_working_time?Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/working_time/date.rb', line 99

def is_working_time?
  # check the day of week
  if WORKING_DAYS.include? cwday
    #check the hour
    if WORKING_HOURS.include? hour
      return true
    else
      return false
    end
  else
    return false
  end
end

#mins_leftObject



71
72
73
# File 'lib/working_time/date.rb', line 71

def mins_left
  hours_left * 60
end

#open_of_businessObject



79
80
81
# File 'lib/working_time/date.rb', line 79

def open_of_business
  self.class.new(self.year, self.mon, self.day, WORKING_HOURS[0], 0, 0, @of, @sg)
end

#secs_leftObject



75
76
77
# File 'lib/working_time/date.rb', line 75

def secs_left
  hours_left * 3600
end

#to_gm_timeObject



157
158
159
# File 'lib/working_time/date.rb', line 157

def to_gm_time
  to_time(new_offset, :gm)
end

#to_local_timeObject



161
162
163
# File 'lib/working_time/date.rb', line 161

def to_local_time
  to_time(new_offset(DateTime.now.offset-offset), :local)
end