Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Beginning and end of hour support for Time and DateTime #6156

Merged
merged 1 commit into from

4 participants

Mark J. Titorenko Larz Conwell Jeremy Kemper Vijay Dev
Mark J. Titorenko

This feature was initially proposed in #598.

This pull request consists of implementations and test coverage fro #beginning_of_hour and #end_of_hour methods for Time and DateTime core extensions within activesupport. No implementation has been provided for Date (beginning_of_hour and end_of_hour for a Date seems nonsensical).

While this feature has been based on master, I'm more than happy to submit it against 3-2-stable for any future 3.2.x release.

For anybody wanting to monkey patch their existing Rails applications with this code, take this gist and place in an initializer: https://gist.github.com/2595181

Larz Conwell

Awesome I like beginning_of_hour. I'd like 3.2 support as well (: :+1:

Mark J. Titorenko

@larzconwell Ok, I've also applied the above commit against 3-2-stable in my feature-beginning_of_hour-for-3-2-stable branch. The commit can be cherry-picked from/viewed etc. here: 145cc69

I can open a second pull request for that branch to 3-2-stable if anybody from the Rails core team wants to instruct me to do so. :-)

Larz Conwell

@jeremy What do you think about this?

Jeremy Kemper
Owner

I've wanted beginning_of_hour before. When have you needed end_of_hour, or is it just for symmetry?

Made some implementation comments inline.

Jeremy Kemper jeremy commented on the diff
...lib/active_support/core_ext/date_time/calculations.rb
@@ -91,6 +91,17 @@ def end_of_day
change(:hour => 23, :min => 59, :sec => 59)
end
+ # Returns a new DateTime representing the start of the hour (hh:00:00)
+ def beginning_of_hour
+ change(:min => 0)
Jeremy Kemper Owner
jeremy added a note

Should this change seconds and usec to 0 also?

Jeremy Kemper Owner
jeremy added a note

Ah nevermind, setting :min zeroes :sec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Jeremy Kemper jeremy merged commit fecfd61 into from
Jeremy Kemper
Owner

Backport welcome to 3-2-stable :)

Larz Conwell

I haven't need end_of_hour but I'm sure it would come in handy at some point, plus it's nice to keep the beginning and ending together. :D

Mark J. Titorenko

Yup, end_of_hour is really for the symmetry and to conform to the principle of least surprise should somebody find they need it. In general, the uses for end_of_X seem to be less common than uses for beginning_of_X. :)

I'll open a pull request from my 3-2-stable feature branch for the backport.

Vijay Dev
Collaborator

@mjtko Hey, can you please document this in the AS core extensions guide?

Mark J. Titorenko

Sure, no problem, will get on that later today (UK).

Mark J. Titorenko

Hi @vijaydev,

Please see rails/docrails@8c4777f . I guess this will want to be brought through to the 3-2-stable banch too given the backport in #6170.

Hope I've done the correct thing in pushing this to docrails; if not, let me know and I'll revert and open pull requests instead.

Cheers.

Mark J. Titorenko

Sorry, I just spotted a mistake. Now corrected in rails/docrails@61587b1

Also, while I'm adding another comment, I also spotted a minor issue with the existing example dates for beginning_of_day and end_of_day. That one is in rails/docrails@f174e8d

Over to you. :-)

Vijay Dev
Collaborator

docrails is fine. I will backport when merging to master. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 4, 2012
  1. Mark J. Titorenko
This page is out of date. Refresh to see the latest.
11 activesupport/lib/active_support/core_ext/date_time/calculations.rb
View
@@ -91,6 +91,17 @@ def end_of_day
change(:hour => 23, :min => 59, :sec => 59)
end
+ # Returns a new DateTime representing the start of the hour (hh:00:00)
+ def beginning_of_hour
+ change(:min => 0)
Jeremy Kemper Owner
jeremy added a note

Should this change seconds and usec to 0 also?

Jeremy Kemper Owner
jeremy added a note

Ah nevermind, setting :min zeroes :sec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ end
+ alias :at_beginning_of_hour :beginning_of_hour
+
+ # Returns a new DateTime representing the end of the hour (hh:59:59)
+ def end_of_hour
+ change(:min => 59, :sec => 59)
+ end
+
# Adjusts DateTime to UTC by adding its offset value; offset is set to 0
#
# Example:
15 activesupport/lib/active_support/core_ext/time/calculations.rb
View
@@ -257,6 +257,21 @@ def end_of_day
)
end
+ # Returns a new Time representing the start of the hour (x:00)
+ def beginning_of_hour
+ change(:min => 0)
+ end
+ alias :at_beginning_of_hour :beginning_of_hour
+
+ # Returns a new Time representing the end of the hour, x:59:59.999999 (.999999999 in ruby1.9)
+ def end_of_hour
+ change(
+ :min => 59,
+ :sec => 59,
+ :usec => 999999.999
+ )
+ end
+
# Returns a new Time representing the start of the month (1st of the month, 0:00)
def beginning_of_month
#self - ((self.mday-1).days + self.seconds_since_midnight)
8 activesupport/test/core_ext/date_time_ext_test.rb
View
@@ -91,6 +91,14 @@ def test_end_of_day
assert_equal DateTime.civil(2005,2,4,23,59,59), DateTime.civil(2005,2,4,10,10,10).end_of_day
end
+ def test_beginning_of_hour
+ assert_equal DateTime.civil(2005,2,4,19,0,0), DateTime.civil(2005,2,4,19,30,10).beginning_of_hour
+ end
+
+ def test_end_of_hour
+ assert_equal DateTime.civil(2005,2,4,19,59,59), DateTime.civil(2005,2,4,19,30,10).end_of_hour
+ end
+
def test_beginning_of_month
assert_equal DateTime.civil(2005,2,1,0,0,0), DateTime.civil(2005,2,22,10,10,10).beginning_of_month
end
8 activesupport/test/core_ext/time_ext_test.rb
View
@@ -93,6 +93,10 @@ def test_beginning_of_day
end
end
+ def test_beginning_of_hour
+ assert_equal Time.local(2005,2,4,19,0,0), Time.local(2005,2,4,19,30,10).beginning_of_hour
+ end
+
def test_beginning_of_month
assert_equal Time.local(2005,2,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_month
end
@@ -127,6 +131,10 @@ def test_end_of_week
assert_equal Time.local(2007,9,2,23,59,59,999999.999), Time.local(2007,9,02,0,0,0).end_of_week #sunday
end
+ def test_end_of_hour
+ assert_equal Time.local(2005,2,4,19,59,59,999999.999), Time.local(2005,2,4,19,30,10).end_of_hour
+ end
+
def test_end_of_month
assert_equal Time.local(2005,3,31,23,59,59,999999.999), Time.local(2005,3,20,10,10,10).end_of_month
assert_equal Time.local(2005,2,28,23,59,59,999999.999), Time.local(2005,2,20,10,10,10).end_of_month
14 activesupport/test/core_ext/time_with_zone_test.rb
View
@@ -512,6 +512,20 @@ def end_of_day
assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_day.inspect
end
+ def test_beginning_of_hour
+ utc = Time.utc(2000, 1, 1, 0, 30)
+ twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
+ assert_equal "Fri, 31 Dec 1999 19:30:00 EST -05:00", twz.inspect
+ assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", twz.beginning_of_hour.inspect
+ end
+
+ def test_end_of_hour
+ utc = Time.utc(2000, 1, 1, 0, 30)
+ twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
+ assert_equal "Fri, 31 Dec 1999 19:30:00 EST -05:00", twz.inspect
+ assert_equal "Fri, 31 Dec 1999 19:59:59 EST -05:00", twz.end_of_hour.inspect
+ end
+
def test_since
assert_equal "Fri, 31 Dec 1999 19:00:01 EST -05:00", @twz.since(1).inspect
end
Something went wrong with that request. Please try again.