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 =
"1.0.0"

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)


102
103
104
105
# File 'lib/dateless_time.rb', line 102

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

#seconds_since_midnightObject Also known as: to_i



88
89
90
# File 'lib/dateless_time.rb', line 88

def seconds_since_midnight
  @seconds_since_midnight ||= calculate_seconds_since_midnight
end

#strftime(template) ⇒ Object



95
96
97
98
99
# File 'lib/dateless_time.rb', line 95

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

#to_aObject



83
84
85
# File 'lib/dateless_time.rb', line 83

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

#to_datetime(base = DateTime.now) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dateless_time.rb', line 52

def to_datetime(base = DateTime.now)
  args = [base.year, base.month, base.day, @hours, @minutes, @seconds]

  normalized_offset = begin
    if base.is_a?(DateTime)
      base.offset * 24
    elsif base.is_a?(Time)
      base.utc_offset / 3600
    end
  end

  normalized_offset && args << sprintf("%+d", normalized_offset)

  DateTime.new(*args)
rescue
  nil
end

#to_hObject



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

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

#to_sObject



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

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
44
45
46
47
48
49
# File 'lib/dateless_time.rb', line 37

def to_time(base = Time.now)
  args = [base.year, base.month, base.day, @hours, @minutes, @seconds]

  if base.is_a?(Time)
    args << base.utc_offset
  elsif base.is_a?(DateTime)
    args << (base.offset * SECONDS_IN_24_HOURS)
  end

  Time.new(*args)
rescue
  nil
end