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/test | |
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/test')
-rw-r--r-- | activesupport/test/core_ext/date_time_ext_test.rb | 8 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_ext_test.rb | 48 |
2 files changed, 56 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index b1d1e8ecb4..3353465c1c 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -61,6 +61,14 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal 86399,DateTime.civil(2005,1,1,23,59,59).seconds_since_midnight end + def test_seconds_until_end_of_day + assert_equal 0, DateTime.civil(2005,1,1,23,59,59).seconds_until_end_of_day + assert_equal 1, DateTime.civil(2005,1,1,23,59,58).seconds_until_end_of_day + assert_equal 60, DateTime.civil(2005,1,1,23,58,59).seconds_until_end_of_day + assert_equal 3660, DateTime.civil(2005,1,1,22,58,59).seconds_until_end_of_day + assert_equal 86399, DateTime.civil(2005,1,1,0,0,0).seconds_until_end_of_day + end + def test_beginning_of_day assert_equal DateTime.civil(2005,2,4,0,0,0), DateTime.civil(2005,2,4,10,10,10).beginning_of_day end diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 6d6757a1b6..0e75104fc6 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -57,6 +57,54 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end + def test_seconds_until_end_of_day + assert_equal 0, Time.local(2005,1,1,23,59,59).seconds_until_end_of_day + assert_equal 1, Time.local(2005,1,1,23,59,58).seconds_until_end_of_day + assert_equal 60, Time.local(2005,1,1,23,58,59).seconds_until_end_of_day + assert_equal 3660, Time.local(2005,1,1,22,58,59).seconds_until_end_of_day + assert_equal 86399, Time.local(2005,1,1,0,0,0).seconds_until_end_of_day + end + + def test_seconds_until_end_of_day_at_daylight_savings_time_start + with_env_tz 'US/Eastern' do + # dt: US: 2005 April 3rd 2:00am ST => April 3rd 3:00am DT + assert_equal 21*3600, Time.local(2005,4,3,1,59,59).seconds_until_end_of_day, 'just before DST start' + assert_equal 21*3600-2, Time.local(2005,4,3,3,0,1).seconds_until_end_of_day, 'just after DST start' + end + + with_env_tz 'NZ' do + # dt: New Zealand: 2006 October 1st 2:00am ST => October 1st 3:00am DT + assert_equal 21*3600, Time.local(2006,10,1,1,59,59).seconds_until_end_of_day, 'just before DST start' + assert_equal 21*3600-2, Time.local(2006,10,1,3,0,1).seconds_until_end_of_day, 'just after DST start' + end + end + + def test_seconds_until_end_of_day_at_daylight_savings_time_end + with_env_tz 'US/Eastern' do + # st: US: 2005 October 30th 2:00am DT => October 30th 1:00am ST + # avoid setting a time between 1:00 and 2:00 since that requires specifying whether DST is active + assert_equal 24*3600, Time.local(2005,10,30,0,59,59).seconds_until_end_of_day, 'just before DST end' + assert_equal 22*3600-2, Time.local(2005,10,30,2,0,1).seconds_until_end_of_day, 'just after DST end' + + # now set a time between 1:00 and 2:00 by specifying whether DST is active + # uses: Time.local( sec, min, hour, day, month, year, wday, yday, isdst, tz ) + assert_equal 24*3600-30*60-1, Time.local(0,30,1,30,10,2005,0,0,true,ENV['TZ']).seconds_until_end_of_day, 'before DST end' + assert_equal 23*3600-30*60-1, Time.local(0,30,1,30,10,2005,0,0,false,ENV['TZ']).seconds_until_end_of_day, 'after DST end' + end + + with_env_tz 'NZ' do + # st: New Zealand: 2006 March 19th 3:00am DT => March 19th 2:00am ST + # avoid setting a time between 2:00 and 3:00 since that requires specifying whether DST is active + assert_equal 23*3600, Time.local(2006,3,19,1,59,59).seconds_until_end_of_day, 'just before DST end' + assert_equal 21*3600-2, Time.local(2006,3,19,3,0,1).seconds_until_end_of_day, 'just after DST end' + + # now set a time between 2:00 and 3:00 by specifying whether DST is active + # uses: Time.local( sec, min, hour, day, month, year, wday, yday, isdst, tz ) + assert_equal 23*3600-30*60-1, Time.local(0,30,2,19,3,2006,0,0,true, ENV['TZ']).seconds_until_end_of_day, 'before DST end' + assert_equal 22*3600-30*60-1, Time.local(0,30,2,19,3,2006,0,0,false,ENV['TZ']).seconds_until_end_of_day, 'after DST end' + end + end + def test_beginning_of_day assert_equal Time.local(2005,2,4,0,0,0), Time.local(2005,2,4,10,10,10).beginning_of_day with_env_tz 'US/Eastern' do |