Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

time.end_of_day not entirely accurate

#1255

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.

Reported by Ivan Vega · October 23rd, 2008 @ 01:38 AM

State: resolved
Milestone: 2.x
Assigned to: Geoff Buesing Geoff Buesing
Importance: none

Activity

  1. Pratik
    Pratik
    • Assigned user set to Geoff Buesing

    January 19th, 2009 @ 02:11 AM

  2. Geoff Buesing
    Geoff Buesing
    • 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.

    January 19th, 2009 @ 03:05 PM

  3. Geoff Buesing
    Geoff Buesing
    • State changed from open to incomplete

    Please reopen if you have a patch for this

    February 8th, 2009 @ 07:15 PM

  4. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from activesupport, time, usec to activesupport, bugmash, time, usec

    August 5th, 2009 @ 01:34 PM

  5. Matt Duncan
    Matt Duncan

    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.

    August 7th, 2009 @ 02:26 AM

  6. Geoff Buesing
    Geoff Buesing

    @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.

    August 7th, 2009 @ 03:41 AM

  7. Matt Duncan
    Matt Duncan

    @Geoff - Here is the fixed patch, it includes only changes to the Time#end_of_ methods.

    August 8th, 2009 @ 12:04 AM

  8. Josh Sharpe
    Josh Sharpe

    The patch applies successfully and all tests pass.

    The tests look good, and the new functionality makes sense to me.

    +1

    August 8th, 2009 @ 01:46 AM

  9. Hugo Peixoto
    Hugo Peixoto

    +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.

    August 9th, 2009 @ 04:27 AM

  10. Rizwan Reza
    Rizwan Reza

    verified

    +1 The patch by Hugo works on 2-3-stable.

    August 9th, 2009 @ 04:33 AM

  11. Matt Duncan
    Matt Duncan

    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)
    

    August 9th, 2009 @ 05:54 AM

  12. Kieran P
    Kieran P

    +1 for Hugo's patch for 2-3. Uploading one compatible with master branch.

    August 9th, 2009 @ 06:42 AM

  13. Kieran P
    Kieran P
    • Title changed from time.end_of_day not entirely accurate to [PATCH] time.end_of_day not entirely accurate

    August 9th, 2009 @ 07:08 AM

  14. Jeremy Kemper
    Jeremy Kemper
    • State changed from incomplete to open

    One downside is that this complicates compatibility with Ruby 1.9.2. It has Time#nsec - nanosecond resolution.

    August 9th, 2009 @ 09:40 AM

  15. Jeremy Kemper
    Jeremy Kemper
    • Title changed from [PATCH] time.end_of_day not entirely accurate to time.end_of_day not entirely accurate

    August 9th, 2009 @ 09:40 AM

  16. Hugo Peixoto
    Hugo Peixoto

    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.

    August 9th, 2009 @ 01:05 PM

  17. railsbob
    railsbob

    +1 Hugo's patch applied cleanly on master, with ruby 1.8

    August 9th, 2009 @ 01:28 PM

  18. Nikolay Petrachkov
    Nikolay Petrachkov

    +1 Hugo's patch applied on master with ruby 1.9.2.

    August 9th, 2009 @ 01:33 PM

  19. Hugo Peixoto
  20. José Valim
    José Valim

    Both patches (master and 2-3) pass on both 1.8.7 and 1.9.1.

    August 9th, 2009 @ 02:26 PM

  21. José Valim
    José Valim
    • Tag changed from activesupport, bugmash, time, usec to activesupport, time, usec
    • State changed from open to resolved

    August 9th, 2009 @ 04:01 PM

  22. Repository