Class Time
In: lib/core/facets/time/set.rb
lib/core/facets/time/change.rb
lib/core/facets/time/trunc.rb
lib/core/facets/time/advance.rb
lib/core/facets/time/to_time.rb
lib/core/facets/time/hence.rb
lib/core/facets/time/round.rb
lib/core/facets/time/stamp.rb
lib/core/facets/time/elapse.rb
Parent: Object

Methods

advance   ago   change   dst_adjustment   elapse   hence   round   set   stamp   stamp   to_time   trunc  

Constants

FORMAT = { :db => "%Y-%m-%d %H:%M:%S", :dbase => "%Y-%m-%d %H:%M:%S", :datbase => "%Y-%m-%d %H:%M:%S", :utc => "%Y-%m-%d %H:%M:%S", :number => "%Y%m%d%H%M%S", :short => "%d %b %H:%M", :time => "%H:%M", :long => "%B %d, %Y %H:%M", :day1st => "%d-%m-%Y %H:%M", :dmYHM => "%d-%m-%Y %H:%M", :rfc822 => "%a, %d %b %Y %H:%M:%S %z", nil => "%a %b %d %H:%M:%S %Z %Y"

Public Class methods

Tracks the elapse time of a code block.

  Time.elapse { sleep 1 }  #=> 0.999188899993896

CREDIT: Hal Fulton

[Source]

    # File lib/core/facets/time/elapse.rb, line 9
 9:   def self.elapse
10:     raise "Need block" unless block_given?
11:     t0 = now.to_f
12:     yield
13:     now.to_f - t0
14:   end

Produce time stamp for Time.now. See stamp.

CREDIT: Trans

[Source]

    # File lib/core/facets/time/stamp.rb, line 24
24:   def self.stamp(*args)
25:     now.stamp(*args)
26:   end

Public Instance methods

Uses Date to provide precise Time calculations for years, months, and days. The options parameter takes a hash with any of these keys: :years, :months, :weeks, :days, :hours, :minutes, :seconds.

CREDIT: ActiveSupport Team

[Source]

    # File lib/core/facets/time/advance.rb, line 10
10:   def advance(options)
11:     d = to_date.advance(options)
12:     time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
13:     seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
14:     seconds_to_advance == 0 ? time_advanced_by_date : time_advanced_by_date.since(seconds_to_advance)
15:   end

Returns a new Time representing the time a number of time-units ago.

[Source]

    # File lib/core/facets/time/hence.rb, line 15
15:     def ago(number, units=:seconds)
16:       return hence(-number, units) if number < 0
17: 
18:       time = (
19:         case units.to_s.downcase.to_sym
20:         when :years
21:           set(:year => (year - number))
22:         when :months
23:           new_month = ((month - number - 1) % 12) + 1
24:           y = (number / 12) + (new_month > month ? 1 : 0)
25:           set(:year => (year - y), :month => new_month)
26:         when :weeks
27:           self - (number * 604800)
28:         when :days
29:           self - (number * 86400)
30:         when :hours
31:           self - (number * 3600)
32:         when :minutes
33:           self - (number * 60)
34:         when :seconds, nil
35:           self - number
36:         else
37:           raise ArgumentError, "unrecognized time units -- #{units}"
38:         end
39:       )
40:       dst_adjustment(time)
41:     end

Returns a new Time where one or more of the elements have been changed according to the options parameter. The time options (hour, minute, sec, usec) reset cascadingly, so if only the hour is passed, then minute, sec, and usec is set to 0. If the hour and minute is passed, then sec and usec is set to 0.

 t = Time.now            #=> Sat Dec 01 14:10:15 -0500 2007
 t.change(:hour => 11)   #=> Sat Dec 01 11:00:00 -0500 2007

[Source]

    # File lib/core/facets/time/change.rb, line 13
13:   def change(options)
14:     opts=options; #{}; options.each_pair{ |k,v| opts[k] = v.to_i }
15:     self.class.send(
16:       self.utc? ? :utc : :local,
17:       opts[:year]  || self.year,
18:       opts[:month] || self.month,
19:       opts[:day]   || self.day,
20:       opts[:hour]  || self.hour,
21:       opts[:min]   || (opts[:hour] ? 0 : self.min),
22:       opts[:sec]   || ((opts[:hour] || opts[:min]) ? 0 : self.sec),
23:       opts[:usec]  || ((opts[:hour] || opts[:min] || opts[:sec]) ? 0 : self.usec)
24:     )
25:   end

Adjust DST

TODO: Can‘t seem to get this to pass ActiveSupport tests. Even though it is essentially identical to the ActiveSupport code (see Time#since in time/calculations.rb). It handles all but 4 tests.

[Source]

    # File lib/core/facets/time/hence.rb, line 83
83:     def dst_adjustment(time)
84:       self_dst = self.dst? ? 1 : 0
85:       time_dst = time.dst? ? 1 : 0
86:       seconds  = (self - time).abs
87:       if (seconds >= 86400 && self_dst != time_dst)
88:         time + ((self_dst - time_dst) * 60 * 60)
89:       else
90:         time
91:       end
92:     end

Returns a new Time representing the time a number of time-units hence.

[Source]

    # File lib/core/facets/time/hence.rb, line 46
46:     def hence(number, units=:seconds)
47:       return ago(-number, units) if number < 0
48: 
49:       time = (
50:         case units.to_s.downcase.to_sym
51:         when :years
52:           set( :year=>(year + number) )
53:         when :months
54:           new_month = ((month + number - 1) % 12) + 1
55:           y = (number / 12) + (new_month < month ? 1 : 0)
56:           set(:year => (year + y), :month => new_month)
57:         when :weeks
58:           self + (number * 604800)
59:         when :days
60:           self + (number * 86400)
61:         when :hours
62:           self + (number * 3600)
63:         when :minutes
64:           self + (number * 60)
65:         when :seconds
66:           self + number
67:         else
68:           raise ArgumentError, "unrecognized time units -- #{units}"
69:         end
70:       )
71:       dst_adjustment(time)
72:     end

Round time at the nearest range (in seconds).

  t = Time.now
  =>
  t.round(60*60) # 1 hour
  =>

[Source]

    # File lib/core/facets/time/round.rb, line 12
12:   def round(amount)
13:     (self+amount/2.0).trunc(amount)
14:   end

Like change but does not reset earlier times.

NOTE: It would be better, probably if this were called "change".

      and that #change were called "reset".

[Source]

    # File lib/core/facets/time/set.rb, line 8
 8:   def set(options)
 9:     opts={}; options.each_pair{ |k,v| opts[k] = v.to_i }
10:     self.class.send( self.utc? ? :utc : :local,
11:       opts[:year]  || self.year,
12:       opts[:month] || self.month,
13:       opts[:day]   || self.day,
14:       opts[:hour]  || self.hour,
15:       opts[:min]   || self.min,
16:       opts[:sec]   || self.sec,
17:       opts[:usec]  || self.usec
18:     )
19:   end

Create a time stamp.

  Time.now.stamp(:short)    #=> "01 Dec 15:15"

Supported formats come from the Time::FORMAT constant.

CREDIT: Trans

[Source]

    # File lib/core/facets/time/stamp.rb, line 36
36:   def stamp(fmt = nil)
37:     unless String === fmt
38:       fmt = FORMAT[fmt]
39:     end
40:     strftime(fmt).strip
41:   end

To be able to keep Dates and Times interchangeable on conversions.

[Source]

    # File lib/core/facets/time/to_time.rb, line 8
 8:     def to_time
 9:       getlocal 
10:     end

Truncate time at give range (in seconds).

  t = Time.now
  =>
  t.trunc(60*60) # 1 hour
  =>

[Source]

    # File lib/core/facets/time/trunc.rb, line 10
10:   def trunc(amount)
11:     self - (self.to_i % amount)
12:   end

[Validate]