Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7706 (closed enhancement: fixed)

Opened 2 years ago

Last modified 2 years ago

[PATCH] add behavior to Time::Calculations so they automatically fall back to DateTime when out of range of Ruby Time class

Reported by: gbuesing Assigned to: core
Priority: normal Milestone: 1.x
Component: ActiveSupport Version: edge
Severity: normal Keywords: time calculations datetime
Cc:

Description

Current behavior:

>> ( Time.now.years_since 5 ).to_s(:long)
=> "March 03, 2012 18:30"

>> ( Time.now.years_since 32 ).to_s(:long)
ArgumentError: time out of range

Enhanced fallback behavior -- Time calculations now return DateTime when out of range of Time class:

>> ( Time.now.years_since 5 ).to_s(:long)
=> "March 03, 2012 18:30"

>> ( Time.now.years_since 32 ).to_s(:long)
=> "March 03, 2039 18:30"

>> ( Time.now.years_since 5 ).class
=> Time

>> ( Time.now.years_since 32 ).class
=> DateTime

Fallback behavior made possible by the addition of class methods #utc_time and #local_time:

>> ( Time.utc_time(2005) ).to_s(:long)
=> "January 01, 2005 00:00"

>> ( Time.utc_time(2039) ).to_s(:long)
=> "January 01, 2039 00:00"

>> ( Time.utc_time(2005) ).class      
=> Time

>> ( Time.utc_time(2039) ).class
=> DateTime  

Tests included.

Attachments

active_support_time_calculations_fallback_to_datetime.diff (4.1 kB) - added by gbuesing on 03/04/07 00:50:55.

Change History

03/04/07 00:50:55 changed by gbuesing

  • attachment active_support_time_calculations_fallback_to_datetime.diff added.

03/04/07 01:41:33 changed by gbuesing

Also see: [PATCH] added DateTime::Calculations to ActiveSupport::CoreExtensions http://dev.rubyonrails.org/ticket/7693

If both of these patches are added to trunk, you could then do stuff like:

Time.now.years_since(32).beginning_of_day

03/04/07 01:58:28 changed by gbuesing

  • summary changed from [PATCH] Time::Calculations automatically fall back to DateTime when out of range of Ruby Time class to [PATCH] add behavior Time::Calculations so they automatically fall back to DateTime when out of range of Ruby Time class.

03/04/07 01:58:53 changed by gbuesing

  • summary changed from [PATCH] add behavior Time::Calculations so they automatically fall back to DateTime when out of range of Ruby Time class to [PATCH] add behavior to Time::Calculations so they automatically fall back to DateTime when out of range of Ruby Time class.

03/04/07 04:51:47 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.