Permalink
Browse files

Added beginning_of_minute support to core_ext calculations for Time a…

…nd DateTime
  • Loading branch information...
gaganawhad committed Feb 21, 2013
1 parent 3893979 commit 6f380d37788a562c13d0d68b1d3f28f8a6f4b3ec
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##
+* Added beginning_of_minute support to core_ext calculations for Time and DateTime
+
+ *Gagan Awhad*
+
* Add `:nsec` date format.
*Jamie Gaskins*
@@ -123,6 +123,18 @@ def end_of_hour
change(:min => 59, :sec => 59)
end
alias :at_end_of_hour :end_of_hour
+
+ # Returns a new DateTime representing the start of the hour (hh:mm:00).
+ def beginning_of_minute

This comment has been minimized.

Show comment Hide comment
@eval

eval Feb 22, 2013

Contributor

The comment should state minute not hour.

@eval

eval Feb 22, 2013

Contributor

The comment should state minute not hour.

+ change(:sec => 0)
+ end
+ alias :at_beginning_of_minute :beginning_of_minute
+
+ # Returns a new DateTime representing the end of the minute (hh:mm:59).
+ def end_of_minute
+ change(:sec => 59)
+ end
+ alias :at_end_of_minute :end_of_minute
# Adjusts DateTime to UTC by adding its offset value; offset is set to 0.
#
@@ -188,6 +188,21 @@ def end_of_hour
end
alias :at_end_of_hour :end_of_hour
+ # Returns a new Time representing the start of the hour (x:xx:00)
+ def beginning_of_minute

This comment has been minimized.

Show comment Hide comment
@eval

eval Feb 22, 2013

Contributor

Here as well: the comment should state minute not hour.

@eval

eval Feb 22, 2013

Contributor

Here as well: the comment should state minute not hour.

This comment has been minimized.

Show comment Hide comment
@gaganawhad

gaganawhad Feb 22, 2013

Contributor
@gaganawhad

gaganawhad via email Feb 22, 2013

Contributor

This comment has been minimized.

Show comment Hide comment
@eval

eval Feb 22, 2013

Contributor

Somewhat hard to write a failing test for this, right? ;)
Let's ask @fxn

@eval

eval Feb 22, 2013

Contributor

Somewhat hard to write a failing test for this, right? ;)
Let's ask @fxn

This comment has been minimized.

Show comment Hide comment
@carlosantoniodasilva

carlosantoniodasilva Feb 22, 2013

Member

Yes, another PR or wait the cross merge with docrails to just update in there, both are ok.

@carlosantoniodasilva

carlosantoniodasilva Feb 22, 2013

Member

Yes, another PR or wait the cross merge with docrails to just update in there, both are ok.

This comment has been minimized.

Show comment Hide comment
@eval

eval Feb 22, 2013

Contributor

Thanks. I opened #9384

@eval

eval Feb 22, 2013

Contributor

Thanks. I opened #9384

This comment has been minimized.

Show comment Hide comment
@gaganawhad

gaganawhad Feb 22, 2013

Contributor

Thanks @eval

@gaganawhad

gaganawhad Feb 22, 2013

Contributor

Thanks @eval

+ change(:sec => 0)
+ end
+ alias :at_beginning_of_minute :beginning_of_minute
+
+ # Returns a new Time representing the end of the hour, x:59:59.999999 (.999999999 in ruby1.9)
+ def end_of_minute
+ change(
+ :sec => 59,
+ :usec => Rational(999999999, 1000)
+ )
+ end
+ alias :at_end_of_minute :end_of_minute
+
# Returns a Range representing the whole day of the current time.
def all_day
beginning_of_day..end_of_day
@@ -88,6 +88,14 @@ 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_minute
+ assert_equal DateTime.civil(2005,2,4,19,30,0), DateTime.civil(2005,2,4,19,30,10).beginning_of_minute
+ end
+
+ def test_end_of_minute
+ assert_equal DateTime.civil(2005,2,4,19,30,59), DateTime.civil(2005,2,4,19,30,10).end_of_minute
+ end
+
def test_end_of_month
assert_equal DateTime.civil(2005,3,31,23,59,59), DateTime.civil(2005,3,20,10,10,10).end_of_month
assert_equal DateTime.civil(2005,2,28,23,59,59), DateTime.civil(2005,2,20,10,10,10).end_of_month
@@ -121,6 +121,10 @@ 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_minute
+ assert_equal Time.local(2005,2,4,19,30,0), Time.local(2005,2,4,19,30,10).beginning_of_minute
+ end
+
def test_end_of_day
assert_equal Time.local(2007,8,12,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,12,10,10,10).end_of_day
with_env_tz 'US/Eastern' do
@@ -137,6 +141,10 @@ def test_end_of_hour
assert_equal Time.local(2005,2,4,19,59,59,Rational(999999999, 1000)), Time.local(2005,2,4,19,30,10).end_of_hour
end
+ def test_end_of_minute
+ assert_equal Time.local(2005,2,4,19,30,59,Rational(999999999, 1000)), Time.local(2005,2,4,19,30,10).end_of_minute
+ end
+
def test_last_year
assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).last_year
end
@@ -539,6 +539,20 @@ def test_end_of_hour
assert_equal "Fri, 31 Dec 1999 19:59:59 EST -05:00", twz.end_of_hour.inspect
end
+ def test_beginning_of_minute
+ utc = Time.utc(2000, 1, 1, 0, 30, 10)
+ twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
+ assert_equal "Fri, 31 Dec 1999 19:30:10 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_minute
+ utc = Time.utc(2000, 1, 1, 0, 30, 10)
+ twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
+ assert_equal "Fri, 31 Dec 1999 19:30:10 EST -05:00", twz.inspect
+ assert_equal "Fri, 31 Dec 1999 19:30:59 EST -05:00", twz.end_of_minute.inspect
+ end
+
def test_since
assert_equal "Fri, 31 Dec 1999 19:00:01 EST -05:00", @twz.since(1).inspect
end

3 comments on commit 6f380d3

@fxn

This comment has been minimized.

Show comment Hide comment
@fxn

fxn Feb 21, 2013

Member

Hey, the Active Support core extensions guide should have been updated by this patch.

Member

fxn replied Feb 21, 2013

Hey, the Active Support core extensions guide should have been updated by this patch.

@gaganawhad

This comment has been minimized.

Show comment Hide comment
@gaganawhad

gaganawhad Feb 21, 2013

Contributor

Got it! I will do that. Thanks for getting my attention on it.

Contributor

gaganawhad replied Feb 21, 2013

Got it! I will do that. Thanks for getting my attention on it.

@gaganawhad

This comment has been minimized.

Show comment Hide comment
@gaganawhad

gaganawhad Feb 22, 2013

Contributor
Please sign in to comment.