Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/extensions/time.rb

Overview

Add workday and weekday concepts to the Time class

Class Method Summary collapse

Class Method Details

.after_business_hours?(time) ⇒ Boolean

Returns:

  • (Boolean)

42
43
44
# File 'lib/extensions/time.rb', line 42

def after_business_hours?(time)
  time > end_of_workday(time)
end

.before_business_hours?(time) ⇒ Boolean

Returns:

  • (Boolean)

38
39
40
# File 'lib/extensions/time.rb', line 38

def before_business_hours?(time)
  time < beginning_of_workday(time)
end

.beginning_of_workday(day) ⇒ Object

Gives the time at the beginning of the workday, assuming that this time falls on a workday. Note: It pretends that this day is a workday whether or not it really is a workday.


19
20
21
22
23
# File 'lib/extensions/time.rb', line 19

def beginning_of_workday(day)
  format = "%B %d %Y #{BusinessTime::Config.beginning_of_workday}"
  Time.zone ? Time.zone.parse(day.strftime(format)) :
      Time.parse(day.strftime(format))
end

.end_of_workday(day) ⇒ Object

Gives the time at the end of the workday, assuming that this time falls on a workday. Note: It pretends that this day is a workday whether or not it really is a workday.


9
10
11
12
13
# File 'lib/extensions/time.rb', line 9

def end_of_workday(day)
  format = "%B %d %Y #{BusinessTime::Config.end_of_workday}"
  Time.zone ? Time.zone.parse(day.strftime(format)) :
      Time.parse(day.strftime(format))
end

.roll_forward(time) ⇒ Object

Rolls forward to the next beginning_of_workday when the time is outside of business hours


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/extensions/time.rb', line 48

def roll_forward(time)

  if (Time.before_business_hours?(time) || !Time.workday?(time))
    next_business_time = Time.beginning_of_workday(time)
  elsif Time.after_business_hours?(time)
    next_business_time = Time.beginning_of_workday(time) + 1.day
  else
    next_business_time = time.clone
  end

  while !Time.workday?(next_business_time)
    next_business_time += 1.day
  end

  next_business_time
end

.weekday?(day) ⇒ Boolean

True if this time falls on a weekday.

Returns:

  • (Boolean)

33
34
35
36
# File 'lib/extensions/time.rb', line 33

def weekday?(day)
  # TODO AS: Internationalize this!
  [1,2,3,4,5].include? day.wday
end

.workday?(day) ⇒ Boolean

True if this time is on a workday (between 00:00:00 and 23:59:59), even if this time falls outside of normal business hours.

Returns:

  • (Boolean)

27
28
29
30
# File 'lib/extensions/time.rb', line 27

def workday?(day)
  Time.weekday?(day) &&
      !BusinessTime::Config.holidays.include?(day.to_date)
end