Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/timerizer.rb

Overview

Time class monkeywrenched with RelativeTime support.

Defined Under Namespace

Classes: TimeIsInTheFutureError, TimeIsInThePastError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.between(time1, time2) ⇒ RelativeTime

Note:

The two times are interchangable; which comes first doesn’t matter

Calculate the amount of time between two times.

Examples:

Time.between(1.minute.ago, 1.hour.ago)
  => 59.minutes

Parameters:

  • time1 (Time)

    The initial time

  • time2 (Time)

    The final time

Returns:

  • (RelativeTime)

    Calculated time between time1 and time2

See Also:



709
710
711
712
713
# File 'lib/timerizer.rb', line 709

def self.between(time1, time2)
  time_between = (time2.to_time - time1.to_time).abs

  RelativeTime.new(time_between.round)
end

.classic_newObject



628
# File 'lib/timerizer.rb', line 628

alias_method :classic_new, :new

.new(*args) ⇒ Object



630
631
632
633
634
635
636
637
638
639
640
# File 'lib/timerizer.rb', line 630

def new(*args)
  begin
    Time.classic_new(*args)
  rescue ArgumentError
    if args.empty?
      Time.new
    else
      Time.local(*args)
    end
  end
end

.since(time) ⇒ RelativeTime

Calculates the time since a given time @raise The provided time is in the future

Examples:

Time.since(Time.new(2011, 10, 31))
  => 46 weeks, 5 days, 18 hours, 26 minutes, 10 seconds

Parameters:

  • time (Time)

    The time to calculate since now

Returns:

Raises:

See Also:



693
694
695
696
697
# File 'lib/timerizer.rb', line 693

def self.since(time)
  raise TimeIsInTheFutureError if time.to_time > Time.now

  Time.between(Time.now, time)
end

.until(time) ⇒ RelativeTime

Calculates the time until a given time @raise The provided time is in the past

Examples:

Time.until(Time.new(2012, 12, 25))
  => 13 weeks, 2 days, 6 hours, 31 minutes, 39 seconds

Parameters:

  • time (Time)

    The time until now to calculate

Returns:

Raises:

See Also:



678
679
680
681
682
# File 'lib/timerizer.rb', line 678

def self.until(time)
  raise TimeIsInThePastError if Time.now > time.to_time

  Time.between(Time.now, time)
end

Instance Method Details

#+(time) ⇒ Object



652
653
654
655
656
657
658
# File 'lib/timerizer.rb', line 652

def +(time)
  if time.is_a? RelativeTime
    time.after(self)
  else
    self.add(time)
  end
end

#-(time) ⇒ Object



661
662
663
664
665
666
667
# File 'lib/timerizer.rb', line 661

def -(time)
  if time.is_a? RelativeTime
    time.before(self)
  else
    self.subtract(time)
  end
end

#addObject

# else

# end

end



651
# File 'lib/timerizer.rb', line 651

alias_method :add, :+

#subtractObject



660
# File 'lib/timerizer.rb', line 660

alias_method :subtract, :-

#to_dateDate

Convert Time to Date.

Examples:

Time.new(2000, 1, 1, 12, 30).to_date
  => #<Date: 2000-01-01 ((2451545j,0s,0n),+0s,2299161j)>

Returns:



720
721
722
# File 'lib/timerizer.rb', line 720

def to_date
  Date.new(self.year, self.month, self.day)
end

#to_timeObject

Convert self to Time.

See Also:

  • Date#to_time


726
727
728
# File 'lib/timerizer.rb', line 726

def to_time
  self
end

#to_wallWallClock

Converts Time to WallClock

Examples:

time = Time.now.to_wall
Date.tomorrow.at(time)
  => 2000-1-2 13:13:27 -0800
  # "Same time tomorrow?"

Returns:



737
738
739
# File 'lib/timerizer.rb', line 737

def to_wall
  WallClock.new(self.hour, self.min, self.sec)
end