timerizer 0.2.1 → 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.
- checksums.yaml +4 -4
- data/lib/timerizer.rb +70 -637
- data/lib/timerizer/core.rb +3 -0
- data/lib/timerizer/duration.rb +766 -0
- data/lib/timerizer/version.rb +3 -0
- data/lib/timerizer/wall_clock.rb +224 -0
- metadata +65 -5
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
module Timerizer
|
|
2
|
+
# Represents a time, but not a date. '7:00 PM' would be an example of a WallClock object
|
|
3
|
+
class WallClock
|
|
4
|
+
# Represents an error where an invalid meridiem was passed to WallClock.
|
|
5
|
+
# @see #new
|
|
6
|
+
class InvalidMeridiemError < ArgumentError; end
|
|
7
|
+
# Represents an error where a time beyond 24 hours was passed to WallClock.
|
|
8
|
+
# @see #new
|
|
9
|
+
class TimeOutOfBoundsError < ArgumentError; end
|
|
10
|
+
|
|
11
|
+
# Initialize a new instance of WallClock
|
|
12
|
+
# @overload new(hash)
|
|
13
|
+
# @param [Hash] units The units to initialize with
|
|
14
|
+
# @option units [Integer] :hour The hour to initialize with
|
|
15
|
+
# @option units [Integer] :minute The minute to initialize with
|
|
16
|
+
# @option units [Integer] :second The second to initialize with
|
|
17
|
+
# @overload new(hour, minute, meridiem)
|
|
18
|
+
# @param [Integer] hour The hour to initialize with
|
|
19
|
+
# @param [Integer] minute The minute to initialize with
|
|
20
|
+
# @param [Symbol] meridiem The meridiem to initialize with (`:am` or `:pm`)
|
|
21
|
+
# @overload new(hour, minute, second, meridiem)
|
|
22
|
+
# @param [Integer] hour The hour to initialize with
|
|
23
|
+
# @param [Integer] minute The minute to initialize with
|
|
24
|
+
# @param [Integer] second The second to initialize with
|
|
25
|
+
# @param [Symbol] meridiem The meridiem to initialize with (`:am` or `:pm`)
|
|
26
|
+
# @overload new(seconds)
|
|
27
|
+
# @param [Integer] seconds The number of seconds to initialize with (for use with #to_i)
|
|
28
|
+
# @raise InvalidMeridiemError Meridiem is not `:am` or `:pm`
|
|
29
|
+
def initialize(hour = nil, minute = nil, second = 0, meridiem = :am)
|
|
30
|
+
units = nil
|
|
31
|
+
if hour.is_a?(Integer) && minute.nil?
|
|
32
|
+
units = {second: hour}
|
|
33
|
+
elsif hour.is_a?(Hash)
|
|
34
|
+
units = hour
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
if !units.nil?
|
|
38
|
+
second = units[:second] || 0
|
|
39
|
+
minute = units[:minute] || 0
|
|
40
|
+
hour = units[:hour] || 0
|
|
41
|
+
else
|
|
42
|
+
if second.is_a?(String) || second.is_a?(Symbol)
|
|
43
|
+
meridiem = second
|
|
44
|
+
second = 0
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
meridiem = meridiem.downcase.to_sym
|
|
48
|
+
if !(meridiem == :am || meridiem == :pm)
|
|
49
|
+
raise InvalidMeridiemError
|
|
50
|
+
elsif meridiem == :pm && hour > 12
|
|
51
|
+
raise TimeOutOfBoundsError, "hour must be <= 12 for PM"
|
|
52
|
+
elsif hour >= 24 || minute >= 60 || second >= 60
|
|
53
|
+
raise TimeOutOfBoundsError
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
hour += 12 if (meridiem == :pm and !(hour == 12))
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
@seconds =
|
|
60
|
+
Duration.new(hour: hour, minute: minute, second: second).to_seconds
|
|
61
|
+
|
|
62
|
+
if @seconds >= 1.day.to_seconds
|
|
63
|
+
raise TimeOutOfBoundsError
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Takes a string and turns it into a WallClock time
|
|
68
|
+
# @param [String] string The string to convert
|
|
69
|
+
# @return [WallClock] The time as a WallClock
|
|
70
|
+
# @example
|
|
71
|
+
# WallClock.from_string("10:30 PM")
|
|
72
|
+
# # => 10:30:00 PM
|
|
73
|
+
# WallClock.from_string("13:01:23")
|
|
74
|
+
# # => 1:01:23 PM
|
|
75
|
+
# @see #to_s
|
|
76
|
+
def self.from_string(string)
|
|
77
|
+
time, meridiem = string.split(' ', 2)
|
|
78
|
+
hour, minute, second = time.split(':', 3)
|
|
79
|
+
WallClock.new(hour.to_i, minute.to_i, second.to_i || 0, meridiem || :am)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Returns the time of the WallClock on a date
|
|
83
|
+
# @param [Date] date The date to apply the time on
|
|
84
|
+
# @return [Time] The time after the given date
|
|
85
|
+
# @example yesterday at 5:00
|
|
86
|
+
# time = WallClock.new(5, 00, :pm)
|
|
87
|
+
# time.on(Date.yesterday)
|
|
88
|
+
# => 2000-1-1 17:00:00 -0800
|
|
89
|
+
def on(date)
|
|
90
|
+
date.to_date.to_time + @seconds
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Comparse two {WallClock}s.
|
|
94
|
+
# @return [Boolean] True if the WallClocks are identical
|
|
95
|
+
def ==(time)
|
|
96
|
+
if time.is_a? WallClock
|
|
97
|
+
self.in_seconds == time.in_seconds
|
|
98
|
+
else
|
|
99
|
+
false
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Get the time of the WallClock, in seconds
|
|
104
|
+
# @return [Integer] The total time of the WallClock, in seconds
|
|
105
|
+
def in_seconds
|
|
106
|
+
@seconds
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Get the time of the WallClock, in minutes
|
|
110
|
+
# @return [Integer] The total time of the WallClock, in minutes
|
|
111
|
+
def in_minutes
|
|
112
|
+
Duration::new(seconds: @seconds).to_minutes
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Get the time of the WallClock, in hours
|
|
116
|
+
# @return [Integer] The total time of the WallClock, in hours
|
|
117
|
+
def in_hours
|
|
118
|
+
Duration::new(seconds: @seconds).to_hours
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Get the second of the WallClock.
|
|
122
|
+
# @return [Integer] The second component of the WallClock
|
|
123
|
+
def second
|
|
124
|
+
self.to_duration.to_units(:hour, :minute, :second).fetch(:second)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Get the minute of the WallClock.
|
|
128
|
+
# @return [Integer] The minute component of the WallClock
|
|
129
|
+
def minute
|
|
130
|
+
self.to_duration.to_units(:hour, :minute, :second).fetch(:minute)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Get the hour of the WallClock.
|
|
134
|
+
# @param [Symbol] system The houring system to use (either `:twelve_hour` or `:twenty_four_hour`; default `:twenty_four_hour`)
|
|
135
|
+
# @return [Integer] The hour component of the WallClock
|
|
136
|
+
def hour(system = :twenty_four_hour)
|
|
137
|
+
hour = self.to_duration.to_units(:hour, :minute, :second).fetch(:hour)
|
|
138
|
+
if system == :twelve_hour
|
|
139
|
+
if hour == 0
|
|
140
|
+
12
|
|
141
|
+
elsif hour > 12
|
|
142
|
+
hour - 12
|
|
143
|
+
else
|
|
144
|
+
hour
|
|
145
|
+
end
|
|
146
|
+
elsif (system == :twenty_four_hour)
|
|
147
|
+
hour
|
|
148
|
+
else
|
|
149
|
+
raise ArgumentError, "system should be :twelve_hour or :twenty_four_hour"
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Get the meridiem of the WallClock.
|
|
154
|
+
# @return [Symbol] The meridiem (either `:am` or `:pm`)
|
|
155
|
+
def meridiem
|
|
156
|
+
if self.hour > 12 || self.hour == 0
|
|
157
|
+
:pm
|
|
158
|
+
else
|
|
159
|
+
:am
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Converts self to {WallClock}
|
|
164
|
+
# @see Time#to_wall
|
|
165
|
+
def to_wall
|
|
166
|
+
self
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Converts `self` to a {Duration}
|
|
170
|
+
# @return [Duration] `self` as a {Duration}
|
|
171
|
+
# @example
|
|
172
|
+
# time = WallClock.new(5, 30, :pm)
|
|
173
|
+
# time.to_duration
|
|
174
|
+
# => 5 hours, 30 minutes
|
|
175
|
+
def to_duration
|
|
176
|
+
@seconds.seconds
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
# Get the time of the WallClock in a more portable format (for a database, for example)
|
|
180
|
+
# @see #in_seconds
|
|
181
|
+
def to_i
|
|
182
|
+
self.in_seconds
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# Convert {WallClock} to a human-readable format.
|
|
186
|
+
# @param [Symbol] system The hour system to use (`:twelve_hour` or `:twenty_four_hour`; default `:twelve_hour`)
|
|
187
|
+
# @param [Hash] options Extra options for the string to use
|
|
188
|
+
# @option options [Boolean] :use_seconds Whether or not to include seconds in the conversion to a string
|
|
189
|
+
# @option options [Boolean] :include_meridian Whether or not to include the meridian for a twelve-hour time
|
|
190
|
+
# @example
|
|
191
|
+
# time = WallClock.new(5, 37, 41, :pm)
|
|
192
|
+
# time.to_s
|
|
193
|
+
# => "5:37:41 PM"
|
|
194
|
+
# time.to_s(:twenty_four_hour, :use_seconds => true)
|
|
195
|
+
# => "17:37:41"
|
|
196
|
+
# time.to_s(:twelve_hour, :use_seconds => false, :include_meridiem => false)
|
|
197
|
+
# => "5:37"
|
|
198
|
+
# time.to_s(:twenty_four_hour, :use_seconds =>false)
|
|
199
|
+
# => "17:37"
|
|
200
|
+
# @raise ArgumentError Argument isn't a proper system
|
|
201
|
+
def to_s(system = :twelve_hour, options = {})
|
|
202
|
+
options = {use_seconds: true, include_meridiem: true}.merge(options)
|
|
203
|
+
pad = "%02d"
|
|
204
|
+
meridiem = self.meridiem.to_s.upcase
|
|
205
|
+
hour = self.hour(system)
|
|
206
|
+
minute = pad % self.minute
|
|
207
|
+
second = pad % self.second
|
|
208
|
+
|
|
209
|
+
string = [hour, minute].join(':')
|
|
210
|
+
if options[:use_seconds]
|
|
211
|
+
string = [string, second].join(':')
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
case system
|
|
215
|
+
when :twelve_hour
|
|
216
|
+
options[:include_meridiem] ? [string, meridiem].join(' ') : string
|
|
217
|
+
when :twenty_four_hour
|
|
218
|
+
string
|
|
219
|
+
else
|
|
220
|
+
raise ArgumentError, "system should be :twelve_hour or :twenty_four_hour"
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
end
|
metadata
CHANGED
|
@@ -1,22 +1,83 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: timerizer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Kyle Lacy
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2017-
|
|
12
|
-
dependencies:
|
|
11
|
+
date: 2017-10-03 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.14'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.14'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: yard
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 0.9.9
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 0.9.9
|
|
13
69
|
description: A simple set of Rails-like time helpers
|
|
14
|
-
email:
|
|
70
|
+
email:
|
|
71
|
+
- kylelacy@me.com
|
|
15
72
|
executables: []
|
|
16
73
|
extensions: []
|
|
17
74
|
extra_rdoc_files: []
|
|
18
75
|
files:
|
|
19
76
|
- lib/timerizer.rb
|
|
77
|
+
- lib/timerizer/core.rb
|
|
78
|
+
- lib/timerizer/duration.rb
|
|
79
|
+
- lib/timerizer/version.rb
|
|
80
|
+
- lib/timerizer/wall_clock.rb
|
|
20
81
|
homepage: http://github.com/kylewlacy/timerizer
|
|
21
82
|
licenses:
|
|
22
83
|
- MIT
|
|
@@ -42,4 +103,3 @@ signing_key:
|
|
|
42
103
|
specification_version: 4
|
|
43
104
|
summary: Rails time helpers... without the Rails
|
|
44
105
|
test_files: []
|
|
45
|
-
has_rdoc:
|