Class: DatelessTime

Inherits:
Object
  • Object
show all
Includes:
Comparable
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.3"

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.



29
30
31
# File 'lib/dateless_time.rb', line 29

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

Instance Attribute Details

#hoursObject (readonly)

Returns the value of attribute hours.



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

def hours
  @hours
end

#minutesObject (readonly)

Returns the value of attribute minutes.



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

def minutes
  @minutes
end

#secondsObject (readonly)

Returns the value of attribute seconds.



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

def seconds
  @seconds
end

Class Method Details

.nowObject



24
25
26
# File 'lib/dateless_time.rb', line 24

def self.now
  new
end

Instance Method Details

#<=>(other) ⇒ Object

Raises:

  • (TypeError)


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

def <=>(other)
  raise TypeError unless other.is_a?(DatelessTime)
  to_i <=> other.to_i
end

#seconds_since_midnightObject Also known as: to_i



60
61
62
# File 'lib/dateless_time.rb', line 60

def seconds_since_midnight
  @seconds_since_midnight ||= calculate_seconds_since_midnight
end

#strftime(template) ⇒ Object



67
68
69
70
71
# File 'lib/dateless_time.rb', line 67

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

#to_aObject



55
56
57
# File 'lib/dateless_time.rb', line 55

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

#to_hObject



50
51
52
# File 'lib/dateless_time.rb', line 50

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

#to_sObject



43
44
45
46
47
# File 'lib/dateless_time.rb', line 43

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

#to_time(base = Time.now) ⇒ Object



35
36
37
38
39
40
# File 'lib/dateless_time.rb', line 35

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