Class: Time

Inherits:
Object show all
Defined in:
lib/nano/time/to_s.rb,
lib/nano/time/stamp.rb,
lib/nano/time/change.rb,
lib/nano/time/elapse.rb,
lib/nano/time/to_date.rb,
lib/nano/time/to_time.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.days_extrema(time1, time2 = nil) ⇒ Object

This method calculates the days extrema given two time objects. start time is the given time1 at 00:00:00 end time is the given time2 at 23:59:59:999

Input:

  • the two times (if only time1 is provided then you get an extrema of exactly one day extrema.

Output:

  • the time range. you can get the start/end times using range methods.

– Credit goes to George Moschovitis ++



16
17
18
19
20
21
22
# File 'lib/nano/time/%3A%3Adays_extrema.rb', line 16

def Time.days_extrema(time1, time2=nil)
  time2 = time1 if (not time2.valid? Time)
  time2 = NEVER if (time2 <= time1)
  start_time = Time.self.start_of_day(time1)
  end_time = self.end_of_day(time2)
  return (start_time..end_time)
end

.elapseObject

Tracks the elapse time of a code block.

Time.elapse { sleep 1 }  #=> 0.999188899993896

– Credit goes to Hal Fulton. ++



11
12
13
14
15
16
# File 'lib/nano/time/elapse.rb', line 11

def self.elapse
  raise "Need block" unless block_given?
  t0 = Time.now.to_f
  yield
  Time.now.to_f - t0
end

Instance Method Details

#change(options) ⇒ Object

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.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/nano/time/change.rb', line 7

def change(options)
  opts={}; options.each_pair{ |k,v| opts[k] = v.to_i }
  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]   || (opts[:hour] ? 0 : self.min),
    opts[:sec]   || ((opts[:hour] || opts[:min]) ? 0 : self.sec),
    opts[:usec]  || ((opts[:hour] || opts[:min] || opts[:usec]) ? 0 : self.usec)
  )
end

#stamp(fmt = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/nano/time/stamp.rb', line 4

def stamp( fmt = nil )
  case fmt
  when :db, :dbase, :database, :utc
    strftime("%Y-%m-%d %H:%M:%S")
  when :short
    strftime("%e %b %H:%M")
  when :long
    strftime("%B %e, %Y %H:%M")
  when :day1st, :dmYHM  # TODO generalize
    strftime("%d-%m-%Y %H:%M")
  when String
    strftime( fmt ).strip
  else
    strftime("%a %b %d %H:%M:%S %Z %Y")
  end
end

#to_dateObject

Convert a Time to a Date. Time is a superset of Date. It is the year, month and day that are carried over.



4
5
6
7
# File 'lib/nano/time/to_date.rb', line 4

def to_date
  require 'date'
  ::Date.new(year, month, day)
end

#to_sObject

Replace #to_s with #stamp.



5
# File 'lib/nano/time/to_s.rb', line 5

alias_method :to_s, :stamp

#to_timeObject

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



5
6
7
# File 'lib/nano/time/to_time.rb', line 5

def to_time
  self
end