diff options
author | Mark J. Titorenko <mark@titorenko.net> | 2012-05-04 20:27:32 +0100 |
---|---|---|
committer | Mark J. Titorenko <mark@titorenko.net> | 2012-05-04 20:27:32 +0100 |
commit | 145cc69524c43d24c257be30a7ea458a284d164f (patch) | |
tree | cd91f30179b00b20b158df30b1963e691e0c23d9 /activesupport/lib | |
parent | 7c7fb3a862651d87c4071e40a1799b973f626b11 (diff) | |
download | rails-145cc69524c43d24c257be30a7ea458a284d164f.tar.gz rails-145cc69524c43d24c257be30a7ea458a284d164f.tar.bz2 rails-145cc69524c43d24c257be30a7ea458a284d164f.zip |
added beginning_of_hour support to core_ext calculations for Time and DateTime
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/date_time/calculations.rb | 11 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/calculations.rb | 15 |
2 files changed, 26 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb index 6f3fd416c1..b759dcc824 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -81,6 +81,17 @@ class DateTime 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) + 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 + # 1.9.3 defines + and - on DateTime, < 1.9.3 do not. if DateTime.public_instance_methods(false).include?(:+) def plus_with_duration(other) #:nodoc: diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index d845cd1c77..5743254462 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -218,6 +218,21 @@ class Time change(:hour => 23, :min => 59, :sec => 59, :usec => 999999.999) 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) |