diff options
author | Serguei Filimonov <serguei.filimonov@gmail.com> | 2009-12-11 13:08:00 -0800 |
---|---|---|
committer | Geoff Buesing <gbuesing@gmail.com> | 2009-12-15 08:15:21 -0600 |
commit | 2ae8300489aaaaaa65f50f3557090b265a2dcc6a (patch) | |
tree | bd60f7573422006c15b500862b19b2bef817f843 /activesupport | |
parent | 7b61541ea53c309847753d5553baa2eed9cc6282 (diff) | |
download | rails-2ae8300489aaaaaa65f50f3557090b265a2dcc6a.tar.gz rails-2ae8300489aaaaaa65f50f3557090b265a2dcc6a.tar.bz2 rails-2ae8300489aaaaaa65f50f3557090b265a2dcc6a.zip |
Added #to_i to DateTime in ActiveSupport so #to_yaml works correctly on ActiveRecord models with DateTime attributes.
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/date_time/conversions.rb | 15 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_time_ext_test.rb | 4 |
2 files changed, 17 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb index 5f01bc4fd6..47a31839a6 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -78,7 +78,18 @@ class DateTime # Converts self to a floating-point number of seconds since the Unix epoch def to_f - days_since_unix_epoch = self - ::DateTime.civil(1970) - (days_since_unix_epoch * 86_400).to_f + seconds_since_unix_epoch.to_f + end + + # Converts self to an integer number of seconds since the Unix epoch + def to_i + seconds_since_unix_epoch.to_i + end + + private + + def seconds_since_unix_epoch + seconds_per_day = 86_400 + (self - ::DateTime.civil(1970)) * seconds_per_day end end diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 4341ead488..278c05797b 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -350,6 +350,10 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert_equal 946684800.0, DateTime.civil(1999,12,31,19,0,0,Rational(-5,24)).to_f end + def test_to_i + assert_equal 946684800, DateTime.civil(2000).to_i + end + protected def with_env_tz(new_tz = 'US/Eastern') old_tz, ENV['TZ'] = ENV['TZ'], new_tz |