Skip to content
This repository
Browse code

Added beginning_of_minute support to core_ext calculations for Time a…

…nd DateTime
  • Loading branch information...
commit 6f380d37788a562c13d0d68b1d3f28f8a6f4b3ec 1 parent 3893979
Gagan Awhad gaganawhad authored
4 activesupport/CHANGELOG.md
Source Rendered
... ... @@ -1,5 +1,9 @@
1 1 ## Rails 4.0.0 (unreleased) ##
2 2
  3 +* Added beginning_of_minute support to core_ext calculations for Time and DateTime
  4 +
  5 + *Gagan Awhad*
  6 +
3 7 * Add `:nsec` date format.
4 8
5 9 *Jamie Gaskins*
12 activesupport/lib/active_support/core_ext/date_time/calculations.rb
@@ -123,6 +123,18 @@ def end_of_hour
123 123 change(:min => 59, :sec => 59)
124 124 end
125 125 alias :at_end_of_hour :end_of_hour
  126 +
  127 + # Returns a new DateTime representing the start of the hour (hh:mm:00).
  128 + def beginning_of_minute
  129 + change(:sec => 0)
  130 + end
  131 + alias :at_beginning_of_minute :beginning_of_minute
  132 +
  133 + # Returns a new DateTime representing the end of the minute (hh:mm:59).
  134 + def end_of_minute
  135 + change(:sec => 59)
  136 + end
  137 + alias :at_end_of_minute :end_of_minute
126 138
127 139 # Adjusts DateTime to UTC by adding its offset value; offset is set to 0.
128 140 #
15 activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -188,6 +188,21 @@ def end_of_hour
188 188 end
189 189 alias :at_end_of_hour :end_of_hour
190 190
  191 + # Returns a new Time representing the start of the hour (x:xx:00)
  192 + def beginning_of_minute
  193 + change(:sec => 0)
  194 + end
  195 + alias :at_beginning_of_minute :beginning_of_minute
  196 +
  197 + # Returns a new Time representing the end of the hour, x:59:59.999999 (.999999999 in ruby1.9)
  198 + def end_of_minute
  199 + change(
  200 + :sec => 59,
  201 + :usec => Rational(999999999, 1000)
  202 + )
  203 + end
  204 + alias :at_end_of_minute :end_of_minute
  205 +
191 206 # Returns a Range representing the whole day of the current time.
192 207 def all_day
193 208 beginning_of_day..end_of_day
8 activesupport/test/core_ext/date_time_ext_test.rb
@@ -88,6 +88,14 @@ def test_end_of_hour
88 88 assert_equal DateTime.civil(2005,2,4,19,59,59), DateTime.civil(2005,2,4,19,30,10).end_of_hour
89 89 end
90 90
  91 + def test_beginning_of_minute
  92 + assert_equal DateTime.civil(2005,2,4,19,30,0), DateTime.civil(2005,2,4,19,30,10).beginning_of_minute
  93 + end
  94 +
  95 + def test_end_of_minute
  96 + assert_equal DateTime.civil(2005,2,4,19,30,59), DateTime.civil(2005,2,4,19,30,10).end_of_minute
  97 + end
  98 +
91 99 def test_end_of_month
92 100 assert_equal DateTime.civil(2005,3,31,23,59,59), DateTime.civil(2005,3,20,10,10,10).end_of_month
93 101 assert_equal DateTime.civil(2005,2,28,23,59,59), DateTime.civil(2005,2,20,10,10,10).end_of_month
8 activesupport/test/core_ext/time_ext_test.rb
@@ -121,6 +121,10 @@ def test_beginning_of_hour
121 121 assert_equal Time.local(2005,2,4,19,0,0), Time.local(2005,2,4,19,30,10).beginning_of_hour
122 122 end
123 123
  124 + def test_beginning_of_minute
  125 + assert_equal Time.local(2005,2,4,19,30,0), Time.local(2005,2,4,19,30,10).beginning_of_minute
  126 + end
  127 +
124 128 def test_end_of_day
125 129 assert_equal Time.local(2007,8,12,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,12,10,10,10).end_of_day
126 130 with_env_tz 'US/Eastern' do
@@ -137,6 +141,10 @@ def test_end_of_hour
137 141 assert_equal Time.local(2005,2,4,19,59,59,Rational(999999999, 1000)), Time.local(2005,2,4,19,30,10).end_of_hour
138 142 end
139 143
  144 + def test_end_of_minute
  145 + assert_equal Time.local(2005,2,4,19,30,59,Rational(999999999, 1000)), Time.local(2005,2,4,19,30,10).end_of_minute
  146 + end
  147 +
140 148 def test_last_year
141 149 assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).last_year
142 150 end
14 activesupport/test/core_ext/time_with_zone_test.rb
@@ -539,6 +539,20 @@ def test_end_of_hour
539 539 assert_equal "Fri, 31 Dec 1999 19:59:59 EST -05:00", twz.end_of_hour.inspect
540 540 end
541 541
  542 + def test_beginning_of_minute
  543 + utc = Time.utc(2000, 1, 1, 0, 30, 10)
  544 + twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
  545 + assert_equal "Fri, 31 Dec 1999 19:30:10 EST -05:00", twz.inspect
  546 + assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", twz.beginning_of_hour.inspect
  547 + end
  548 +
  549 + def test_end_of_minute
  550 + utc = Time.utc(2000, 1, 1, 0, 30, 10)
  551 + twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
  552 + assert_equal "Fri, 31 Dec 1999 19:30:10 EST -05:00", twz.inspect
  553 + assert_equal "Fri, 31 Dec 1999 19:30:59 EST -05:00", twz.end_of_minute.inspect
  554 + end
  555 +
542 556 def test_since
543 557 assert_equal "Fri, 31 Dec 1999 19:00:01 EST -05:00", @twz.since(1).inspect
544 558 end

3 comments on commit 6f380d3

Xavier Noria
Owner

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

Gagan Awhad

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

Gagan Awhad

Added to the guide in this commit: rails/docrails@f7a0a95

Gert Goet

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

Gagan Awhad
Gert Goet

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

Carlos Antonio da Silva

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

Please sign in to comment.
Something went wrong with that request. Please try again.