class Time

Author: Stefano Harding <riddopic@gmail.com> License: Apache License, Version 2.0 Copyright: © 2014-2015 Stefano Harding

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Constants

FORMAT

Public Class Methods

elapse() { || ... } click to toggle source

Tracks the elapse time of a code block.

@example

e = Time.elapse { sleep 1 }
e.assert > 1
# File lib/garcon/core_ext/time.rb, line 27
def self.elapse
  raise "you need to pass a block" unless block_given?
  t0 = now.to_f
  yield
  now.to_f - t0
end
stamp(*args) click to toggle source

Produce time stamp for Time.now. See stamp.

# File lib/garcon/core_ext/time.rb, line 64
def self.stamp(*args)
  now.stamp(*args)
end
time() click to toggle source

Return a float of time since linux epoch

@example

Time.time     -> 1295953427.0005338

@return [Float]

# File lib/garcon/core_ext/time.rb, line 40
def self.time
  now.to_f
end

Public Instance Methods

ago(*time_units)

Alias for less

Alias for: less
dst_adjustment(time) click to toggle source

Adjust DST

# File lib/garcon/core_ext/time.rb, line 86
def dst_adjustment(time)
  self_dst = self.dst? ? 1 : 0
  time_dst = time.dst? ? 1 : 0
  seconds  = (self - time).abs
  if (seconds >= 86400 && self_dst != time_dst)
    time + ((self_dst - time_dst) * 60 * 60)
  else
    time
  end
end
hence(*time_units)

Alias for shift.

Alias for: shift
in(*time_units)

Alias for shift.

Alias for: shift
less(*time_units) click to toggle source

Returns a new Time representing the time a number of time-units ago. This is just like shift, but reverses the direction.

@example

t = Time.utc(2010,10,10,0,0,0)
t.less(4, :days)             # =>  Time.utc(2010,10,6,0,0,0)
# File lib/garcon/core_ext/time.rb, line 188
def less(*time_units)
  time_hash  = Hash===time_units.last ? time_units.pop : {}
  time_units = time_units.flatten

  time_units << :seconds if time_units.size % 2 == 1

  time_hash.each{ |units, number| time_units << number; time_units << units }

  neg_times = []
  time_units.each_slice(2){ |number, units| neg_times << -number; neg_times << units }

  shift(*neg_times)
end
Also aliased as: ago
set(options) click to toggle source

Like change but does not reset earlier times.

# File lib/garcon/core_ext/time.rb, line 100
def set(options)
  opts={}
  options.each_pair do |k,v|
    k = :min if k.to_s =~ /^min/
    k = :sec if k.to_s =~ /^sec/
    opts[k] = v.to_i
  end
  self.class.send(
    self.utc? ? :utc : :local,
    opts[:year]  || self.year,
    opts[:month] || self.month,
    opts[:day]   || self.day,
    opts[:hour]  || self.hour,
    opts[:min]   || self.min,
    opts[:sec]   || self.sec,
    opts[:usec]  || self.usec
  )
end
shift(*time_units) click to toggle source

Returns a new Time representing the time shifted by the time-units given. Positive number shift the time forward, negative number shift the time backward.

@example

t = Time.utc(2010,10,10,0,0,0)
t.shift( 4, :days)            # =>  Time.utc(2010,10,14,0,0,0)
t.shift(-4, :days)            # =>  Time.utc(2010,10,6,0,0,0)

More than one unit of time can be given.

t.shift(4, :days, 3, :hours)  # =>  Time.utc(2010,10,14,3,0,0)

The shift method can also take a hash.

t.shift(:days=>4, :hours=>3)  # =>  Time.utc(2010,10,14,3,0,0)
# File lib/garcon/core_ext/time.rb, line 134
def shift(*time_units)
  time_hash   = Hash===time_units.last ? time_units.pop : {}
  time_units  = time_units.flatten
  time_units << :seconds if time_units.size % 2 == 1
  time_hash.each { |units, number| time_units << number; time_units << units }

  time = self
  time_units.each_slice(2) do |number, units|
    time = (
      case units.to_s.downcase.to_sym
      when :years, :year
        time.set( :year=>(year + number) )
      when :months, :month
        if number > 0
          new_month = ((month + number - 1) % 12) + 1
          y = (number / 12) + (new_month < month ? 1 : 0)
          time.set(:year => (year + y), :month => new_month)
        else
          number = -number
          new_month = ((month - number - 1) % 12) + 1
          y = (number / 12) + (new_month > month ? 1 : 0)
          time.set(:year => (year - y), :month => new_month)
        end
      when :weeks, :week
        time + (number * 604800)
      when :days, :day
        time + (number * 86400)
      when :hours, :hour
        time + (number * 3600)
      when :minutes, :minute, :mins, :min
        time + (number * 60)
      when :seconds, :second, :secs, :sec, nil
        time + number
      else
        raise ArgumentError, "unrecognized time units -- #{units}"
      end
    )
  end
  dst_adjustment(time)
end
Also aliased as: in, hence
stamp(format = nil) click to toggle source

Create a time stamp.

@example

t = Time.at(10000)
t.stamp(:short)    # => "31 Dec 21:46"

Supported formats come from the Time::FORMAT constant.

# File lib/garcon/core_ext/time.rb, line 76
def stamp(format = nil)
  unless String === format
    format = FORMAT[format]
  end
  strftime(format).strip
end