From ce73055139013d44b36d0c2cd3ab42146c0b4353 Mon Sep 17 00:00:00 2001 From: Olek Janiszewski Date: Tue, 16 Oct 2012 12:05:24 +0200 Subject: Add #seconds_until_end_of_day to DateTime and Time --- .../lib/active_support/core_ext/date_time/calculations.rb | 9 +++++++++ activesupport/lib/active_support/core_ext/time/calculations.rb | 9 +++++++++ 2 files changed, 18 insertions(+) (limited to 'activesupport/lib/active_support') 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 (:hour, # :minute, :sec) 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 (:hour, :min, # :sec, :usec) reset cascadingly, so if only the hour is passed, -- cgit v1.2.3