Class: DatelessTime

Inherits:
Object
  • Object
show all
Defined in:
lib/dateless_time.rb,
lib/dateless_time/version.rb

Defined Under Namespace

Classes: InitializationError, TimeOutOfRangeError

Constant Summary collapse

SECONDS_IN_24_HOURS =
86400
MAX_HOURS =
24
MAX_MINUTES =
59
MAX_SECONDS =
59
TIME_STRING_REGEX =
/\A\d{1,2}(:\d{2})?(:\d{2})?( ?(am|pm))?\z/i
AM_PM_REGEX =
/( )?(am|pm)\z/i
SPRINTF_FORMAT =
"%02d:%02d:%02d".freeze
VERSION =
"0.0.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = Time.now) ⇒ DatelessTime

Returns a new instance of DatelessTime.



26
27
28
# File 'lib/dateless_time.rb', line 26

def initialize(source = Time.now)
  conditional_init source
end

Instance Attribute Details

#hoursObject (readonly)

Returns the value of attribute hours.



18
19
20
# File 'lib/dateless_time.rb', line 18

def hours
  @hours
end

#minutesObject (readonly)

Returns the value of attribute minutes.



18
19
20
# File 'lib/dateless_time.rb', line 18

def minutes
  @minutes
end

#secondsObject (readonly)

Returns the value of attribute seconds.



18
19
20
# File 'lib/dateless_time.rb', line 18

def seconds
  @seconds
end

Class Method Details

.nowObject



21
22
23
# File 'lib/dateless_time.rb', line 21

def self.now
  new
end

Instance Method Details

#seconds_since_midnightObject Also known as: to_i



57
58
59
# File 'lib/dateless_time.rb', line 57

def seconds_since_midnight
  @seconds_since_midnight ||= calculate_seconds_since_midnight
end

#strftime(template) ⇒ Object



64
65
66
67
68
# File 'lib/dateless_time.rb', line 64

def strftime(template)
  to_time.strftime(template)
rescue
  nil
end

#to_aObject



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

def to_a
  @array_value ||= [@hours, @minutes, @seconds]
end

#to_hObject



47
48
49
# File 'lib/dateless_time.rb', line 47

def to_h
  @hash_value ||= { hours: @hours, minutes: @minutes, seconds: @seconds }
end

#to_sObject



40
41
42
43
44
# File 'lib/dateless_time.rb', line 40

def to_s
  @string_value ||= sprintf(SPRINTF_FORMAT, @hours, @minutes, @seconds)
rescue
  nil
end

#to_time(base = Time.now) ⇒ Object



32
33
34
35
36
37
# File 'lib/dateless_time.rb', line 32

def to_time(base = Time.now)
  @time_value ||= Time.new(base.year, base.month, base.day,
                      @hours, @minutes, @seconds, base.utc_offset)
rescue
  nil
end