dateless_time 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -5
- data/dateless_time.gemspec +1 -1
- data/lib/dateless_time.rb +32 -3
- data/lib/dateless_time/version.rb +1 -1
- data/test/comparable_test.rb +12 -0
- data/test/creation_test.rb +42 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ac149024e32610ff56ae354bfad143ad37639d5
|
4
|
+
data.tar.gz: 941a7c524b054ecbd995c48c8b5f44a82c5da92a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a044edb3457d468247a6d8bf0919ebc71d3ef803b7a08344d7368262c37d7421c954fa0087e906537033108db0fb72038a5c034a65f8b18c131cd03c1e4bdf9
|
7
|
+
data.tar.gz: 56fc6415511cc95bef2d152e04d3f1f337a5d50a5af6551f076bda2fa27f1ae6750549c240be28932ae22e19d04b00b4544b7f2cfc2acdeaf599f7625ed4f471
|
data/README.md
CHANGED
@@ -20,14 +20,15 @@ As easy as pie.
|
|
20
20
|
|
21
21
|
## Dependencies
|
22
22
|
|
23
|
-
|
23
|
+
No external gems.
|
24
|
+
`date` from the stdlib.
|
24
25
|
|
25
26
|
|
26
27
|
## Rubies
|
27
28
|
|
28
29
|
Tested with:
|
29
30
|
|
30
|
-
* MRI `1.9.3`, `2.0.0`, `2.1.0` and `2.1.
|
31
|
+
* MRI `1.9.3`, `2.0.0`, `2.1.0`, `2.1.1` and `2.1.2`
|
31
32
|
* Rubinius `2.2.4`
|
32
33
|
* JRuby `1.7.10`
|
33
34
|
|
@@ -46,6 +47,9 @@ time = DatelessTime.now
|
|
46
47
|
# or a Time object
|
47
48
|
time = DatelessTime.new Time.now
|
48
49
|
|
50
|
+
# or a DateTime object
|
51
|
+
time = DatelessTime.new DateTime.now
|
52
|
+
|
49
53
|
# or a String (minutes and seconds are optional)
|
50
54
|
time = DatelessTime.new "13:37:00"
|
51
55
|
|
@@ -58,6 +62,9 @@ time = DatelessTime.new hours: 13, minutes: 37, seconds: 0
|
|
58
62
|
# or an Array (minutes and seconds are optional)
|
59
63
|
time = DatelessTime.new [13, 37, 0]
|
60
64
|
|
65
|
+
# or another DatelessTime object
|
66
|
+
time = DatelessTime.new DatelessTime.new('00:42')
|
67
|
+
|
61
68
|
```
|
62
69
|
|
63
70
|
Interface:
|
@@ -109,21 +116,26 @@ Comparisons:
|
|
109
116
|
```ruby
|
110
117
|
|
111
118
|
@time_1 = DatelessTime.new "11:22"
|
112
|
-
@
|
119
|
+
@time_2 = DatelessTime.new "11:30"
|
113
120
|
|
114
121
|
# all the usual suspects
|
115
122
|
|
116
123
|
@time_1 < @time_2
|
117
124
|
# => true
|
118
125
|
|
119
|
-
# all the usual suspects
|
120
126
|
@time_1 == @time_2
|
121
127
|
# => false
|
122
128
|
|
123
129
|
@time_2.between? @time_1, DatelessTime.new("20:30")
|
124
130
|
# => true
|
125
131
|
|
126
|
-
|
132
|
+
array = (1..5).map { DatelessTime.new(rand(DatelessTime::SECONDS_IN_24_HOURS)) }
|
133
|
+
array.map(&:to_s)
|
134
|
+
# => ["06:51:58", "04:50:32", "14:36:53", "21:36:38", "10:17:12"]
|
135
|
+
array.sort.map(&:to_s)
|
136
|
+
# => ["04:50:32", "06:51:58", "10:17:12", "14:36:53", "21:36:38"]
|
137
|
+
|
138
|
+
# etc...
|
127
139
|
|
128
140
|
```
|
129
141
|
|
data/dateless_time.gemspec
CHANGED
data/lib/dateless_time.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
require
|
1
|
+
require 'date'
|
2
|
+
require 'dateless_time/version'
|
2
3
|
|
3
4
|
class DatelessTime
|
4
5
|
|
@@ -83,11 +84,13 @@ private
|
|
83
84
|
def conditional_init(source)
|
84
85
|
case source
|
85
86
|
when Time then init_with_time(source)
|
87
|
+
when DateTime then init_with_datetime(source)
|
86
88
|
when String then init_with_string(source)
|
87
89
|
when Fixnum then init_with_seconds(source)
|
88
90
|
when Hash then init_with_hash(source)
|
89
91
|
when Array then init_with_array(source)
|
90
|
-
|
92
|
+
when DatelessTime then init_with_dateless_time(source)
|
93
|
+
else raise DatelessTime::InitializationError, "DatelessTime objects cannot be initialized with instances of #{source.class}."
|
91
94
|
end
|
92
95
|
end
|
93
96
|
|
@@ -100,6 +103,14 @@ private
|
|
100
103
|
end
|
101
104
|
|
102
105
|
|
106
|
+
def init_with_datetime(datetime)
|
107
|
+
@hours = datetime.hour
|
108
|
+
@minutes = datetime.min
|
109
|
+
@seconds = datetime.sec
|
110
|
+
seconds_since_midnight
|
111
|
+
end
|
112
|
+
|
113
|
+
|
103
114
|
def init_with_string(string)
|
104
115
|
validate_time_string string
|
105
116
|
data = time_string_to_array string
|
@@ -133,6 +144,9 @@ private
|
|
133
144
|
end
|
134
145
|
|
135
146
|
|
147
|
+
def init_with_dateless_time(source)
|
148
|
+
init_with_array([source.hours, source.minutes, source.seconds])
|
149
|
+
end
|
136
150
|
|
137
151
|
|
138
152
|
|
@@ -179,11 +193,26 @@ private
|
|
179
193
|
end
|
180
194
|
|
181
195
|
ary = str.split(":").map(&:to_i)
|
182
|
-
ary[0]
|
196
|
+
ary[0] = adjust_for_am_pm(ary[0], am_pm) if am_pm
|
183
197
|
ary
|
184
198
|
end
|
185
199
|
|
186
200
|
|
201
|
+
def adjust_for_am_pm(hour, am_pm)
|
202
|
+
if am_pm == 'am'
|
203
|
+
if hour == 12
|
204
|
+
hour = 0
|
205
|
+
end
|
206
|
+
elsif am_pm == 'pm'
|
207
|
+
if hour != 12
|
208
|
+
hour += 12
|
209
|
+
end
|
210
|
+
end
|
211
|
+
hour
|
212
|
+
end
|
213
|
+
|
214
|
+
|
215
|
+
|
187
216
|
def validate_time_array(ary)
|
188
217
|
if ary.empty?
|
189
218
|
raise DatelessTime::InitializationError
|
data/test/comparable_test.rb
CHANGED
@@ -107,4 +107,16 @@ class ComparableTest < Minitest::Test
|
|
107
107
|
refute @t3.between?(@t1, @t2)
|
108
108
|
end
|
109
109
|
|
110
|
+
|
111
|
+
def test_sort
|
112
|
+
@t1 = DatelessTime.new "20:45:10"
|
113
|
+
@t2 = DatelessTime.new "13:30:00"
|
114
|
+
@t3 = DatelessTime.new "9:20"
|
115
|
+
@t4 = DatelessTime.new "20:45:11"
|
116
|
+
@array = [@t1, @t2, @t3, @t4]
|
117
|
+
|
118
|
+
refute_equal @array, @array.sort
|
119
|
+
assert_equal [@t3, @t2, @t1, @t4], @array.sort
|
120
|
+
end
|
121
|
+
|
110
122
|
end
|
data/test/creation_test.rb
CHANGED
@@ -26,6 +26,17 @@ class CreationTest < Minitest::Test
|
|
26
26
|
end
|
27
27
|
|
28
28
|
|
29
|
+
def test_create_with_datetime
|
30
|
+
@datetime = DateTime.new 2014, 1, 1, 13, 37, 42
|
31
|
+
@dl_time = DatelessTime.new @datetime
|
32
|
+
|
33
|
+
assert_equal 13, @dl_time.hours
|
34
|
+
assert_equal 37, @dl_time.minutes
|
35
|
+
assert_equal 42, @dl_time.seconds
|
36
|
+
assert_equal (42 + (37 * 60) + (13 * 60 * 60)), @dl_time.to_i
|
37
|
+
end
|
38
|
+
|
39
|
+
|
29
40
|
|
30
41
|
def test_create_with_string
|
31
42
|
@dl_time = DatelessTime.new "12:13:14"
|
@@ -125,6 +136,26 @@ class CreationTest < Minitest::Test
|
|
125
136
|
end
|
126
137
|
end
|
127
138
|
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
def test_create_with_borderline_am
|
143
|
+
@dl_time = DatelessTime.new "12:13:14 am"
|
144
|
+
assert_equal 0, @dl_time.hours
|
145
|
+
assert_equal 13, @dl_time.minutes
|
146
|
+
assert_equal 14, @dl_time.seconds
|
147
|
+
assert_equal (14 + (13 * 60) + 0), @dl_time.to_i
|
148
|
+
end
|
149
|
+
|
150
|
+
|
151
|
+
def test_create_with_borderline_pm
|
152
|
+
@dl_time = DatelessTime.new "12:13:14 pm"
|
153
|
+
assert_equal 12, @dl_time.hours
|
154
|
+
assert_equal 13, @dl_time.minutes
|
155
|
+
assert_equal 14, @dl_time.seconds
|
156
|
+
assert_equal (14 + (13 * 60) + (12 * 60 * 60)), @dl_time.to_i
|
157
|
+
end
|
158
|
+
|
128
159
|
|
129
160
|
|
130
161
|
def test_create_with_seconds
|
@@ -290,6 +321,17 @@ class CreationTest < Minitest::Test
|
|
290
321
|
end
|
291
322
|
|
292
323
|
|
324
|
+
def test_create_with_other_dateless_time
|
325
|
+
@other = DatelessTime.now
|
326
|
+
@this = DatelessTime.new(@other)
|
327
|
+
|
328
|
+
assert_equal @other.hours, @this.hours
|
329
|
+
assert_equal @other.minutes, @this.minutes
|
330
|
+
assert_equal @other.seconds, @this.seconds
|
331
|
+
assert_equal @other.to_i, @this.to_i
|
332
|
+
end
|
333
|
+
|
334
|
+
|
293
335
|
|
294
336
|
def test_create_without_argument
|
295
337
|
@time = Time.now
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dateless_time
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tommaso Pavese
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 5.3
|
47
|
+
version: '5.3'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 5.3
|
54
|
+
version: '5.3'
|
55
55
|
description: A class to handle dateless time values. DatelessTime objects are a lightweight
|
56
56
|
alternative to Ruby's default Time class, and don't care about timezones and DST.
|
57
57
|
email:
|