Methods
Public Class methods
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.
[ show source ]
# File lib/facets/core/time/self/days_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
Tracks the elapse time of a code block.
Time.elapse { sleep 1 } #=> 0.999188899993896
[ show source ]
# File lib/facets/core/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
[ show source ]
# File lib/facets/core/time/stamp.rb, line 21 def self.stamp(*args) now.stamp(*args) end
Public Instance methods
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.
[ show source ]
# File lib/facets/core/time/change.rb, line 10 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
[ show source ]
# File lib/facets/core/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
Convert a Time to a Date. Time is a superset of Date. It is the year, month and day that are carried over.
[ show source ]
# File lib/facets/core/time/to_date.rb, line 6 def to_date require 'date' ::Date.new(year, month, day) end
To be able to keep Dates and Times interchangeable on conversions.
[ show source ]
# File lib/facets/core/time/to_time.rb, line 6 def to_time self end