class Unichron

Attributes

r[R]

Public Class Methods

new(obj, mode=nil, debug: false) click to toggle source
# File lib/unichron.rb, line 14
def initialize(obj, mode=nil, debug: false)

  @mode, @debug = mode, debug
  @r = select_method(obj)
  
end

Public Instance Methods

elapsed(limit=:day) click to toggle source

Returns the elapsed time in a humanized format e.g. elapsed(Time.now - 4000) #=> 1h note: An elapsed time more than a day is returned as a calendar date

# File lib/unichron.rb, line 25
def elapsed(limit=:day)
  
  elapsed = Time.now - @r

  relative_time = if elapsed < Time.now - (Date.today - 1).to_time then
    Subunit.seconds(elapsed).strfunit("%s")
  else
    @r.strftime("%b %d")
  end    
  
end
to_date() click to toggle source
# File lib/unichron.rb, line 41
def to_date
  @r.to_date if valid?
end
to_datetime() click to toggle source
# File lib/unichron.rb, line 45
def to_datetime
  @r.to_datetime if valid?
end
to_i() click to toggle source
# File lib/unichron.rb, line 49
def to_i()
  @r.to_time.to_i if valid?
end
to_seconds() click to toggle source
# File lib/unichron.rb, line 37
def to_seconds
  @r
end
to_time() click to toggle source
# File lib/unichron.rb, line 53
def to_time
  @r.to_time if valid?
end
valid?() click to toggle source
# File lib/unichron.rb, line 57
def valid?
  @r.respond_to? :to_time
end

Private Instance Methods

select_method(obj) click to toggle source
# File lib/unichron.rb, line 63
def select_method(obj)

  if obj.is_a? String then

    s = obj
    
    r = if @mode = :little_endian
      Chronic.parse(s, :endian_precedence => :little)
    else
      Chronic.parse(s)
    end
    
    puts 'r1: ' + r.inspect if @debug      
    return r if r
    
    r = ChronicCron.new s
    puts 'r2: ' + r.inspect if @debug
    return r if r.valid?

    r = ChronicDuration.parse s
    puts 'r3: ' + r.inspect if @debug
    return r if r
    
    r = ChristianCalendar.new.query(obj)
    puts 'r4 ' + r.inspect if @debug      
    return r if r

    
    return nil
    
  elsif obj.is_a? Integer
    r = Time.at obj
    return r
  else
    obj
  end
end