This project is archived and is in readonly mode.
time.end_of_day not entirely accurate
Reported by Ivan Vega | October 23rd, 2008 @ 01:38 AM | in 2.x
Time.new.end_of_day.usec returns 0, and it should return 999999 instead, IMHO.
The relevant change is on line 210 of activesupport/lib/active_support/core_ext/time/calculations.rb, simply from:
def end_of_day
change(:hour => 23, :min => 59, :sec => 59)
end
to:
def end_of_day
change(:hour => 23, :min => 59, :sec => 59, :usec => 999999)
end
I didn't create a patch because I thought the change is so minimal, but I will if you require it.
This is relevant on an application I'm experimenting with because I'm sorting records by a timestamp, and that sorting could be messed up as the order of two records with the same hour/min/sec would be unpredictable.
Comments and changes to this ticket
-
Pratik January 19th, 2009 @ 02:11 AM
- Assigned user set to Geoff Buesing
-
Geoff Buesing January 19th, 2009 @ 03:05 PM
- Tag changed from activesupport, time, timestamps to activesupport, time, usec
- State changed from new to open
I can see how this would result unexpected behavior when you're sorting or comparing based on a timestamp -- you'd assume, for example, that any record that was created after the #end_of_day was created on the next day, but this would not necessarily be the case.
If we're going to go ahead and change #end_of_day, we should also change the other end_of_ methods: #end_of_week, #end_of_month, #end_of_quarter, #end_of_year.
Existing tests for these methods would break when we added :usec => 999999 to their implementations, so we'd need a patch that changed implementations and tests for these methods.
-
Geoff Buesing February 8th, 2009 @ 07:15 PM
- State changed from open to incomplete
Please reopen if you have a patch for this
-
CancelProfileIsBroken August 5th, 2009 @ 01:34 PM
- Tag changed from activesupport, time, usec to activesupport, bugmash, time, usec
-
Matt Duncan August 7th, 2009 @ 02:26 AM
Attached patch to fix all of the end_of methods: #end_of_day, #end_of_week, #end_of_month, #end_of_quarter, and #end_of_year. I've also updated the tests for these methods to check for the correct usec value.
-
Geoff Buesing August 7th, 2009 @ 03:41 AM
@Matt - thanks for this patch.
One issue: your updated docs for Date#end_of_ methods say that "DateTime objects will have time set to 23:59:59.999999", but this isn't true -- the existing tests confirm this (you're passing a :usec value into DateTime change, but that doesn't do anything.)
We could go forward without worrying about DateTime. DateTime has a weird sec_fraction value that's a Rational; I don't know how that would translate into usec, or something similar.
-
Matt Duncan August 8th, 2009 @ 12:04 AM
@Geoff - Here is the fixed patch, it includes only changes to the Time#end_of_ methods.
-
Josh Sharpe August 8th, 2009 @ 01:46 AM
The patch applies successfully and all tests pass.
The tests look good, and the new functionality makes sense to me.
+1
-
Hugo Peixoto August 9th, 2009 @ 04:27 AM
+1 for the idea, but the patch doesn't apply to 2-3-stable. I've attached a patch that works. It was probably due to all the recent commits or something.
-
Matt Duncan August 9th, 2009 @ 05:54 AM
Hugo, I see the following two stylistic items got changed in your patch (vs my original):
# Extra space before closing paren: last_day = ::Time.days_in_month( self.month, self.year ) # Missing space after first comma: # (and while it's being fixed, the spacing around the second comma could probably be fixed also) change(:month => 12,:day => 31,:hour => 23, :min => 59, :sec => 59)
-
Kieran P August 9th, 2009 @ 06:42 AM
+1 for Hugo's patch for 2-3. Uploading one compatible with master branch.
-
Kieran P August 9th, 2009 @ 07:08 AM
- Title changed from time.end_of_day not entirely accurate to [PATCH] time.end_of_day not entirely accurate
-
Jeremy Kemper August 9th, 2009 @ 09:40 AM
- State changed from incomplete to open
One downside is that this complicates compatibility with Ruby 1.9.2. It has Time#nsec - nanosecond resolution.
-
Jeremy Kemper August 9th, 2009 @ 09:40 AM
- Title changed from [PATCH] time.end_of_day not entirely accurate to time.end_of_day not entirely accurate
-
Hugo Peixoto August 9th, 2009 @ 01:05 PM
I've attached a patch for master that supposedly on master, with both ruby1.8 and 1.9. Please test it, as my 1.9 setup is kind of broken.
-
José Valim August 9th, 2009 @ 04:01 PM
- Tag changed from activesupport, bugmash, time, usec to activesupport, time, usec
- State changed from open to resolved
-
Repository August 9th, 2009 @ 04:03 PM
(from [3545d6b0b227cc9ee4a645097bdbd43b33b42a87]) Setting usec (and nsec for Ruby 1.9) on Time#end_of_* methods [#1255 status:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/3545d6b0b227cc9ee4a645097bdbd4...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Tags
Referenced by
- 1255 time.end_of_day not entirely accurate (from [3545d6b0b227cc9ee4a645097bdbd43b33b42a87]) Setting...