class DatelessTime

Constants

AM_PM_REGEX
MAX_HOURS
MAX_MINUTES
MAX_SECONDS
SECONDS_IN_24_HOURS
SPRINTF_FORMAT
TIME_STRING_REGEX
VERSION

Attributes

hour[R]
hours[R]
min[R]
minutes[R]
sec[R]
seconds[R]

Public Class Methods

new(source = Time.now) click to toggle source
# File lib/dateless_time.rb, line 31
def initialize(source = Time.now)
  conditional_init source
end
now() click to toggle source
# File lib/dateless_time.rb, line 26
def self.now
  new
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/dateless_time.rb, line 102
def <=>(other)
  raise TypeError unless other.is_a?(DatelessTime)
  to_i <=> other.to_i
end
seconds_since_midnight() click to toggle source
# File lib/dateless_time.rb, line 88
def seconds_since_midnight
  @seconds_since_midnight ||= calculate_seconds_since_midnight
end
Also aliased as: to_i
strftime(template) click to toggle source
# File lib/dateless_time.rb, line 95
def strftime(template)
  to_time.strftime(template)
rescue
  nil
end
to_a() click to toggle source
# File lib/dateless_time.rb, line 83
def to_a
  @array_value ||= [@hours, @minutes, @seconds]
end
to_datetime(base = DateTime.now) click to toggle source
# 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_h() click to toggle source
# File lib/dateless_time.rb, line 78
def to_h
  @hash_value ||= { hours: @hours, minutes: @minutes, seconds: @seconds }
end
to_i()
to_s() click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

adjust_for_am_pm(hour, am_pm) click to toggle source
# File lib/dateless_time.rb, line 215
def adjust_for_am_pm(hour, am_pm)
  if am_pm == 'am'
    if hour == 12
      hour = 0
    end
  elsif am_pm == 'pm'
    if hour != 12
      hour += 12
    end
  end
  hour
end
calculate_hours_minutes_and_seconds() click to toggle source
# File lib/dateless_time.rb, line 180
def calculate_hours_minutes_and_seconds
  if @seconds_since_midnight
    cache, @seconds = @seconds_since_midnight.divmod(60)
    @hours, @minutes = cache.divmod(60)
  else
    nil
  end
end
calculate_seconds_since_midnight() click to toggle source
# File lib/dateless_time.rb, line 167
def calculate_seconds_since_midnight
  if @hours && @minutes && @seconds
    cache =  @seconds
    cache += @minutes * 60
    cache += @hours * 3600
    validate_seconds_since_midnight cache
    cache
  else
    nil
  end
end
conditional_init(source) click to toggle source
# File lib/dateless_time.rb, line 112
def conditional_init(source)
  case source
  when Time, DateTime, DatelessTime then init_with_time(source)
  when String then init_with_string(source)
  when Fixnum then init_with_seconds(source)
  when Hash   then init_with_hash(source)
  when Array  then init_with_array(source)
  else raise DatelessTime::InitializationError, "DatelessTime objects cannot be initialized with instances of #{source.class}."
  end
end
init_with_array(array) click to toggle source
# File lib/dateless_time.rb, line 157
def init_with_array(array)
  validate_time_array array
  @hours   = array[0]
  @minutes = array[1] || 0
  @seconds = array[2] || 0
  seconds_since_midnight
end
init_with_hash(hash) click to toggle source
# File lib/dateless_time.rb, line 148
def init_with_hash(hash)
  validate_time_hash hash
  @hours   = hash[:hours]
  @minutes = hash[:minutes] || 0
  @seconds = hash[:seconds] || 0
  seconds_since_midnight
end
init_with_seconds(seconds) click to toggle source
# File lib/dateless_time.rb, line 140
def init_with_seconds(seconds)
  validate_seconds_since_midnight seconds
  #seconds = SECONDS_IN_24_HOURS if seconds > SECONDS_IN_24_HOURS
  @seconds_since_midnight = seconds
  calculate_hours_minutes_and_seconds
end
init_with_string(string) click to toggle source
# File lib/dateless_time.rb, line 133
def init_with_string(string)
  validate_time_string string
  data = time_string_to_array string
  init_with_array data
end
init_with_time(time) click to toggle source
# File lib/dateless_time.rb, line 124
def init_with_time(time)
  @hours   = time.hour
  @minutes = time.min
  @seconds = time.sec
  seconds_since_midnight
end
time_string_to_array(str) click to toggle source

COLON = “:”.freeze EMPTY = “”.freeze PM = “pm”.freeze

# File lib/dateless_time.rb, line 201
def time_string_to_array(str)
  am_pm = false

  if AM_PM_REGEX =~ str
    am_pm = str[-2,2].downcase
    str.sub!(AM_PM_REGEX, '')
  end

  ary = str.split(":").map(&:to_i)
  ary[0] = adjust_for_am_pm(ary[0], am_pm) if am_pm
  ary
end
validate_seconds_since_midnight(seconds) click to toggle source
# File lib/dateless_time.rb, line 248
def validate_seconds_since_midnight(seconds)
  if seconds > SECONDS_IN_24_HOURS
    raise DatelessTime::TimeOutOfRangeError
  end
end
validate_time_array(ary) click to toggle source
# File lib/dateless_time.rb, line 230
def validate_time_array(ary)
  if ary.empty?
    raise DatelessTime::InitializationError
  elsif ary[0] > MAX_HOURS || (ary[1] && ary[1] > MAX_MINUTES) || (ary[2] && ary[2] > MAX_SECONDS)
    raise DatelessTime::TimeOutOfRangeError
  end
end
validate_time_hash(h) click to toggle source
# File lib/dateless_time.rb, line 239
def validate_time_hash(h)
  if h.empty? || h[:hours].nil?
    raise DatelessTime::InitializationError
  elsif h[:hours] > MAX_HOURS || (h[:minutes] && h[:minutes] > MAX_MINUTES) || (h[:seconds] && h[:seconds] > MAX_SECONDS)
    raise DatelessTime::TimeOutOfRangeError
  end
end
validate_time_string(str) click to toggle source
# File lib/dateless_time.rb, line 190
def validate_time_string(str)
  unless TIME_STRING_REGEX =~ str
    raise DatelessTime::InitializationError, "bad string format"
  end
end