Class: TimeOfDay
Instance Attribute Summary collapse
-
#hour ⇒ Object
0 - 23.
-
#minute ⇒ Object
0 - 59.
-
#second ⇒ Object
0 - 59.
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=>(other) ⇒ Object
- #acts_like_time? ⇒ Boolean
- #encode_with(coder) ⇒ Object
- #in_time_zone ⇒ Object
- #init_with(coder) ⇒ Object
-
#initialize(hour, minute = 0, second = 0) ⇒ TimeOfDay
constructor
A new instance of TimeOfDay.
- #on(date) ⇒ Object
- #strftime(format) ⇒ Object
- #to_a ⇒ Object
- #to_json ⇒ Object
- #to_s(with_seconds = true) ⇒ Object
- #to_time ⇒ Object
Constructor Details
#initialize(hour, minute = 0, second = 0) ⇒ TimeOfDay
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/time_of_day.rb', line 12 def initialize(hour, minute = 0, second = 0) if hour == 24 raise "Invalid TimeOfDay. #{hour}:#{minute}:#{second} given, but highest allowed value is 24:00:00" unless minute == 0 && second == 0 else raise "Invalid hour: #{hour}" unless hour >= 0 && hour <= 23 end raise "Invalid minute: #{minute}" unless minute >= 0 && minute <= 59 raise "Invalid second: #{second}" unless second >= 0 && second <= 59 @hour = hour @minute = minute @second = second end |
Instance Attribute Details
#hour ⇒ Object
0 - 23
8 9 10 |
# File 'lib/time_of_day.rb', line 8 def hour @hour end |
#minute ⇒ Object
0 - 59
9 10 11 |
# File 'lib/time_of_day.rb', line 9 def minute @minute end |
#second ⇒ Object
0 - 59
10 11 12 |
# File 'lib/time_of_day.rb', line 10 def second @second end |
Class Method Details
._parse(string) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/time_of_day.rb', line 49 def self._parse(string) parts = parse_parts(string) return unless parts new(*parts) end |
.now ⇒ Object
36 37 38 |
# File 'lib/time_of_day.rb', line 36 def self.now Time.now.time_of_day # rubocop: disable Rails/TimeZone end |
.parse(string) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/time_of_day.rb', line 40 def self.parse(string) return nil if string.blank? tod = _parse(string) raise ArgumentError, "Illegal time format: '#{string}'" unless tod tod end |
.parse_parts(string) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/time_of_day.rb', line 56 def self.parse_parts(string) return nil if string.blank? return unless /^(?<hours>\d{1,2}):?(?<minutes>\d{2})?(?::(?<seconds>\d{1,2}))?$/ =~ string.strip [hours.to_i, minutes.to_i, seconds.to_i] end |
Instance Method Details
#+(other) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/time_of_day.rb', line 79 def +(other) raise "Illegal argument: #{other.inspect}" unless other.is_a? Numeric t = Time.local(0, 1, 1, hour, minute, second) # rubocop: disable Rails/TimeZone t += other self.class.new(t.hour, t.min, t.sec) end |
#-(other) ⇒ Object
87 88 89 90 91 |
# File 'lib/time_of_day.rb', line 87 def -(other) raise "Illegal argument: #{other.inspect}" unless other.is_a? Numeric self.+(-other) end |
#<=>(other) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/time_of_day.rb', line 93 def <=>(other) return -1 unless other other_tod = if other.is_a?(TimeOfDay) other elsif other.respond_to?(:time_of_day) other.time_of_day else other end to_a <=> [other_tod.hour, other_tod.minute, other_tod.second] end |
#acts_like_time? ⇒ Boolean
63 64 65 |
# File 'lib/time_of_day.rb', line 63 def acts_like_time? true end |
#encode_with(coder) ⇒ Object
31 32 33 34 |
# File 'lib/time_of_day.rb', line 31 def encode_with(coder) coder.tag = 'tag:yaml.org,2002:time' coder.scalar = to_s end |
#in_time_zone ⇒ Object
67 68 69 |
# File 'lib/time_of_day.rb', line 67 def in_time_zone(*) self end |
#init_with(coder) ⇒ Object
26 27 28 29 |
# File 'lib/time_of_day.rb', line 26 def init_with(coder) parts = self.class.parse_parts(coder.scalar) initialize(*parts) end |
#on(date) ⇒ Object
75 76 77 |
# File 'lib/time_of_day.rb', line 75 def on(date) Time.local(date.year, date.month, date.day, hour, minute, second) # rubocop: disable Rails/TimeZone end |
#strftime(format) ⇒ Object
107 108 109 |
# File 'lib/time_of_day.rb', line 107 def strftime(format) on(Date.today).strftime(format) end |
#to_a ⇒ Object
121 122 123 |
# File 'lib/time_of_day.rb', line 121 def to_a [@hour, @minute, @second] end |
#to_json ⇒ Object
125 126 127 |
# File 'lib/time_of_day.rb', line 125 def to_json(*) %("#{self}") end |
#to_s(with_seconds = true) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/time_of_day.rb', line 111 def to_s(with_seconds = true) if with_seconds '%02d:%02d:%02d' % to_a else '%02d:%02d' % [@hour, @minute] end rescue "#{@hour.inspect}:#{@minute.inspect}:#{@second.inspect}" end |
#to_time ⇒ Object
71 72 73 |
# File 'lib/time_of_day.rb', line 71 def to_time on Date.today end |