Class: DateValues::TimeOfDay
- Inherits:
-
Data
- Object
- Data
- DateValues::TimeOfDay
- Includes:
- Comparable
- Defined in:
- lib/date_values/time_of_day.rb
Instance Attribute Summary collapse
-
#hour ⇒ Object
readonly
Returns the value of attribute hour.
-
#minute ⇒ Object
readonly
Returns the value of attribute minute.
-
#second ⇒ Object
readonly
Returns the value of attribute second.
Class Method Summary collapse
Instance Method Summary collapse
- #+(seconds) ⇒ Object
- #-(other) ⇒ Object
- #<=>(other) ⇒ Object
- #advance(hours: 0, minutes: 0, seconds: 0) ⇒ Object
- #as_json ⇒ Object
- #change(hour: self.hour, minute: self.minute, second: self.second) ⇒ Object
-
#initialize(hour:, minute:, second: 0) ⇒ TimeOfDay
constructor
A new instance of TimeOfDay.
- #inspect ⇒ Object
- #strftime(format) ⇒ Object
- #to_s ⇒ Object
- #to_seconds ⇒ Object
Constructor Details
#initialize(hour:, minute:, second: 0) ⇒ TimeOfDay
Returns a new instance of TimeOfDay.
7 8 9 10 11 12 13 |
# File 'lib/date_values/time_of_day.rb', line 7 def initialize(hour:, minute:, second: 0) raise ArgumentError, "invalid hour: #{hour}" unless (0..23).include?(hour) raise ArgumentError, "invalid minute: #{minute}" unless (0..59).include?(minute) raise ArgumentError, "invalid second: #{second}" unless (0..59).include?(second) super end |
Instance Attribute Details
#hour ⇒ Object (readonly)
Returns the value of attribute hour
4 5 6 |
# File 'lib/date_values/time_of_day.rb', line 4 def hour @hour end |
#minute ⇒ Object (readonly)
Returns the value of attribute minute
4 5 6 |
# File 'lib/date_values/time_of_day.rb', line 4 def minute @minute end |
#second ⇒ Object (readonly)
Returns the value of attribute second
4 5 6 |
# File 'lib/date_values/time_of_day.rb', line 4 def second @second end |
Class Method Details
.from(time) ⇒ Object
15 16 17 |
# File 'lib/date_values/time_of_day.rb', line 15 def self.from(time) new(time.hour, time.min, time.sec) end |
.from_seconds(total) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/date_values/time_of_day.rb', line 56 def self.from_seconds(total) total = total % 86_400 h, rest = total.divmod(3600) m, s = rest.divmod(60) new(h, m, s) end |
.parse(str) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/date_values/time_of_day.rb', line 19 def self.parse(str) parts = str.split(':') raise ArgumentError, "invalid TimeOfDay: #{str}" unless [2, 3].include?(parts.size) new(*parts.map(&:to_i)) end |
Instance Method Details
#+(seconds) ⇒ Object
32 33 34 |
# File 'lib/date_values/time_of_day.rb', line 32 def +(seconds) self.class.from_seconds((to_seconds + seconds) % 86_400) end |
#-(other) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/date_values/time_of_day.rb', line 36 def -(other) case other when Integer then self + (-other) when TimeOfDay then to_seconds - other.to_seconds else raise TypeError, "#{other.class} can't be coerced into Integer or TimeOfDay" end end |
#<=>(other) ⇒ Object
26 27 28 29 30 |
# File 'lib/date_values/time_of_day.rb', line 26 def <=>(other) return nil unless other.is_a?(TimeOfDay) [hour, minute, second] <=> [other.hour, other.minute, other.second] end |
#advance(hours: 0, minutes: 0, seconds: 0) ⇒ Object
44 45 46 |
# File 'lib/date_values/time_of_day.rb', line 44 def advance(hours: 0, minutes: 0, seconds: 0) self + (hours * 3600 + minutes * 60 + seconds) end |
#as_json ⇒ Object
67 68 69 |
# File 'lib/date_values/time_of_day.rb', line 67 def as_json(*) to_s end |
#change(hour: self.hour, minute: self.minute, second: self.second) ⇒ Object
48 49 50 |
# File 'lib/date_values/time_of_day.rb', line 48 def change(hour: self.hour, minute: self.minute, second: self.second) self.class.new(hour, minute, second) end |
#inspect ⇒ Object
79 80 81 |
# File 'lib/date_values/time_of_day.rb', line 79 def inspect "#<DateValues::TimeOfDay #{self}>" end |
#strftime(format) ⇒ Object
63 64 65 |
# File 'lib/date_values/time_of_day.rb', line 63 def strftime(format) Time.new(2000, 1, 1, hour, minute, second).strftime(format) end |
#to_s ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/date_values/time_of_day.rb', line 71 def to_s if second.zero? format('%02d:%02d', hour, minute) else format('%02d:%02d:%02d', hour, minute, second) end end |
#to_seconds ⇒ Object
52 53 54 |
# File 'lib/date_values/time_of_day.rb', line 52 def to_seconds hour * 3600 + minute * 60 + second end |