Class: DayTime
- Inherits:
-
Object
- Object
- DayTime
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/day_time.rb
Constant Summary collapse
- SECONDS_PER_DAY =
86_399- HM_REGEXP =
/^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])$/i- HMS_REGEXP =
/^(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])$/i
Instance Attribute Summary collapse
-
#seconds_since_midnight ⇒ Object
readonly
Returns the value of attribute seconds_since_midnight.
Instance Method Summary collapse
- #+(obj) ⇒ Object
- #-(obj) ⇒ Object
- #<=>(other) ⇒ Object
- #each ⇒ Object
- #hours ⇒ Object
-
#initialize(arg) ⇒ DayTime
constructor
A new instance of DayTime.
- #minutes ⇒ Object
- #seconds ⇒ Object
- #succ ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(arg) ⇒ DayTime
Returns a new instance of DayTime.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/day_time.rb', line 14 def initialize(arg) @seconds_since_midnight = case arg when self.class arg.seconds_since_midnight when Time seconds_from_time(arg) when DateTime seconds_from_time(arg.to_time) when Integer seconds_from_integer(arg) when String seconds_from_time_string(arg) when Hash seconds_from_hash(arg) else raise ArgumentError end end |
Instance Attribute Details
#seconds_since_midnight ⇒ Object (readonly)
Returns the value of attribute seconds_since_midnight.
12 13 14 |
# File 'lib/day_time.rb', line 12 def seconds_since_midnight @seconds_since_midnight end |
Instance Method Details
#+(obj) ⇒ Object
57 58 59 60 |
# File 'lib/day_time.rb', line 57 def +(obj) other = DayTime.new(obj) DayTime.new(@seconds_since_midnight + other.seconds_since_midnight) end |
#-(obj) ⇒ Object
62 63 64 65 |
# File 'lib/day_time.rb', line 62 def -(obj) other = DayTime.new(obj) DayTime.new((@seconds_since_midnight - other.seconds_since_midnight).abs) end |
#<=>(other) ⇒ Object
45 46 47 |
# File 'lib/day_time.rb', line 45 def <=>(other) seconds_since_midnight <=> other.seconds_since_midnight end |
#each ⇒ Object
53 54 55 |
# File 'lib/day_time.rb', line 53 def each 1.upto(Float::INFINITY) { |i| yield self + i } end |
#hours ⇒ Object
33 34 35 |
# File 'lib/day_time.rb', line 33 def hours @seconds_since_midnight / (60 ** 2) end |
#minutes ⇒ Object
37 38 39 |
# File 'lib/day_time.rb', line 37 def minutes (@seconds_since_midnight % 60 ** 2) / 60 end |
#seconds ⇒ Object
41 42 43 |
# File 'lib/day_time.rb', line 41 def seconds @seconds_since_midnight % 60 end |
#succ ⇒ Object
49 50 51 |
# File 'lib/day_time.rb', line 49 def succ self + 1 end |
#to_i ⇒ Object
71 72 73 |
# File 'lib/day_time.rb', line 71 def to_i @seconds_since_midnight end |
#to_s ⇒ Object
67 68 69 |
# File 'lib/day_time.rb', line 67 def to_s [hours, minutes, seconds].map { |int| int.to_s.rjust(2, "0") }.join(":") end |