Prev | Current Page 96 | Next

Brad Ediger

"Advanced Rails"

The hour, min, sec,
and usec options cascade downward??”for example, if the hour but not the minute is
specified, the minute will be set to zero.
t = Time.now
t # => Thu Dec 28 13:39:18 CST 2006
t.change(:min => 31, :sec => 12) # => Thu Dec 28 13:31:12 CST 2006
t.change(:hour => 12) # => Thu Dec 28 12:00:00 CST 2006
??? The ago and since methods, formerly operating on Time, now operate on
ActiveSupport::Duration objects, to support exact time values. Methods such as
Fixnum#months and Fixnum#year now return ActiveSupport::Duration objects:
Table 2-2. Time formats
Format Example
:default Thu Dec 28 13:12:23 CST 2006
:db 2006-12-28 13:12:23
:time 13:12
:short 28 Dec 13:12
:long December 28, 2006 13:12
:rfc822 Thu, 28 Dec 2006 13:12:23 -0600
Enumerable
Enumerable | 65
1.month # => 1 month
1.month.ago # => Mon Sep 24 23:57:04 -0500 2007
1.month.since(1.year.ago) # => Fri Nov 24 23:57:04 -0600 2006
The addition of ActiveSupport::Duration fixes many subtle problems with time calculations,
because it stores durations as exact values (years, months, days, hours, minutes,
and seconds). Prior to its addition, errors could easily accumulate, as some
duration functions assumed 30-day months and 365.25-day years.
??? Time#advance adds the specified (exact) values to the given time:
t = Time.now # => Wed Oct 24 23:50:11 -0500 2007
t.advance :days => 1 # => Thu Oct 25 23:50:11 -0500 2007
t.


Pages:
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108