alphasights-business_time 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,75 @@
1
+ require 'helper'
2
+
3
+ class TestBusinessHours < Test::Unit::TestCase
4
+
5
+ context "with a TimeWithZone object in US Eastern" do
6
+ setup do
7
+ Time.zone = 'Eastern Time (US & Canada)'
8
+ end
9
+ teardown do
10
+ Time.zone = nil
11
+ end
12
+
13
+ should "move to tomorrow if we add 8 business hours" do
14
+ first = Time.zone.parse("Aug 4 2010, 9:35 am")
15
+ later = 8.business_hours.after(first)
16
+ expected = Time.zone.parse("Aug 5 2010, 9:35 am")
17
+ assert_equal expected, later
18
+ end
19
+
20
+ should "move to yesterday if we subtract 8 business hours" do
21
+ first = Time.zone.parse("Aug 4 2010, 9:35 am")
22
+ later = 8.business_hours.before(first)
23
+ expected = Time.zone.parse("Aug 3 2010, 9:35 am")
24
+ assert_equal expected, later
25
+ end
26
+
27
+ should "take into account a weekend when adding an hour" do
28
+ friday_afternoon = Time.zone.parse("April 9th, 4:50 pm")
29
+ monday_morning = 1.business_hour.after(friday_afternoon)
30
+ expected = Time.zone.parse("April 12th 2010, 9:50 am")
31
+ assert_equal expected, monday_morning
32
+ end
33
+
34
+ should "take into account a weekend when subtracting an hour" do
35
+ monday_morning = Time.zone.parse("April 12th 2010, 9:50 am")
36
+ friday_afternoon = 1.business_hour.before(monday_morning)
37
+ expected = Time.zone.parse("April 9th 2010, 4:50 pm")
38
+ assert_equal expected, friday_afternoon
39
+ end
40
+
41
+ should "take into account a holiday" do
42
+ BusinessTime::Config.holidays << Date.parse("July 5th, 2010")
43
+ friday_afternoon = Time.zone.parse("July 2nd 2010, 4:50pm")
44
+ tuesday_morning = 1.business_hour.after(friday_afternoon)
45
+ expected = Time.zone.parse("July 6th 2010, 9:50 am")
46
+ assert_equal expected, tuesday_morning
47
+ end
48
+
49
+ should "add hours in the middle of the workday" do
50
+ monday_morning = Time.zone.parse("April 12th 2010, 9:50 am")
51
+ later = 3.business_hours.after(monday_morning)
52
+ expected = Time.zone.parse("April 12th 2010, 12:50 pm")
53
+ assert_equal expected, later
54
+ end
55
+
56
+ should "roll forward to 9 am if asked in the early morning" do
57
+ crack_of_dawn_monday = Time.zone.parse("Mon Apr 26, 04:30:00, 2010")
58
+ monday_morning = Time.zone.parse("Mon Apr 26, 09:00:00, 2010")
59
+ assert_equal monday_morning, Time.roll_forward(crack_of_dawn_monday)
60
+ end
61
+
62
+ should "roll forward to the next morning if aftern business hours" do
63
+ monday_evening = Time.zone.parse("Mon Apr 26, 18:00:00, 2010")
64
+ tuesday_morning = Time.zone.parse("Tue Apr 27, 09:00:00, 2010")
65
+ assert_equal tuesday_morning, Time.roll_forward(monday_evening)
66
+ end
67
+
68
+ should "consider any time on a weekend as equivalent to monday morning" do
69
+ sunday = Time.zone.parse("Sun Apr 25 12:06:56, 2010")
70
+ monday = Time.zone.parse("Mon Apr 26, 09:00:00, 2010")
71
+ assert_equal 1.business_hour.before(monday), 1.business_hour.before(sunday)
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,75 @@
1
+ require 'helper'
2
+
3
+ class TestBusinessHours < Test::Unit::TestCase
4
+
5
+ context "with a TimeWithZone object in UTC" do
6
+ setup do
7
+ Time.zone = 'UTC'
8
+ end
9
+ teardown do
10
+ Time.zone = nil
11
+ end
12
+
13
+ should "move to tomorrow if we add 8 business hours" do
14
+ first = Time.zone.parse("Aug 4 2010, 9:35 am")
15
+ later = 8.business_hours.after(first)
16
+ expected = Time.zone.parse("Aug 5 2010, 9:35 am")
17
+ assert_equal expected, later
18
+ end
19
+
20
+ should "move to yesterday if we subtract 8 business hours" do
21
+ first = Time.zone.parse("Aug 4 2010, 9:35 am")
22
+ later = 8.business_hours.before(first)
23
+ expected = Time.zone.parse("Aug 3 2010, 9:35 am")
24
+ assert_equal expected, later
25
+ end
26
+
27
+ should "take into account a weekend when adding an hour" do
28
+ friday_afternoon = Time.zone.parse("April 9th, 4:50 pm")
29
+ monday_morning = 1.business_hour.after(friday_afternoon)
30
+ expected = Time.zone.parse("April 12th 2010, 9:50 am")
31
+ assert_equal expected, monday_morning
32
+ end
33
+
34
+ should "take into account a weekend when subtracting an hour" do
35
+ monday_morning = Time.zone.parse("April 12th 2010, 9:50 am")
36
+ friday_afternoon = 1.business_hour.before(monday_morning)
37
+ expected = Time.zone.parse("April 9th 2010, 4:50 pm")
38
+ assert_equal expected, friday_afternoon, "Expected #{expected} but received #{friday_afternoon}"
39
+ end
40
+
41
+ should "take into account a holiday" do
42
+ BusinessTime::Config.holidays << Date.parse("July 5th, 2010")
43
+ friday_afternoon = Time.zone.parse("July 2nd 2010, 4:50pm")
44
+ tuesday_morning = 1.business_hour.after(friday_afternoon)
45
+ expected = Time.zone.parse("July 6th 2010, 9:50 am")
46
+ assert_equal expected, tuesday_morning
47
+ end
48
+
49
+ should "add hours in the middle of the workday" do
50
+ monday_morning = Time.zone.parse("April 12th 2010, 9:50 am")
51
+ later = 3.business_hours.after(monday_morning)
52
+ expected = Time.zone.parse("April 12th 2010, 12:50 pm")
53
+ assert_equal expected, later
54
+ end
55
+
56
+ should "roll forward to 9 am if asked in the early morning" do
57
+ crack_of_dawn_monday = Time.zone.parse("Mon Apr 26, 04:30:00, 2010")
58
+ monday_morning = Time.zone.parse("Mon Apr 26, 09:00:00, 2010")
59
+ assert_equal monday_morning, Time.roll_forward(crack_of_dawn_monday)
60
+ end
61
+
62
+ should "roll forward to the next morning if aftern business hours" do
63
+ monday_evening = Time.zone.parse("Mon Apr 26, 18:00:00, 2010")
64
+ tuesday_morning = Time.zone.parse("Tue Apr 27, 09:00:00, 2010")
65
+ assert_equal tuesday_morning, Time.roll_forward(monday_evening)
66
+ end
67
+
68
+ should "consider any time on a weekend as equivalent to monday morning" do
69
+ sunday = Time.zone.parse("Sun Apr 25 12:06:56, 2010")
70
+ monday = Time.zone.parse("Mon Apr 26, 09:00:00, 2010")
71
+ assert_equal 1.business_hour.before(monday), 1.business_hour.before(sunday)
72
+ end
73
+ end
74
+
75
+ end
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+
3
+ class TestConfig < Test::Unit::TestCase
4
+
5
+ should "keep track of the start of the day" do
6
+ assert_equal BusinessTime::Config.beginning_of_workday, "9:00 am"
7
+ BusinessTime::Config.beginning_of_workday = "8:30 am"
8
+ assert_equal BusinessTime::Config.beginning_of_workday, "8:30 am"
9
+ end
10
+
11
+ should "keep track of the end of the day" do
12
+ assert_equal BusinessTime::Config.end_of_workday, "5:00 pm"
13
+ BusinessTime::Config.end_of_workday = "5:30 pm"
14
+ assert_equal BusinessTime::Config.end_of_workday, "5:30 pm"
15
+ end
16
+
17
+ should "keep track of holidays" do
18
+ BusinessTime::Config.reset
19
+ assert BusinessTime::Config.holidays.empty?
20
+ daves_birthday = Date.parse("August 4th, 1969")
21
+ BusinessTime::Config.holidays << daves_birthday
22
+ assert BusinessTime::Config.holidays.include?(daves_birthday)
23
+ end
24
+
25
+ end
@@ -0,0 +1,33 @@
1
+ require 'helper'
2
+
3
+ class TestDateExtensions < Test::Unit::TestCase
4
+
5
+ should "know what a weekend day is" do
6
+ assert(Date.parse("April 9, 2010").weekday?)
7
+ assert(!Date.parse("April 10, 2010").weekday?)
8
+ assert(!Date.parse("April 11, 2010").weekday?)
9
+ assert(Date.parse("April 12, 2010").weekday?)
10
+ end
11
+
12
+ should "know a weekend day is not a workday" do
13
+ assert(Date.parse("April 9, 2010").workday?)
14
+ assert(!Date.parse("April 10, 2010").workday?)
15
+ assert(!Date.parse("April 11, 2010").workday?)
16
+ assert(Date.parse("April 12, 2010").workday?)
17
+ end
18
+
19
+ should "know a holiday is not a workday" do
20
+ july_4 = Date.parse("July 4, 2010")
21
+ july_5 = Date.parse("July 5, 2010")
22
+
23
+ assert(!july_4.workday?)
24
+ assert(july_5.workday?)
25
+
26
+ BusinessTime::Config.holidays << july_4
27
+ BusinessTime::Config.holidays << july_5
28
+
29
+ assert(!july_4.workday?)
30
+ assert(!july_5.workday?)
31
+ end
32
+
33
+ end
@@ -0,0 +1,17 @@
1
+ require 'helper'
2
+
3
+ class TestFixnumExtensions < Test::Unit::TestCase
4
+
5
+ should "respond to business_hours by returning an instance of BusinessHours" do
6
+ assert(1.respond_to?(:business_hour))
7
+ assert(1.respond_to?(:business_hours))
8
+ assert 1.business_hour.instance_of?(BusinessTime::BusinessHours)
9
+ end
10
+
11
+ should "respond to business_days by returning an instance of BusinessDays" do
12
+ assert(1.respond_to?(:business_day))
13
+ assert(1.respond_to?(:business_days))
14
+ assert 1.business_day.instance_of?(BusinessTime::BusinessDays)
15
+ end
16
+
17
+ end
@@ -0,0 +1,131 @@
1
+ require 'helper'
2
+
3
+ class TestTimeExtensions < Test::Unit::TestCase
4
+
5
+ should "know what a weekend day is" do
6
+ assert( Time.weekday?(Time.parse("April 9, 2010 10:30am")))
7
+ assert(!Time.weekday?(Time.parse("April 10, 2010 10:30am")))
8
+ assert(!Time.weekday?(Time.parse("April 11, 2010 10:30am")))
9
+ assert( Time.weekday?(Time.parse("April 12, 2010 10:30am")))
10
+ end
11
+
12
+ should "know a weekend day is not a workday" do
13
+ assert( Time.workday?(Time.parse("April 9, 2010 10:45 am")))
14
+ assert(!Time.workday?(Time.parse("April 10, 2010 10:45 am")))
15
+ assert(!Time.workday?(Time.parse("April 11, 2010 10:45 am")))
16
+ assert( Time.workday?(Time.parse("April 12, 2010 10:45 am")))
17
+ end
18
+
19
+ should "know a holiday is not a workday" do
20
+ BusinessTime::Config.reset
21
+
22
+ BusinessTime::Config.holidays << Date.parse("July 4, 2010")
23
+ BusinessTime::Config.holidays << Date.parse("July 5, 2010")
24
+
25
+ assert(!Time.workday?(Time.parse("July 4th, 2010 1:15 pm")))
26
+ assert(!Time.workday?(Time.parse("July 5th, 2010 2:37 pm")))
27
+ end
28
+
29
+
30
+ should "know the beginning of the day for an instance" do
31
+ first = Time.parse("August 17th, 2010, 11:50 am")
32
+ expecting = Time.parse("August 17th, 2010, 9:00 am")
33
+ assert_equal expecting, Time.beginning_of_workday(first)
34
+ end
35
+
36
+ should "know the end of the day for an instance" do
37
+ first = Time.parse("August 17th, 2010, 11:50 am")
38
+ expecting = Time.parse("August 17th, 2010, 5:00 pm")
39
+ assert_equal expecting, Time.end_of_workday(first)
40
+ end
41
+
42
+ should "know if instance is during business hours" do
43
+ assert(!Time.during_business_hours?(Time.parse("April 11, 2010 10:45 am")))
44
+ assert(!Time.during_business_hours?(Time.parse("April 12, 2010 8:45 am")))
45
+ assert(Time.during_business_hours?(Time.parse("April 12, 2010 9:45 am")))
46
+ end
47
+
48
+
49
+ should "know a seconds to an instance" do
50
+ start_time = Time.parse("August 17th, 2010, 8:50 am")
51
+ end_time = Time.parse("August 17th, 2010, 13:00 pm")
52
+ expecting = start_time.business_time_left_to(end_time)
53
+ assert_equal expecting, 3600 * 4
54
+ end
55
+
56
+ should "know a seconds to an instance" do
57
+ start_time = Time.parse("August 17th, 2010, 11:50 am")
58
+ end_time = Time.parse("August 17th, 2010, 13:50 pm")
59
+ expecting = start_time.business_time_left_to(end_time)
60
+ assert_equal expecting, 7200
61
+ end
62
+
63
+ should "know a seconds to an instance" do
64
+ start_time = Time.parse("August 17th, 2010, 17:50 am")
65
+ end_time = Time.parse("August 17th, 2010, 18:50 pm")
66
+ expecting = start_time.business_time_left_to(end_time)
67
+ assert_equal expecting, 0
68
+ end
69
+
70
+ should "know a seconds to an instance in weekend" do
71
+ start_time = Time.parse("August 22th, 2010, 11:50 am")
72
+ end_time = Time.parse("August 22th, 2010, 13:50 pm")
73
+ expecting = start_time.business_time_left_to(end_time)
74
+ assert_equal expecting, 0
75
+ end
76
+
77
+ should "know a seconds to an instance between holidays" do
78
+ start_time = Time.parse("August 22th, 2010, 11:50 am")
79
+ BusinessTime::Config.reset
80
+
81
+ BusinessTime::Config.holidays << Date.parse("August 24, 2010")
82
+ end_time = Time.parse("August 24th, 2010, 13:50 pm")
83
+ expecting = start_time.business_time_left_to(end_time)
84
+ assert_equal expecting, 8 * 3600
85
+ end
86
+
87
+ should "know a seconds to an other day instance" do
88
+ start_time = Time.parse("August 17th, 2010, 16:00 pm")
89
+ end_time = Time.parse("August 18th, 2010, 9:00 am")
90
+ expecting = start_time.business_time_left_to(end_time)
91
+ assert_equal expecting, 3600
92
+ end
93
+
94
+ should "know a seconds to an other week instance" do
95
+ start_time = Time.parse("August 17th, 2010, 16:00 pm")
96
+ end_time = Time.parse("August 23th, 2010, 10:00 am")
97
+ expecting = start_time.business_time_left_to(end_time)
98
+ assert_equal expecting, (3600 * 26)
99
+ end
100
+
101
+ should "know a seconds to end of business hours" do
102
+ time = Time.parse("August 17th, 2010, 16:00 pm")
103
+ expecting = time.business_time_left_to_end
104
+ assert_equal expecting, 3600
105
+ time = Time.parse("August 17th, 2010, 8:00 am")
106
+ expecting = time.business_time_left_to_end
107
+ assert_equal expecting, 3600 * 8
108
+ time = Time.parse("August 17th, 2010, 18:00 pm")
109
+ expecting = time.business_time_left_to_end
110
+ assert_equal expecting, 0
111
+ time = Time.parse("August 21th, 2010, 16:00 pm")
112
+ expecting = time.business_time_left_to_end
113
+ assert_equal expecting, 0
114
+ end
115
+
116
+ should "know a seconds from beginning of business hours" do
117
+ time = Time.parse("August 17th, 2010, 16:00 pm")
118
+ expecting = time.business_time_passed_from_beginning
119
+ assert_equal expecting, 3600 * 7
120
+ time = Time.parse("August 17th, 2010, 8:00 am")
121
+ expecting = time.business_time_passed_from_beginning
122
+ assert_equal expecting, 0
123
+ time = Time.parse("August 17th, 2010, 18:00 pm")
124
+ expecting = time.business_time_passed_from_beginning
125
+ assert_equal expecting, 3600 * 8
126
+ time = Time.parse("August 21th, 2010, 16:00 pm")
127
+ expecting = time.business_time_passed_from_beginning
128
+ assert_equal expecting, 0
129
+ end
130
+
131
+ end
@@ -0,0 +1,92 @@
1
+ require 'helper'
2
+
3
+ class TestTimeWithZoneExtensions < Test::Unit::TestCase
4
+
5
+ context "With Eastern Standard Time" do
6
+ setup do
7
+ Time.zone = 'Eastern Time (US & Canada)'
8
+ end
9
+
10
+ should "know what a weekend day is" do
11
+ assert( Time.weekday?(Time.zone.parse("April 9, 2010 10:30am")))
12
+ assert(!Time.weekday?(Time.zone.parse("April 10, 2010 10:30am")))
13
+ assert(!Time.weekday?(Time.zone.parse("April 11, 2010 10:30am")))
14
+ assert( Time.weekday?(Time.zone.parse("April 12, 2010 10:30am")))
15
+ end
16
+
17
+ should "know a weekend day is not a workday" do
18
+ assert( Time.workday?(Time.zone.parse("April 9, 2010 10:45 am")))
19
+ assert(!Time.workday?(Time.zone.parse("April 10, 2010 10:45 am")))
20
+ assert(!Time.workday?(Time.zone.parse("April 11, 2010 10:45 am")))
21
+ assert( Time.workday?(Time.zone.parse("April 12, 2010 10:45 am")))
22
+ end
23
+
24
+ should "know a holiday is not a workday" do
25
+ BusinessTime::Config.reset
26
+
27
+ BusinessTime::Config.holidays << Date.parse("July 4, 2010")
28
+ BusinessTime::Config.holidays << Date.parse("July 5, 2010")
29
+
30
+ assert(!Time.workday?(Time.zone.parse("July 4th, 2010 1:15 pm")))
31
+ assert(!Time.workday?(Time.zone.parse("July 5th, 2010 2:37 pm")))
32
+ end
33
+
34
+
35
+ should "know the beginning of the day for an instance" do
36
+ first = Time.zone.parse("August 17th, 2010, 11:50 am")
37
+ expecting = Time.zone.parse("August 17th, 2010, 9:00 am")
38
+ assert_equal expecting, Time.beginning_of_workday(first)
39
+ end
40
+
41
+ should "know the end of the day for an instance" do
42
+ first = Time.zone.parse("August 17th, 2010, 11:50 am")
43
+ expecting = Time.zone.parse("August 17th, 2010, 5:00 pm")
44
+ assert_equal expecting, Time.end_of_workday(first)
45
+ end
46
+ end
47
+
48
+
49
+ context "With UTC Timezone" do
50
+ setup do
51
+ Time.zone = 'UTC'
52
+ end
53
+
54
+ should "know what a weekend day is" do
55
+ assert( Time.weekday?(Time.zone.parse("April 9, 2010 10:30am")))
56
+ assert(!Time.weekday?(Time.zone.parse("April 10, 2010 10:30am")))
57
+ assert(!Time.weekday?(Time.zone.parse("April 11, 2010 10:30am")))
58
+ assert( Time.weekday?(Time.zone.parse("April 12, 2010 10:30am")))
59
+ end
60
+
61
+ should "know a weekend day is not a workday" do
62
+ assert( Time.workday?(Time.zone.parse("April 9, 2010 10:45 am")))
63
+ assert(!Time.workday?(Time.zone.parse("April 10, 2010 10:45 am")))
64
+ assert(!Time.workday?(Time.zone.parse("April 11, 2010 10:45 am")))
65
+ assert( Time.workday?(Time.zone.parse("April 12, 2010 10:45 am")))
66
+ end
67
+
68
+ should "know a holiday is not a workday" do
69
+ BusinessTime::Config.reset
70
+
71
+ BusinessTime::Config.holidays << Date.parse("July 4, 2010")
72
+ BusinessTime::Config.holidays << Date.parse("July 5, 2010")
73
+
74
+ assert(!Time.workday?(Time.zone.parse("July 4th, 2010 1:15 pm")))
75
+ assert(!Time.workday?(Time.zone.parse("July 5th, 2010 2:37 pm")))
76
+ end
77
+
78
+
79
+ should "know the beginning of the day for an instance" do
80
+ first = Time.zone.parse("August 17th, 2010, 11:50 am")
81
+ expecting = Time.zone.parse("August 17th, 2010, 9:00 am")
82
+ assert_equal expecting, Time.beginning_of_workday(first)
83
+ end
84
+
85
+ should "know the end of the day for an instance" do
86
+ first = Time.zone.parse("August 17th, 2010, 11:50 am")
87
+ expecting = Time.zone.parse("August 17th, 2010, 5:00 pm")
88
+ assert_equal expecting, Time.end_of_workday(first)
89
+ end
90
+ end
91
+
92
+ end