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.4"

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.



31
32
33
# File 'lib/dateless_time.rb', line 31

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

Instance Attribute Details

#hoursObject (readonly) Also known as: hour

Returns the value of attribute hours.



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

def hours
  @hours
end

#minutesObject (readonly) Also known as: min

Returns the value of attribute minutes.



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

def minutes
  @minutes
end

#secondsObject (readonly) Also known as: sec

Returns the value of attribute seconds.



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

def seconds
  @seconds
end

Class Method Details

.nowObject



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

def self.now
  new
end

Instance Method Details

#<=>(other) ⇒ Object

Raises:

  • (TypeError)


77
78
79
80
# File 'lib/dateless_time.rb', line 77

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

#seconds_since_midnightObject Also known as: to_i



63
64
65
# File 'lib/dateless_time.rb', line 63

def seconds_since_midnight
  @seconds_since_midnight ||= calculate_seconds_since_midnight
end

#strftime(template) ⇒ Object



70
71
72
73
74
# File 'lib/dateless_time.rb', line 70

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

#to_aObject



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

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

#to_hObject



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

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

#to_sObject



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

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

#to_time(base = Time.now) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/dateless_time.rb', line 37

def to_time(base = Time.now)
  args = [base.year, base.month, base.day, @hours, @minutes, @seconds]
  args << base.utc_offset if base.is_a?(Time)
  @time_value = Time.new(*args)
rescue
  nil
end