Class: DateValues::TimeOfDay

Inherits:
Data
  • Object
show all
Includes:
Comparable
Defined in:
lib/date_values/time_of_day.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hour:, minute:, second: 0) ⇒ TimeOfDay

Returns a new instance of TimeOfDay.

Raises:

  • (ArgumentError)


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

#hourObject (readonly)

Returns the value of attribute hour

Returns:

  • (Object)

    the current value of hour



4
5
6
# File 'lib/date_values/time_of_day.rb', line 4

def hour
  @hour
end

#minuteObject (readonly)

Returns the value of attribute minute

Returns:

  • (Object)

    the current value of minute



4
5
6
# File 'lib/date_values/time_of_day.rb', line 4

def minute
  @minute
end

#secondObject (readonly)

Returns the value of attribute second

Returns:

  • (Object)

    the current value of 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

Raises:

  • (ArgumentError)


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_jsonObject



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

#inspectObject



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_sObject



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_secondsObject



52
53
54
# File 'lib/date_values/time_of_day.rb', line 52

def to_seconds
  hour * 3600 + minute * 60 + second
end