From 898bd38d7a89c39a5e89f0e6be208e6622b07e3d Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 10 Aug 2010 01:30:37 +0200 Subject: AS guide: documents date/datetime/time arithmetic with durations --- .../source/active_support_core_extensions.textile | 63 +++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) (limited to 'railties/guides/source/active_support_core_extensions.textile') diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index 9a449debae..fbb617715f 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -3009,6 +3009,26 @@ Date.new(2010, 1, 31).change(:month => 2) # => ArgumentError: invalid date +h5. Durations + +Durations can be added and substracted to dates: + + +d = Date.current +# => Mon, 09 Aug 2010 +d + 1.year +# => Tue, 09 Aug 2011 +d - 3.hours +# => Sun, 08 Aug 2010 21:00:00 UTC +00:00 + + +They translate to calls to +since+ or +advance+. For example here we get the correct jump in the calendar reform: + + +Date.new(1582, 10, 4) + 1.day +# => Fri, 15 Oct 1582 + + h5. Timestamps INFO: The following methods return a +Time+ object if possible, otherwise a +DateTime+. If set, they honor the user time zone. @@ -3195,7 +3215,25 @@ DateTime.current.change(:month => 2, :day => 30) # => ArgumentError: invalid date -h4(#datetime-conversions). Conversions +h5. Durations + +Durations can be added and substracted to datetimes: + + +now = DateTime.current +# => Mon, 09 Aug 2010 23:15:17 +0000 +now + 1.year +# => Tue, 09 Aug 2011 23:15:17 +0000 +now - 1.week +# => Mon, 02 Aug 2010 23:15:17 +0000 + + +They translate to calls to +since+ or +advance+. For example here we get the correct jump in the calendar reform: + + +DateTime.new(1582, 10, 4, 23) + 1.hour +# => Fri, 15 Oct 1582 00:00:00 +0000 + h3. Extensions to +Time+ @@ -3243,6 +3281,9 @@ They are analogous. Please refer to their documentation above and take into acco * +Time+ understands DST, so you get correct DST calculations as in +Time.zone_default +# => # + # In Barcelona, 2010/03/28 02:00 +0100 becomes 2010/03/28 03:00 +0200 due to DST. t = Time.local_time(2010, 3, 28, 1, 59, 59) # => Sun Mar 28 01:59:59 +0100 2010 @@ -3287,6 +3328,26 @@ Both +local_time+ and +utc_time+ accept up to seven positional arguments: year, If the time to be constructed lies beyond the range supported by +Time+ in the runtime platform, usecs are discarded and a +DateTime+ object is returned instead. +h5. Durations + +Durations can be added and substracted to time objects: + + +now = Time.current +# => Mon, 09 Aug 2010 23:20:05 UTC +00:00 +now + 1.year +# => Tue, 09 Aug 2011 23:21:11 UTC +00:00 +now - 1.week +# => Mon, 02 Aug 2010 23:21:11 UTC +00:00 + + +They translate to calls to +since+ or +advance+. For example here we get the correct jump in the calendar reform: + + +Time.utc_time(1582, 10, 3) + 5.days +# => Mon Oct 18 00:00:00 UTC 1582 + + h3. Extensions to +Process+ ... -- cgit v1.2.3