Class: TimeOnly

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/time_only.rb,
lib/time_only/version.rb

Constant Summary collapse

SECONDS_PER_MIN =
60
SECONDS_PER_HOUR =
60 * SECONDS_PER_MIN
SECONDS_PER_DAY =
24 * SECONDS_PER_HOUR
VERSION =
'1.0.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TimeOnly

Public: Initialize a TimeOnly.

seconds - The Integer number of seconds since midnight.

OR

hours - The Integer number of hours. minutes - The Integer number of minutes. seconds - The Integer number of seconds.

Examples

TimeOnly.new(4)
# => '00:00:04'

TimeOnly.new(13, 24, 56)
# => '13:24:56'


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/time_only.rb', line 41

def initialize(*args)
  seconds = case args.size
    when 1
      args.first
    when 3
      hours, minutes, seconds = args

      raise ArgumentError, 'hours must be between 0 and 23'   if hours   < 0 || hours   > 23
      raise ArgumentError, 'minutes must be between 0 and 59' if minutes < 0 || minutes > 59
      raise ArgumentError, 'seconds must be between 0 and 59' if seconds < 0 || seconds > 59

      (hours * SECONDS_PER_HOUR) + (minutes * SECONDS_PER_MIN) + seconds
    else
      raise ArgumentError, "wrong number of arguments (#{args.size} for 1 or 3)"
    end

  @seconds_since_midnight = mod_by_day(seconds)
end

Class Method Details

.at(seconds) ⇒ Object

Public: Initialize a TimeOnly.

seconds - The Integer number of seconds since midnight.

Examples

TimeOnly.at(4)
# => '00:00:04'


16
17
18
# File 'lib/time_only.rb', line 16

def self.at(seconds)
  new(seconds)
end

.nowObject



20
21
22
23
24
# File 'lib/time_only.rb', line 20

def self.now
  time = Time.now

  new(time.hour, time.min, time.sec)
end

Instance Method Details

#+(seconds) ⇒ Object

Public: Add seconds to the time and return that as a new value. If the value exceeds the number of seconds in a day the time will roll forwardd.

seconds - The Integer number of seconds.

Examples

TimeOnly.new(4) + 3
# => '00:00:07'

TimeOnly.new(23, 59, 59) + 3
# => '00:00:02'

Returns a new TimeOnly.



74
75
76
# File 'lib/time_only.rb', line 74

def +(seconds)
  self.class.new(mod_by_day(@seconds_since_midnight + seconds))
end

#-(seconds) ⇒ Object

Public: Subtract seconds from the time and return that as a new value. If the value is less than zero seconds in a day the time will roll backwards.

seconds - The Integer number of seconds.

Examples

TimeOnly.new(4) - 3
# => '00:00:01'

TimeOnly.new(0, 0, 0) - 3
# => '23:59:57'

Returns a new TimeOnly.



92
93
94
# File 'lib/time_only.rb', line 92

def -(seconds)
  self + (seconds * -1)
end

#<=>(other) ⇒ Object



101
102
103
# File 'lib/time_only.rb', line 101

def <=>(other)
  to_i <=> other.to_i
end

#==(other) ⇒ Object Also known as: eql?



96
97
98
# File 'lib/time_only.rb', line 96

def ==(other)
  to_i == other.to_i
end

#am?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/time_only.rb', line 105

def am?
  hour < 12
end

#hourObject



109
110
111
# File 'lib/time_only.rb', line 109

def hour
  @hour ||= @seconds_since_midnight / SECONDS_PER_HOUR
end

#minObject



113
114
115
# File 'lib/time_only.rb', line 113

def min
  @min ||= (@seconds_since_midnight % SECONDS_PER_HOUR) / SECONDS_PER_MIN
end

#pm?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/time_only.rb', line 117

def pm?
  !am?
end

#secObject



121
122
123
# File 'lib/time_only.rb', line 121

def sec
  @sec ||= @seconds_since_midnight % SECONDS_PER_MIN
end

#strftime(format) ⇒ Object

Public: Formats time according to the directives in the given format string. The directives begins with a percent (%) character. Any text not listed as a directive will be passed through to the output string.)

The directive consists of a percent (%) character, zero or more flags and a conversion specifier as follows.

 %<flags><conversion>

Flags:
  - don't pad a numerical output

Directives:
  %H - Hour of the day, 24-hour clock, zero-padded (00..23)
  %k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
  %I - Hour of the day, 12-hour clock, zero-padded (01..12)
  %l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
  %P - Meridian indicator, lowercase (``am' or ``pm')
  %p - Meridian indicator, uppercase (``AM' or ``PM')
  %M - Minute of the hour (00..59)
  %S - Second of the minute (00..60)

Literal strings:
  %n - Newline character (\n)
  %t - Tab character (\t)
  %% - Literal ``%'' character)

Combinations:
  %X - Same as %T
  %r - 12-hour time (%I:%M:%S %p)
  %R - 24-hour time (%H:%M)
  %T - 24-hour time (%H:%M:%S)

format - The String containing directives.

Examples

TimeOnly.new(12, 34, 56).strftime('%r')
# => '12:34:56 PM'

TimeOnly.new(1, 3, 56).strftime('The time is %-l:%M:%S %P.')
# => 'The time is 1:03:56 pm.'

Returns the formatted String.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/time_only.rb', line 169

def strftime(format)
  format = format.dup

  format.gsub!(/%[rRTX]/) do |token|
    case token
    when '%r'
      '%I:%M:%S %p'
    when '%R'
      '%H:%M'
    when '%T', '%X'
      '%H:%M:%S'
    end
  end

  format.gsub(/%-?[HkIlPpMSnt%]/) do |token|
    case token
    when '%H'
      zero_pad(hour)
    when '%k'
      blank_pad(hour)
    when '%-H', '%-k'
      hour
    when '%I'
      zero_pad(twelve_hour)
    when '%l'
      blank_pad(twelve_hour)
    when '%-I', '%-l'
      twelve_hour
    when '%P'
      am? ? 'am' : 'pm'
    when '%p'
      am? ? 'AM' : 'PM'
    when '%M'
      zero_pad(min)
    when '%-M'
      min
    when '%S'
      zero_pad(sec)
    when '%-S'
      sec
    when '%n'
      "\n"
    when '%t'
      "\t"
    when '%%'
      '%'
    end
  end
end

#succObject



219
220
221
# File 'lib/time_only.rb', line 219

def succ
  self + 1
end

#to_aObject



223
224
225
# File 'lib/time_only.rb', line 223

def to_a
  [hour, min, sec]
end

#to_fObject



227
228
229
# File 'lib/time_only.rb', line 227

def to_f
  @seconds_since_midnight.to_f
end

#to_iObject Also known as: tv_sec



231
232
233
# File 'lib/time_only.rb', line 231

def to_i
  @seconds_since_midnight
end

#to_sObject Also known as: asctime, ctime, inspect



236
237
238
# File 'lib/time_only.rb', line 236

def to_s
  strftime('%T')
end