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

Ticket #9312 (closed enhancement: fixed)

Opened 1 year ago

Last modified 8 months ago

[PATCH] Missing methods "end_of_..." in Time and Date

Reported by: BigTitus Assigned to: gbuesing
Priority: normal Milestone: 2.x
Component: ActiveSupport Version: edge
Severity: normal Keywords: end_of_day, end_of_quarter, end_of_week, end_of_year, time, date, calculations
Cc: juanjo.bazan, gbuesing

Description

Since Time::Calculations provides methods like "end_of_month" and even "beginning_of_year", "beginning_of_quarter" and so on I would except also the methods "end_of_day", "end_of_quarter", "end_of_week" and "end_of_year".

Attachments

time_end_of_methods.diff (6.8 kB) - added by juanjo.bazan on 08/24/07 23:44:26.
Methods and test
time_end_of_methods.patch (4.8 kB) - added by tarmo on 09/29/07 16:55:05.
Cleaned/rebased patch also fixed the :mday -> :day making test pass again
updated_time_end_of_methods.diff (8.9 kB) - added by juanjo.bazan on 02/19/08 13:57:18.
end_of_XX methods updated with Geoff suggestions

Change History

08/24/07 23:44:26 changed by juanjo.bazan

  • attachment time_end_of_methods.diff added.

Methods and test

08/24/07 23:47:15 changed by juanjo.bazan

  • summary changed from Missing methods "end_of_..." to [PATCH] Missing methods "end_of_..." in Time.

New methods(end_of_week, end_of_quarter, end_of_year), alias and tests.

08/24/07 23:47:34 changed by juanjo.bazan

  • version changed from 1.2.3 to edge.

09/29/07 16:55:05 changed by tarmo

  • attachment time_end_of_methods.patch added.

Cleaned/rebased patch also fixed the :mday -> :day making test pass again

10/08/07 11:19:54 changed by manfred

I was looking at the implementation of all the end_of_* methods and I saw that they all set the hours, minutes and seconds to 0. Wouldn't it make more sense if they were all :hour => 23, :min => 59, :sec => 59, :usec => 999?

(follow-up: ↓ 5 ) 11/16/07 20:28:52 changed by bitsweat

  • owner changed from core to gbuesing.

What do you think Geoff?

(in reply to: ↑ 4 ) 11/17/07 19:10:26 changed by gbuesing

Replying to bitsweat:

What do you think Geoff?

These new methods are consistent with the pattern set by end_of_month, which returns the beginning of the day of the last day of the month:

>> Time.local(2007,11, 17).end_of_month
=> Fri Nov 30 00:00:00 -0600 2007

However, to manfred's point, this doesn't seem quite right -- given that we're modifying a Time object, not a Date object, this should return a value corresponding to the end of the last day of the month, not the beginning of that day.

This results in counterintuitive behavior -- noon on Nov 29 is before the end of the month, but noon on Nov 30 is after:

>> Time.local(2007,11,29,12) < Time.local(2007,11,17).end_of_month
=> true
>> Time.local(2007,11,30,12) < Time.local(2007,11,17).end_of_month
=> false

To correct this, I think we should change end_of_month to return a Time representing the last second of the month. After this change, anyone who wanted the beginning of this day instead of the end could always get that with: time_obj.end_of_month.beginning_of_day.

In regards to manfred's suggestion to set usec to 999 for all end_of methods: this is technically correct, given the resolution of the Time class; however, in the domain of web apps, we generally don't care about storing and presenting time values with milliseconds, and to_s(:db) chops them off anyway. Also, usec values don't appear in Time #inspect representations, which makes debugging more difficult.

Given these reasons, I suggest that we stay with ActiveSupport's current behavior, which assumes a seconds resolution for all beginning_of and end_of methods (see Time#change, which sets usec to 0 when hour, min, or sec values are supplied.)

Moving forward, I suggest:

  1. end_of_month's implementation should be changed to return a Time representing the last second of the month
  2. the new end_of week/quarter/year methods should be updated to return the last second of the week, quarter and year
  3. the :sunday alias for end_of_week should be removed, since it would no longer be appropriate
  4. add end_of week/quarter/year methods to Date calculations, to preserve duck-typing compatibility with DateTime

11/18/07 01:03:08 changed by gbuesing

Patch to fix Time and DateTime #end_of_month: #10200

11/18/07 22:21:11 changed by revans

I have a plugin of most of these methods, some word for word (online for a year), if the implementations are meant to remain outside of rails, for anyone to use. ;)

11/18/07 22:22:04 changed by revans

02/19/08 13:00:56 changed by juanjo.bazan

  • cc set to juanjo.bazan, gbuesing.

02/19/08 13:57:18 changed by juanjo.bazan

  • attachment updated_time_end_of_methods.diff added.

end_of_XX methods updated with Geoff suggestions

02/19/08 13:59:18 changed by juanjo.bazan

  • keywords changed from end_of_day, end_of_quarter, end_of_week, end_of_year, time, calculations to end_of_day, end_of_quarter, end_of_week, end_of_year, time, date, calculations.
  • summary changed from [PATCH] Missing methods "end_of_..." in Time to [PATCH] Missing methods "end_of_..." in Time and Date.

Replying to gbuesing:

Moving forward, I suggest: 1. end_of_month's implementation should be changed to return a Time representing the last second of the month 2. the new end_of week/quarter/year methods should be updated to return the last second of the week, quarter and year 3. the :sunday alias for end_of_week should be removed, since it would no longer be appropriate 4. add end_of week/quarter/year methods to Date calculations, to preserve duck-typing compatibility with DateTime

Done! The new patch updates the implmentations of the methods and include them both in Time and Date classes.

02/27/08 22:53:58 changed by bitsweat

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

(In [8934]) Adding Time#end_of_day, _quarter, _week, and _year. Closes #9312.