diff options
author | Olek Janiszewski <olek.janiszewski@gmail.com> | 2012-10-16 12:05:24 +0200 |
---|---|---|
committer | Olek Janiszewski <olek.janiszewski@gmail.com> | 2012-11-29 06:39:32 +0100 |
commit | ce73055139013d44b36d0c2cd3ab42146c0b4353 (patch) | |
tree | c9bd2f91ac22e087d44569215e3e62a9d520e108 /activesupport/lib | |
parent | ec17f0d3521876a5b8c14afa0bd9154c6d194c16 (diff) | |
download | rails-ce73055139013d44b36d0c2cd3ab42146c0b4353.tar.gz rails-ce73055139013d44b36d0c2cd3ab42146c0b4353.tar.bz2 rails-ce73055139013d44b36d0c2cd3ab42146c0b4353.zip |
Add #seconds_until_end_of_day to DateTime and Time
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/date_time/calculations.rb | 9 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/calculations.rb | 9 |
2 files changed, 18 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 0c6437b02b..f77d444479 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -32,6 +32,15 @@ class DateTime sec + (min * 60) + (hour * 3600) end + # Returns the number of seconds until 23:59:59. + # + # DateTime.new(2012, 8, 29, 0, 0, 0).seconds_until_end_of_day # => 86399 + # DateTime.new(2012, 8, 29, 12, 34, 56).seconds_until_end_of_day # => 41103 + # DateTime.new(2012, 8, 29, 23, 59, 59).seconds_until_end_of_day # => 0 + def seconds_until_end_of_day + end_of_day.to_i - to_i + end + # Returns a new DateTime where one or more of the elements have been changed # according to the +options+ parameter. The time options (<tt>:hour</tt>, # <tt>:minute</tt>, <tt>:sec</tt>) reset cascadingly, so if only the hour is diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 931851d40e..46c9f05c15 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -62,6 +62,15 @@ class Time to_i - change(:hour => 0).to_i + (usec / 1.0e+6) end + # Returns the number of seconds until 23:59:59. + # + # Time.new(2012, 8, 29, 0, 0, 0).seconds_until_end_of_day # => 86399 + # Time.new(2012, 8, 29, 12, 34, 56).seconds_until_end_of_day # => 41103 + # Time.new(2012, 8, 29, 23, 59, 59).seconds_until_end_of_day # => 0 + def seconds_until_end_of_day + end_of_day.to_i - to_i + end + # Returns a new Time where one or more of the elements have been changed according # to the +options+ parameter. The time options (<tt>:hour</tt>, <tt>:min</tt>, # <tt>:sec</tt>, <tt>:usec</tt>) reset cascadingly, so if only the hour is passed, |