Class: Timerizer::WallClock
- Inherits:
-
Object
- Object
- Timerizer::WallClock
- Defined in:
- lib/timerizer/wall_clock.rb
Overview
Represents a time, but not a date. ‘7:00 PM’ would be an example of a WallClock object
Defined Under Namespace
Classes: InvalidMeridiemError, TimeOutOfBoundsError
Class Method Summary collapse
-
.from_string(string) ⇒ WallClock
Takes a string and turns it into a WallClock time.
Instance Method Summary collapse
-
#==(time) ⇒ Boolean
Comparse two WallClocks.
-
#hour(system = :twenty_four_hour) ⇒ Integer
Get the hour of the WallClock.
-
#in_hours ⇒ Integer
Get the time of the WallClock, in hours.
-
#in_minutes ⇒ Integer
Get the time of the WallClock, in minutes.
-
#in_seconds ⇒ Integer
Get the time of the WallClock, in seconds.
-
#initialize(hour = nil, minute = nil, second = 0, meridiem = :am) ⇒ WallClock
constructor
Initialize a new instance of WallClock.
-
#meridiem ⇒ Symbol
Get the meridiem of the WallClock.
-
#minute ⇒ Integer
Get the minute of the WallClock.
-
#on(date) ⇒ Time
Returns the time of the WallClock on a date.
-
#second ⇒ Integer
Get the second of the WallClock.
-
#to_duration ⇒ Duration
Converts ‘self` to a Duration.
-
#to_i ⇒ Object
Get the time of the WallClock in a more portable format (for a database, for example).
-
#to_s(system = :twelve_hour, options = {}) ⇒ Object
Convert WallClock to a human-readable format.
-
#to_wall ⇒ Object
Converts self to WallClock.
Constructor Details
#new(hash) ⇒ WallClock #new(hour, minute, meridiem) ⇒ WallClock #new(hour, minute, second, meridiem) ⇒ WallClock #new(seconds) ⇒ WallClock
Initialize a new instance of WallClock
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/timerizer/wall_clock.rb', line 29 def initialize(hour = nil, minute = nil, second = 0, meridiem = :am) units = nil if hour.is_a?(Integer) && minute.nil? units = {second: hour} elsif hour.is_a?(Hash) units = hour end if !units.nil? second = units[:second] || 0 minute = units[:minute] || 0 hour = units[:hour] || 0 else if second.is_a?(String) || second.is_a?(Symbol) meridiem = second second = 0 end meridiem = meridiem.downcase.to_sym if !(meridiem == :am || meridiem == :pm) raise InvalidMeridiemError elsif meridiem == :pm && hour > 12 raise TimeOutOfBoundsError, "hour must be <= 12 for PM" elsif hour >= 24 || minute >= 60 || second >= 60 raise TimeOutOfBoundsError end hour += 12 if (meridiem == :pm and !(hour == 12)) end @seconds = Duration.new(hour: hour, minute: minute, second: second).to_seconds if @seconds >= 1.day.to_seconds raise TimeOutOfBoundsError end end |
Class Method Details
.from_string(string) ⇒ WallClock
Takes a string and turns it into a WallClock time
76 77 78 79 80 |
# File 'lib/timerizer/wall_clock.rb', line 76 def self.from_string(string) time, meridiem = string.split(' ', 2) hour, minute, second = time.split(':', 3) WallClock.new(hour.to_i, minute.to_i, second.to_i || 0, meridiem || :am) end |
Instance Method Details
#==(time) ⇒ Boolean
Comparse two Timerizer::WallClocks.
95 96 97 98 99 100 101 |
# File 'lib/timerizer/wall_clock.rb', line 95 def ==(time) if time.is_a? WallClock self.in_seconds == time.in_seconds else false end end |
#hour(system = :twenty_four_hour) ⇒ Integer
Get the hour of the WallClock.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/timerizer/wall_clock.rb', line 136 def hour(system = :twenty_four_hour) hour = self.to_duration.to_units(:hour, :minute, :second).fetch(:hour) if system == :twelve_hour if hour == 0 12 elsif hour > 12 hour - 12 else hour end elsif (system == :twenty_four_hour) hour else raise ArgumentError, "system should be :twelve_hour or :twenty_four_hour" end end |
#in_hours ⇒ Integer
Get the time of the WallClock, in hours
117 118 119 |
# File 'lib/timerizer/wall_clock.rb', line 117 def in_hours Duration::new(seconds: @seconds).to_hours end |
#in_minutes ⇒ Integer
Get the time of the WallClock, in minutes
111 112 113 |
# File 'lib/timerizer/wall_clock.rb', line 111 def in_minutes Duration::new(seconds: @seconds).to_minutes end |
#in_seconds ⇒ Integer
Get the time of the WallClock, in seconds
105 106 107 |
# File 'lib/timerizer/wall_clock.rb', line 105 def in_seconds @seconds end |
#meridiem ⇒ Symbol
Get the meridiem of the WallClock.
155 156 157 158 159 160 161 |
# File 'lib/timerizer/wall_clock.rb', line 155 def meridiem if self.hour > 12 || self.hour == 0 :pm else :am end end |
#minute ⇒ Integer
Get the minute of the WallClock.
129 130 131 |
# File 'lib/timerizer/wall_clock.rb', line 129 def minute self.to_duration.to_units(:hour, :minute, :second).fetch(:minute) end |
#on(date) ⇒ Time
Returns the time of the WallClock on a date
89 90 91 |
# File 'lib/timerizer/wall_clock.rb', line 89 def on(date) date.to_date.to_time + @seconds end |
#second ⇒ Integer
Get the second of the WallClock.
123 124 125 |
# File 'lib/timerizer/wall_clock.rb', line 123 def second self.to_duration.to_units(:hour, :minute, :second).fetch(:second) end |
#to_duration ⇒ Duration
Converts ‘self` to a Duration
175 176 177 |
# File 'lib/timerizer/wall_clock.rb', line 175 def to_duration @seconds.seconds end |
#to_i ⇒ Object
Get the time of the WallClock in a more portable format (for a database, for example)
181 182 183 |
# File 'lib/timerizer/wall_clock.rb', line 181 def to_i self.in_seconds end |
#to_s(system = :twelve_hour, options = {}) ⇒ Object
Convert Timerizer::WallClock to a human-readable format.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/timerizer/wall_clock.rb', line 201 def to_s(system = :twelve_hour, = {}) = {use_seconds: true, include_meridiem: true}.merge() pad = "%02d" meridiem = self.meridiem.to_s.upcase hour = self.hour(system) minute = pad % self.minute second = pad % self.second string = [hour, minute].join(':') if [:use_seconds] string = [string, second].join(':') end case system when :twelve_hour [:include_meridiem] ? [string, meridiem].join(' ') : string when :twenty_four_hour string else raise ArgumentError, "system should be :twelve_hour or :twenty_four_hour" end end |
#to_wall ⇒ Object
Converts self to Timerizer::WallClock
165 166 167 |
# File 'lib/timerizer/wall_clock.rb', line 165 def to_wall self end |