opening_hours 0.0.2 → 0.0.3
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.
- data/README.md +19 -3
- data/lib/opening_hours.rb +13 -17
- data/lib/opening_hours/version.rb +1 -1
- data/spec/opening_hours_spec.rb +38 -24
- metadata +1 -1
data/README.md
CHANGED
@@ -20,7 +20,7 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
Init new hours object:
|
22
22
|
```ruby
|
23
|
-
hours = OpeningHours.new("9:00 AM", "3:00 PM", "Berlin")
|
23
|
+
hours = OpeningHours.new("9:00 AM", "3:00 PM", "Europe/Berlin")
|
24
24
|
```
|
25
25
|
|
26
26
|
Methods to set the hours, closed times or holidays:
|
@@ -34,11 +34,11 @@ Calculate the deadline (next available working hour)
|
|
34
34
|
```ruby
|
35
35
|
# offset and time
|
36
36
|
hours.calculate_deadline(4*60*60, "Dec 23, 2010 8:00 PM")
|
37
|
-
|
37
|
+
=> "Fri, 24 Dec 2010 13:00:00 +0100"
|
38
38
|
|
39
39
|
# now with timezone
|
40
40
|
hours.calculate_deadline(0, "Dec 23, 2010 9:00 AM -0400")
|
41
|
-
|
41
|
+
=> "Thu, 23 Dec 2010 14:00:00 +0100"
|
42
42
|
```
|
43
43
|
|
44
44
|
Check if business is open right now:
|
@@ -46,6 +46,22 @@ Check if business is open right now:
|
|
46
46
|
hours.now_open?
|
47
47
|
```
|
48
48
|
|
49
|
+
And get all week days with values:
|
50
|
+
```ruby
|
51
|
+
hours.week.each do |day|
|
52
|
+
puts day[1].open
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
Get human readable opening/close hours
|
57
|
+
```ruby
|
58
|
+
hours.week[:mon].open.to_human_readable_hours
|
59
|
+
=> "09:00 AM"
|
60
|
+
|
61
|
+
hours.week[:mon].close.to_human_readable_hours
|
62
|
+
=> "03:00 PM"
|
63
|
+
```
|
64
|
+
|
49
65
|
## Contributing
|
50
66
|
|
51
67
|
1. Fork it
|
data/lib/opening_hours.rb
CHANGED
@@ -20,9 +20,9 @@ class OpeningHours
|
|
20
20
|
CLOSED = new(0, 0)
|
21
21
|
|
22
22
|
def self.parse(open, close)
|
23
|
-
open = Time.parse(open)
|
24
|
-
close = Time.parse(close)
|
25
|
-
|
23
|
+
open = Time.zone.parse(open)
|
24
|
+
close = Time.zone.parse(close)
|
25
|
+
|
26
26
|
open = TimeUtils::seconds_from_midnight(open)
|
27
27
|
close = TimeUtils::seconds_from_midnight(close)
|
28
28
|
|
@@ -58,10 +58,10 @@ class OpeningHours
|
|
58
58
|
|
59
59
|
WEEK_DAYS = Time::RFC2822_DAY_NAME.map { |m| m.downcase.to_sym }
|
60
60
|
|
61
|
-
def initialize(start_time, end_time, time_zone =
|
62
|
-
|
61
|
+
def initialize(start_time, end_time, time_zone = 'Europe/London')
|
62
|
+
Time.zone = time_zone
|
63
63
|
|
64
|
-
|
64
|
+
open_hours = OpenHours.parse(start_time, end_time)
|
65
65
|
|
66
66
|
@week = {}
|
67
67
|
WEEK_DAYS.each do |day|
|
@@ -71,10 +71,7 @@ class OpeningHours
|
|
71
71
|
@specific_days = {}
|
72
72
|
end
|
73
73
|
|
74
|
-
def update(day, start_time, end_time
|
75
|
-
|
76
|
-
@time_zone = time_zone
|
77
|
-
|
74
|
+
def update(day, start_time, end_time)
|
78
75
|
set_open_hours day, OpenHours.parse(start_time, end_time)
|
79
76
|
end
|
80
77
|
|
@@ -89,7 +86,6 @@ class OpeningHours
|
|
89
86
|
end
|
90
87
|
|
91
88
|
def calculate_deadline(job_duration, start_date_time)
|
92
|
-
Time.zone = @time_zone
|
93
89
|
start_date_time = Time.zone.parse(start_date_time)
|
94
90
|
|
95
91
|
today = Date.civil(start_date_time.year, start_date_time.month, start_date_time.day)
|
@@ -124,10 +120,10 @@ class OpeningHours
|
|
124
120
|
|
125
121
|
end
|
126
122
|
|
127
|
-
class
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
123
|
+
class Fixnum
|
124
|
+
def to_human_readable_hours
|
125
|
+
t = Time.local(Date.today.year, Date.today.month, Date.today.day)
|
126
|
+
t += self.seconds
|
127
|
+
t.strftime("%I:%M %p")
|
132
128
|
end
|
133
|
-
end
|
129
|
+
end
|
data/spec/opening_hours_spec.rb
CHANGED
@@ -14,30 +14,30 @@ describe OpeningHours do
|
|
14
14
|
it { should respond_to(:week) }
|
15
15
|
|
16
16
|
context "schedule without exceptions" do
|
17
|
-
before { @hours = OpeningHours.new("9:00 AM", "3:00 PM") }
|
17
|
+
before { @hours = OpeningHours.new("9:00 AM", "3:00 PM", "Europe/Berlin") }
|
18
18
|
|
19
19
|
it "should handle start time during open hours" do
|
20
|
-
@hours.calculate_deadline(1*60*60, "Jun 7, 2010 9:10 AM").should == Time.
|
20
|
+
@hours.calculate_deadline(1*60*60, "Jun 7, 2010 9:10 AM").should == Time.zone.parse("Jun 7, 2010 10:10 AM").to_formatted_s(:rfc822)
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should handle start time before open hours" do
|
24
|
-
@hours.calculate_deadline(2*60*60, "Jun 7, 2010 8:45 AM").should == Time.
|
24
|
+
@hours.calculate_deadline(2*60*60, "Jun 7, 2010 8:45 AM").should == Time.zone.parse("Jun 7, 2010 11:00 AM").to_formatted_s(:rfc822)
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should handle start time after open hours" do
|
28
|
-
@hours.calculate_deadline(2*60*60, "Jun 7, 2010 10:45 PM").should == Time.
|
28
|
+
@hours.calculate_deadline(2*60*60, "Jun 7, 2010 10:45 PM").should == Time.zone.parse("Jun 8, 2010 11:00 AM").to_formatted_s(:rfc822)
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should finish job next day if not enough time left" do
|
32
|
-
@hours.calculate_deadline(2*60*60, "Jun 7, 2010 2:45 PM").should == Time.
|
32
|
+
@hours.calculate_deadline(2*60*60, "Jun 7, 2010 2:45 PM").should == Time.zone.parse("Jun 8, 2010 10:45 AM").to_formatted_s(:rfc822)
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should process huge job for several days" do
|
36
|
-
@hours.calculate_deadline(20*60*60, "Jun 7, 2010 10:45 AM").should == Time.
|
36
|
+
@hours.calculate_deadline(20*60*60, "Jun 7, 2010 10:45 AM").should == Time.zone.parse("Jun 10, 2010 12:45 PM").to_formatted_s(:rfc822)
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should flip the edge" do
|
40
|
-
@hours.calculate_deadline(6*60*60, "Jun 7, 2010 9:00 AM").should == Time.
|
40
|
+
@hours.calculate_deadline(6*60*60, "Jun 7, 2010 9:00 AM").should == Time.zone.parse("Jun 8, 2010 9:00 AM").to_formatted_s(:rfc822)
|
41
41
|
end
|
42
42
|
|
43
43
|
# this is also possible, but I prefer previous variant
|
@@ -49,19 +49,19 @@ describe OpeningHours do
|
|
49
49
|
context "on dst changes" do
|
50
50
|
|
51
51
|
it "should respect changing to dst" do
|
52
|
-
@hours.calculate_deadline(8*60*60, "March 27, 2010 2:00 PM").should == Time.
|
52
|
+
@hours.calculate_deadline(8*60*60, "March 27, 2010 2:00 PM").should == Time.zone.parse("March 29, 2010 10:00 AM").to_formatted_s(:rfc822)
|
53
53
|
end
|
54
54
|
|
55
55
|
it "should respect changing to dst" do
|
56
|
-
@hours.calculate_deadline(2*60*60, "March 27, 2010 2:00 PM").should == Time.
|
56
|
+
@hours.calculate_deadline(2*60*60, "March 27, 2010 2:00 PM").should == Time.zone.parse("March 28, 2010 10:00 AM").to_formatted_s(:rfc822)
|
57
57
|
end
|
58
58
|
|
59
59
|
it "should respect changing from dst" do
|
60
|
-
@hours.calculate_deadline(8*60*60, "October 31, 2010 2:00 PM").should == Time.
|
60
|
+
@hours.calculate_deadline(8*60*60, "October 31, 2010 2:00 PM").should == Time.zone.parse("November 2, 2010 10:00 AM").to_formatted_s(:rfc822)
|
61
61
|
end
|
62
62
|
|
63
63
|
it "should respect changing from dst" do
|
64
|
-
@hours.calculate_deadline(2*60*60, "October 31, 2010 2:00 PM").should == Time.
|
64
|
+
@hours.calculate_deadline(2*60*60, "October 31, 2010 2:00 PM").should == Time.zone.parse("November 1, 2010 10:00 AM").to_formatted_s(:rfc822)
|
65
65
|
end
|
66
66
|
|
67
67
|
end
|
@@ -75,11 +75,11 @@ describe OpeningHours do
|
|
75
75
|
end
|
76
76
|
|
77
77
|
it "should skip closed days" do
|
78
|
-
@hours.calculate_deadline(2*60*60, "Jun 5, 2010 2:45 PM").should == Time.
|
78
|
+
@hours.calculate_deadline(2*60*60, "Jun 5, 2010 2:45 PM").should == Time.zone.parse("Jun 7, 2010 10:45 AM").to_formatted_s(:rfc822)
|
79
79
|
end
|
80
80
|
|
81
81
|
it "should skip closed days even if work scheduled to closed day" do
|
82
|
-
@hours.calculate_deadline(2*60*60, "Jun 6, 2010 11:45 AM").should == Time.
|
82
|
+
@hours.calculate_deadline(2*60*60, "Jun 6, 2010 11:45 AM").should == Time.zone.parse("Jun 7, 2010 11:00 AM").to_formatted_s(:rfc822)
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
@@ -91,11 +91,11 @@ describe OpeningHours do
|
|
91
91
|
end
|
92
92
|
|
93
93
|
it "should skip closed days" do
|
94
|
-
@hours.calculate_deadline(2*60*60, "Dec 24, 2010 2:45 PM").should == Time.
|
94
|
+
@hours.calculate_deadline(2*60*60, "Dec 24, 2010 2:45 PM").should == Time.zone.parse("Dec 26, 2010 10:45 AM").to_formatted_s(:rfc822)
|
95
95
|
end
|
96
96
|
|
97
97
|
it "should skip closed days even if work scheduled to closed day" do
|
98
|
-
@hours.calculate_deadline(2*60*60, "Dec 25, 2010 11:45 AM").should == Time.
|
98
|
+
@hours.calculate_deadline(2*60*60, "Dec 25, 2010 11:45 AM").should == Time.zone.parse("Dec 26, 2010 11:00 AM").to_formatted_s(:rfc822)
|
99
99
|
end
|
100
100
|
|
101
101
|
end
|
@@ -108,7 +108,7 @@ describe OpeningHours do
|
|
108
108
|
end
|
109
109
|
|
110
110
|
it "should skip closed days" do
|
111
|
-
@hours.calculate_deadline(2*60*60, "Dec 24, 2010 2:45 PM").should == Time.
|
111
|
+
@hours.calculate_deadline(2*60*60, "Dec 24, 2010 2:45 PM").should == Time.zone.parse("Dec 27, 2010 10:45 AM").to_formatted_s(:rfc822)
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
@@ -119,7 +119,7 @@ describe OpeningHours do
|
|
119
119
|
end
|
120
120
|
|
121
121
|
it "should spend open hours" do
|
122
|
-
@hours.calculate_deadline(14*60*60, "Jun 3, 2010 9:00 AM").should == Time.
|
122
|
+
@hours.calculate_deadline(14*60*60, "Jun 3, 2010 9:00 AM").should == Time.zone.parse("Jun 5, 2010 10:00 AM").to_formatted_s(:rfc822)
|
123
123
|
end
|
124
124
|
|
125
125
|
end
|
@@ -131,11 +131,11 @@ describe OpeningHours do
|
|
131
131
|
end
|
132
132
|
|
133
133
|
it "should spend open hours" do
|
134
|
-
@hours.calculate_deadline(12*60*60, "Dec 23, 2010 9:00 AM").should == Time.
|
134
|
+
@hours.calculate_deadline(12*60*60, "Dec 23, 2010 9:00 AM").should == Time.zone.parse("Dec 25, 2010 10:00 AM").to_formatted_s(:rfc822)
|
135
135
|
end
|
136
136
|
|
137
137
|
it "should spend open hours if started at the day" do
|
138
|
-
@hours.calculate_deadline(6*60*60, "Dec 24, 2010 12:00 PM").should == Time.
|
138
|
+
@hours.calculate_deadline(6*60*60, "Dec 24, 2010 12:00 PM").should == Time.zone.parse("Dec 25, 2010 2:00 PM").to_formatted_s(:rfc822)
|
139
139
|
end
|
140
140
|
|
141
141
|
end
|
@@ -150,29 +150,32 @@ describe OpeningHours do
|
|
150
150
|
end
|
151
151
|
|
152
152
|
it "should pass test #1" do
|
153
|
-
@hours.calculate_deadline(2*60*60, "Jun 7, 2010 9:10 AM").should == Time.
|
153
|
+
@hours.calculate_deadline(2*60*60, "Jun 7, 2010 9:10 AM").should == Time.zone.parse("Mon Jun 07 11:10:00 2010").to_formatted_s(:rfc822)
|
154
154
|
end
|
155
155
|
|
156
156
|
it "should pass test #2" do
|
157
|
-
@hours.calculate_deadline(15*60, "Jun 8, 2010 2:48 PM").should == Time.
|
157
|
+
@hours.calculate_deadline(15*60, "Jun 8, 2010 2:48 PM").should == Time.zone.parse("Thu Jun 10 09:03:00 2010").to_formatted_s(:rfc822)
|
158
158
|
end
|
159
159
|
|
160
160
|
it "should pass test #3" do
|
161
|
-
@hours.calculate_deadline(7*60*60, "Dec 24, 2010 6:45 AM").should == Time.
|
161
|
+
@hours.calculate_deadline(7*60*60, "Dec 24, 2010 6:45 AM").should == Time.zone.parse("Mon Dec 27 11:00:00 2010").to_formatted_s(:rfc822)
|
162
162
|
end
|
163
163
|
|
164
164
|
end
|
165
165
|
|
166
166
|
context "timezone checks" do
|
167
167
|
before do
|
168
|
-
|
169
|
-
@hours = OpeningHours.new("9:00 AM", "3:00 PM", "Moscow")
|
168
|
+
@hours = OpeningHours.new("9:00 AM", "3:00 PM", "Europe/Moscow")
|
170
169
|
end
|
171
170
|
|
172
171
|
it "should spend open hours in the right time zone" do
|
173
172
|
@hours.calculate_deadline(2*60*60, "Dec 23, 2010 9:00 AM").should == Time.zone.parse("Dec 23, 2010 11:00 AM").to_formatted_s(:rfc822)
|
174
173
|
end
|
175
174
|
|
175
|
+
it "should spend open hours in the right time zone" do
|
176
|
+
@hours.calculate_deadline(2*60*60, "Dec 23, 2010 2:00 PM").should == Time.zone.parse("Dec 24, 2010 10:00 AM").to_formatted_s(:rfc822)
|
177
|
+
end
|
178
|
+
|
176
179
|
it "should spend open hours in the right time zone" do
|
177
180
|
@hours.calculate_deadline(4*60*60, "Dec 23, 2010 8:00 PM -0900").should == Time.zone.parse("Dec 24, 2010 01:00 PM").to_formatted_s(:rfc822)
|
178
181
|
end
|
@@ -183,4 +186,15 @@ describe OpeningHours do
|
|
183
186
|
|
184
187
|
end
|
185
188
|
|
189
|
+
context "check accessors" do
|
190
|
+
before do
|
191
|
+
@hours = OpeningHours.new("9:00 AM", "3:00 PM", "Europe/Moscow")
|
192
|
+
end
|
193
|
+
|
194
|
+
it "should respond to human readable times" do
|
195
|
+
@hours.week[:mon].open.to_human_readable_hours == "09:00 AM"
|
196
|
+
@hours.week[:mon].close.to_human_readable_hours == "03:00 PM"
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
186
200
|
end
|