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

Ticket #6835 (closed enhancement: fixed)

Opened 3 years ago

Last modified 2 years ago

[PATCH] Make 1.month.from_now be accurate

Reported by: eventualbuddha Assigned to: nzkoz
Priority: normal Milestone: 1.x
Component: ActiveSupport Version: edge
Severity: normal Keywords: time date manipulation
Cc: bitsweat

Description

The month, day, and year methods on Fixnum are not accurate, which is why Time#advance was added. This patch creates a Duration class which makes these methods use Time#advance for accurate date and time processing:

>> Time.now
=> Tue Dec 12 23:59:38 PST 2006
>> 1.month.from_now
=> Fri Jan 12 23:59:46 PST 2007

The current results:

>> Time.now
=> Tue Dec 12 23:59:38 PST 2006
>> 1.month.from_now
=> Fri Jan 11 23:59:46 PST 2007

The same applies for years and days. This patch also addresses the disconnect between adding to a Time and adding to a Date. See #6803 for the a description of the problem.

I expect to go through at least a few updates of the patch, so feedback is appreciated! Tests are included.

Attachments

add-duration-to-date-time.diff (8.4 kB) - added by eventualbuddha on 12/13/06 08:06:48.
add-duration-to-date-time2.diff (10.8 kB) - added by eventualbuddha on 01/06/07 00:31:17.
add-duration-to-date-time3.diff (11.2 kB) - added by eventualbuddha on 01/12/07 09:14:40.
add-duration-to-date-time4.diff (13.4 kB) - added by eventualbuddha on 01/12/07 09:49:45.

Change History

12/13/06 08:06:48 changed by eventualbuddha

  • attachment add-duration-to-date-time.diff added.

12/19/06 01:59:36 changed by divoxx

I really like it. +1 Vote for including it.

01/06/07 00:31:17 changed by eventualbuddha

  • attachment add-duration-to-date-time2.diff added.

01/06/07 00:36:56 changed by eventualbuddha

New patch supports Date#-, includes test cases for chaining methods.

01/07/07 22:15:17 changed by nzkoz

  • owner changed from core to nzkoz.
  • status changed from new to assigned.

01/12/07 09:14:40 changed by eventualbuddha

  • attachment add-duration-to-date-time3.diff added.

(follow-up: ↓ 6 ) 01/12/07 09:34:38 changed by bitsweat

  • cc set to bitsweat.
  • version set to edge.

This is cool. Great patch. Curious: what does rcov think of the test coverage?

01/12/07 09:49:45 changed by eventualbuddha

  • attachment add-duration-to-date-time4.diff added.

01/12/07 09:56:17 changed by eventualbuddha

Patch 4 has an additional test for Duration#inspect, so that when using script/console it'll show some more meaningful output:

>> 1.month
1 month
>> 10.years + 2.months + 1.day
10 years, 2 months, and 1 day

In addition I've fixed up the documentation a bit.

(in reply to: ↑ 4 ) 01/14/07 22:47:03 changed by eventualbuddha

Replying to bitsweat:

This is cool. Great patch. Curious: what does rcov think of the test coverage?

Just ran this rcov command:

rcov test/core_ext/duration_test.rb test/core_ext/date_ext_test.rb \
test/core_ext/time_ext_test.rb test/core_ext/numeric_ext_test.rb

And though RCov seems to think lines 61 and 62 of duration.rb are not covered, they are. So duration.rb and my additions to the other files are covered by the tests 100%. Is there anything else I need to do before this can be accepted?

01/15/07 00:51:32 changed by nzkoz

Looks good to me,

I'm happy to apply as is, will just test a few client's apps first and add any tests I find missing.

Great work

01/20/07 08:12:50 changed by eventualbuddha

Are we waiting on closing this until the related tickets (#7182, #7192, #7114) are taken care of? Or did someone just forget?

01/22/07 03:52:14 changed by eventualbuddha

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

Closing as fixed since it seems that this feature is done.