tod 1.2.2 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.markdown +35 -0
- data/lib/tod/shift.rb +41 -0
- data/lib/tod/version.rb +2 -2
- data/lib/tod.rb +1 -0
- data/test/tod/shift_test.rb +94 -0
- data/tod.gemspec +3 -3
- metadata +17 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 853fd23075cb8c0131b49bae9b571249cf01f940
|
4
|
+
data.tar.gz: 8fcb950ad2f427b980232962831d6ac53c25353a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 07c301bb3668574f5d1129d7fcaed0109c50e1a9e85ec663da88d2f863831c703b9f30160cd5078a4d8b7807cb875f8fdb54162551ff2facac491075d0eb445f
|
7
|
+
data.tar.gz: 49b0c82abd08efa118b80cd7c63b3d5c1c034a4e856ac79eea31c626034c868c9683ea825d6c4d6ab159331e19657d7a703eabc7d6aad1a286ac1796ec2e61d5
|
data/Gemfile.lock
CHANGED
data/README.markdown
CHANGED
@@ -4,6 +4,9 @@ Tod
|
|
4
4
|
Supplies TimeOfDay class that includes parsing, strftime, comparison, and
|
5
5
|
arithmetic.
|
6
6
|
|
7
|
+
Supplies Shift to represent a period of time, using a beginning and ending TimeOfDay. Allows to calculate its duration and
|
8
|
+
to determine if a TimeOfDay is included inside the shift. For nightly shifts (when beginning time is greater than ending time),
|
9
|
+
it supposes the shift ends the following day.
|
7
10
|
|
8
11
|
Installation
|
9
12
|
============
|
@@ -84,6 +87,34 @@ then require 'tod/time_of_day' instead of 'tod'.
|
|
84
87
|
Time.now.to_time_of_day # => 16:30:43
|
85
88
|
DateTime.now.to_time_of_day # => 16:30:43
|
86
89
|
|
90
|
+
|
91
|
+
Shifts
|
92
|
+
=======================
|
93
|
+
|
94
|
+
Represents a period of time, using a beginning and ending TimeOfDay. Allows to calculate its duration and
|
95
|
+
to determine if a TimeOfDay is included inside the shift. For nightly shifts (when beginning time is greater than ending time),
|
96
|
+
it supposes the shift ends the following day.
|
97
|
+
|
98
|
+
Creating from TimeOfDay
|
99
|
+
--------------------------------------
|
100
|
+
|
101
|
+
Shift.new(TimeOfDay.new(9), TimeOfDay.new(17))
|
102
|
+
Shift.new(TimeOfDay.new(22), TimeOfDay.new(4))
|
103
|
+
|
104
|
+
Duration
|
105
|
+
--------------------
|
106
|
+
|
107
|
+
Shift.new(TimeOfDay.new(9), TimeOfDay.new(17)).duration # => 28800
|
108
|
+
Shift.new(TimeOfDay.new(20), TimeOfDay.new(2)).duration # => 21600
|
109
|
+
|
110
|
+
Include?
|
111
|
+
--------------------
|
112
|
+
|
113
|
+
Shift.new(TimeOfDay.new(9), TimeOfDay.new(17)).include?(TimeOfDay.new(12)) # => true
|
114
|
+
Shift.new(TimeOfDay.new(9), TimeOfDay.new(17)).include?(TimeOfDay.new(7)) # => false
|
115
|
+
Shift.new(TimeOfDay.new(20), TimeOfDay.new(4)).include?(TimeOfDay.new(2)) # => true
|
116
|
+
Shift.new(TimeOfDay.new(20), TimeOfDay.new(4)).include?(TimeOfDay.new(18)) # => false
|
117
|
+
|
87
118
|
Rails Time Zone Support
|
88
119
|
=======================
|
89
120
|
|
@@ -113,6 +144,10 @@ Tod is compatible with Ruby 1.9.3 and 2.0.0 and Rails 3.0, 3.1, 3.2, and 4.0.
|
|
113
144
|
History
|
114
145
|
=======
|
115
146
|
|
147
|
+
## 1.3.0 (December 9, 2013)
|
148
|
+
|
149
|
+
* Add Shift class (Pablo Russo)
|
150
|
+
|
116
151
|
## 1.2.2 (November 16, 2013)
|
117
152
|
|
118
153
|
* Fix dumping nil or empty string to PostgreSQL time column (Maik Arnold)
|
data/lib/tod/shift.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Tod
|
2
|
+
class Shift
|
3
|
+
attr_reader :beginning, :ending
|
4
|
+
|
5
|
+
def initialize(beginning, ending)
|
6
|
+
raise ArgumentError, "beginning can not be nil" unless beginning
|
7
|
+
raise ArgumentError, "ending can not be nil" unless ending
|
8
|
+
|
9
|
+
@beginning = beginning
|
10
|
+
@ending = ending
|
11
|
+
freeze # Shift instances are value objects
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns true if the time of day is inside the shift (inclusive range), false otherwise
|
15
|
+
def include?(tod)
|
16
|
+
if ending >= beginning
|
17
|
+
tod >= beginning && tod <= ending
|
18
|
+
else
|
19
|
+
start_of_day = TimeOfDay.new(0,0,0)
|
20
|
+
end_of_day = TimeOfDay.new(23,59,59)
|
21
|
+
(tod >= beginning && tod <= end_of_day) || (tod >= start_of_day && tod <= ending)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Return shift duration in seconds.
|
26
|
+
# if ending is lower than beginning this method will calculate the duration as the ending time is from the following day
|
27
|
+
def duration
|
28
|
+
if ending >= beginning
|
29
|
+
(ending.to_i - beginning.to_i)
|
30
|
+
else
|
31
|
+
start_of_day = TimeOfDay.new(0,0,0)
|
32
|
+
end_of_day = TimeOfDay.new(23,59,59)
|
33
|
+
duration_day_1 = (end_of_day.to_i - beginning.to_i) + 1
|
34
|
+
duration_day_2 = (ending.to_i - start_of_day.to_i)
|
35
|
+
duration_day_1 + duration_day_2
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
Shift = Tod::Shift
|
data/lib/tod/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "1.
|
1
|
+
module Tod
|
2
|
+
VERSION = "1.3.0"
|
3
3
|
end
|
data/lib/tod.rb
CHANGED
@@ -0,0 +1,94 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__),'..','test_helper'))
|
2
|
+
require 'active_support/time'
|
3
|
+
|
4
|
+
class ShiftTest < Test::Unit::TestCase
|
5
|
+
context "duration" do
|
6
|
+
should "return correct duration when first time is lower than the second one" do
|
7
|
+
duration_expected = 4 * 60 * 60 + 30 * 60 + 30 # 4 hours, 30 min and 30 sec later
|
8
|
+
tod1 = TimeOfDay.new 8,30
|
9
|
+
tod2 = TimeOfDay.new 13,00,30
|
10
|
+
shift = Shift.new tod1, tod2
|
11
|
+
duration = shift.duration
|
12
|
+
assert_equal duration, duration_expected
|
13
|
+
end
|
14
|
+
should "return correct duration when first time is greater than the second one" do
|
15
|
+
duration_expected = 4 * 60 * 60 + 30 * 60 + 30 # 4 hours, 30 min and 30 sec later
|
16
|
+
tod1 = TimeOfDay.new 22,30
|
17
|
+
tod2 = TimeOfDay.new 3,00,30
|
18
|
+
shift = Shift.new tod1, tod2
|
19
|
+
duration = shift.duration
|
20
|
+
assert_equal duration, duration_expected
|
21
|
+
end
|
22
|
+
should "be zero when both times are equal" do
|
23
|
+
tod1 = TimeOfDay.new 3,00,30
|
24
|
+
shift = Shift.new tod1, tod1
|
25
|
+
duration = shift.duration
|
26
|
+
assert_equal duration, 0
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "include?" do
|
31
|
+
# |------------------------|--------T1----V----T2----|------------------------|
|
32
|
+
should "be true when value is between ToDs and boths tods are in the same day" do
|
33
|
+
tod1 = TimeOfDay.new 8
|
34
|
+
tod2 = TimeOfDay.new 16
|
35
|
+
value = TimeOfDay.new 12
|
36
|
+
shift = Shift.new tod1, tod2
|
37
|
+
assert_equal shift.include?(value), true
|
38
|
+
end
|
39
|
+
|
40
|
+
# |------------------T1----|-------V----------T2-----|------------------------|
|
41
|
+
should "be true when value is on second day between ToDs and start ToD is in a different day" do
|
42
|
+
tod1 = TimeOfDay.new 20
|
43
|
+
tod2 = TimeOfDay.new 15
|
44
|
+
value = TimeOfDay.new 12
|
45
|
+
shift = Shift.new tod1, tod2
|
46
|
+
assert_equal shift.include?(value), true
|
47
|
+
end
|
48
|
+
|
49
|
+
# |------------------T1--V-|------------------T2-----|------------------------|
|
50
|
+
should "be true when value is on first day between ToDs and start ToD is in a different day" do
|
51
|
+
tod1 = TimeOfDay.new 20
|
52
|
+
tod2 = TimeOfDay.new 15
|
53
|
+
value = TimeOfDay.new 22
|
54
|
+
shift = Shift.new tod1, tod2
|
55
|
+
assert_equal shift.include?(value), true
|
56
|
+
end
|
57
|
+
|
58
|
+
# |------------------------|--------T1----------V----|----T2------------------|
|
59
|
+
should "be true when value is on first day between ToDs and end ToD is in a different day" do
|
60
|
+
tod1 = TimeOfDay.new 16
|
61
|
+
tod2 = TimeOfDay.new 4
|
62
|
+
value = TimeOfDay.new 20
|
63
|
+
shift = Shift.new tod1, tod2
|
64
|
+
assert_equal shift.include?(value), true
|
65
|
+
end
|
66
|
+
|
67
|
+
# |------------------------|--------T1---------------|--V---T2----------------|
|
68
|
+
should "be true when value is on second day between ToDs and end ToD is in a different day" do
|
69
|
+
tod1 = TimeOfDay.new 16
|
70
|
+
tod2 = TimeOfDay.new 4
|
71
|
+
value = TimeOfDay.new 2
|
72
|
+
shift = Shift.new tod1, tod2
|
73
|
+
assert_equal shift.include?(value), true
|
74
|
+
end
|
75
|
+
|
76
|
+
# |------------------------|--------T1-----T2----V---|------------------------|
|
77
|
+
should "be false when value is after second ToD" do
|
78
|
+
tod1 = TimeOfDay.new 10
|
79
|
+
tod2 = TimeOfDay.new 16
|
80
|
+
value = TimeOfDay.new 20
|
81
|
+
shift = Shift.new tod1, tod2
|
82
|
+
assert_equal shift.include?(value), false
|
83
|
+
end
|
84
|
+
|
85
|
+
# |------------------------|--V-----T1-----T2--------|------------------------|
|
86
|
+
should "be false when value is before first ToD" do
|
87
|
+
tod1 = TimeOfDay.new 10
|
88
|
+
tod2 = TimeOfDay.new 16
|
89
|
+
value = TimeOfDay.new 8
|
90
|
+
shift = Shift.new tod1, tod2
|
91
|
+
assert_equal shift.include?(value), false
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
data/tod.gemspec
CHANGED
@@ -4,13 +4,13 @@ require "tod/version"
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "tod"
|
7
|
-
s.version =
|
7
|
+
s.version = Tod::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Jack Christensen"]
|
10
10
|
s.email = ["jack@jackchristensen.com"]
|
11
11
|
s.homepage = "https://github.com/JackC/tod"
|
12
|
-
s.summary = %q{Supplies TimeOfDay class}
|
13
|
-
s.description = %q{Supplies TimeOfDay class that includes parsing, strftime, comparison, and arithmetic.}
|
12
|
+
s.summary = %q{Supplies TimeOfDay and Shift class}
|
13
|
+
s.description = %q{Supplies TimeOfDay and Shift class that includes parsing, strftime, comparison, and arithmetic.}
|
14
14
|
|
15
15
|
s.add_development_dependency "test-unit"
|
16
16
|
s.add_development_dependency "shoulda"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tod
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jack Christensen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: test-unit
|
@@ -108,7 +108,7 @@ dependencies:
|
|
108
108
|
- - '>='
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
-
description: Supplies TimeOfDay class that includes parsing, strftime, comparison,
|
111
|
+
description: Supplies TimeOfDay and Shift class that includes parsing, strftime, comparison,
|
112
112
|
and arithmetic.
|
113
113
|
email:
|
114
114
|
- jack@jackchristensen.com
|
@@ -129,6 +129,7 @@ files:
|
|
129
129
|
- gemfiles/4.0.gemfile
|
130
130
|
- lib/tod.rb
|
131
131
|
- lib/tod/date.rb
|
132
|
+
- lib/tod/shift.rb
|
132
133
|
- lib/tod/time.rb
|
133
134
|
- lib/tod/time_of_day.rb
|
134
135
|
- lib/tod/version.rb
|
@@ -136,6 +137,7 @@ files:
|
|
136
137
|
- test/test_helper.rb
|
137
138
|
- test/tod/a_a_a_time_of_day_time_zone_without_active_support_test.rb
|
138
139
|
- test/tod/date_test.rb
|
140
|
+
- test/tod/shift_test.rb
|
139
141
|
- test/tod/time_of_day_serializable_attribute_test.rb
|
140
142
|
- test/tod/time_of_day_test.rb
|
141
143
|
- test/tod/time_of_day_time_zone_with_active_support_test.rb
|
@@ -163,5 +165,15 @@ rubyforge_project:
|
|
163
165
|
rubygems_version: 2.0.3
|
164
166
|
signing_key:
|
165
167
|
specification_version: 4
|
166
|
-
summary: Supplies TimeOfDay class
|
167
|
-
test_files:
|
168
|
+
summary: Supplies TimeOfDay and Shift class
|
169
|
+
test_files:
|
170
|
+
- test/support/active_record.rb
|
171
|
+
- test/test_helper.rb
|
172
|
+
- test/tod/a_a_a_time_of_day_time_zone_without_active_support_test.rb
|
173
|
+
- test/tod/date_test.rb
|
174
|
+
- test/tod/shift_test.rb
|
175
|
+
- test/tod/time_of_day_serializable_attribute_test.rb
|
176
|
+
- test/tod/time_of_day_test.rb
|
177
|
+
- test/tod/time_of_day_time_zone_with_active_support_test.rb
|
178
|
+
- test/tod/time_test.rb
|
179
|
+
has_rdoc:
|