diff options
author | Mark J. Titorenko <mark@titorenko.net> | 2012-05-04 15:18:59 +0100 |
---|---|---|
committer | Mark J. Titorenko <mark@titorenko.net> | 2012-05-04 16:01:57 +0100 |
commit | 8c3f4bec1fc6b50025f256f5244acbb8e892c9ee (patch) | |
tree | 5e715a3af8d2b4c28277539aad6ed310ee16bf8a /activesupport/lib | |
parent | 510cf0ad93f07e9285178c8b7ba7d4d68c139fec (diff) | |
download | rails-8c3f4bec1fc6b50025f256f5244acbb8e892c9ee.tar.gz rails-8c3f4bec1fc6b50025f256f5244acbb8e892c9ee.tar.bz2 rails-8c3f4bec1fc6b50025f256f5244acbb8e892c9ee.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 0e5aa5af10..020fa1a06d 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -91,6 +91,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 + # Adjusts DateTime to UTC by adding its offset value; offset is set to 0 # # Example: diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 1434e186c3..a0f610d60c 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -257,6 +257,21 @@ class Time ) 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) |