diff options
author | Geoff Buesing <gbuesing@gmail.com> | 2008-03-17 03:45:32 +0000 |
---|---|---|
committer | Geoff Buesing <gbuesing@gmail.com> | 2008-03-17 03:45:32 +0000 |
commit | b132413885cff785851980dc1740a003109214b7 (patch) | |
tree | 074620a90d288f7dc0d291fde30ce763dd79b621 /activesupport/lib/active_support | |
parent | 895487c1224bf85047f317e7bddf4f2abdea0822 (diff) | |
download | rails-b132413885cff785851980dc1740a003109214b7.tar.gz rails-b132413885cff785851980dc1740a003109214b7.tar.bz2 rails-b132413885cff785851980dc1740a003109214b7.zip |
Adding TimeZone#at and DateTime#to_f
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9042 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support')
3 files changed, 19 insertions, 3 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 7e78550f7c..9b6d6e5e90 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -78,6 +78,12 @@ module ActiveSupport #:nodoc: def xmlschema strftime("%Y-%m-%dT%H:%M:%S%Z") end if RUBY_VERSION < '1.9' + + # 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 + end end end end diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 10c3543d6e..ca82c2e004 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -160,7 +160,7 @@ module ActiveSupport end unless RUBY_VERSION < '1.9' def to_a - time.to_a[0, 8].push(dst?, zone) + [time.sec, time.min, time.hour, time.day, time.mon, time.year, time.wday, time.yday, dst?, zone] end def to_f diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 401b669a0a..391ebc644e 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -178,7 +178,7 @@ class TimeZone begin # the following methods depend on the tzinfo gem require_library_or_gem "tzinfo" unless Object.const_defined?(:TZInfo) - # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+. Example: + # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from given values. Example: # # Time.zone = "Hawaii" # => "Hawaii" # Time.zone.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00 @@ -186,6 +186,16 @@ class TimeZone time = Time.utc_time(*args) ActiveSupport::TimeWithZone.new(nil, self, time) end + + # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from number of seconds since the Unix epoch. Example: + # + # Time.zone = "Hawaii" # => "Hawaii" + # Time.utc(2000).to_f # => 946684800.0 + # Time.zone.at(946684800.0) # => Fri, 31 Dec 1999 14:00:00 HST -10:00 + def at(secs) + utc = Time.at(secs).utc rescue DateTime.civil(1970).since(secs) + utc.in_time_zone(self) + end # Returns an ActiveSupport::TimeWithZone instance representing the current time # in the time zone represented by +self+. Example: @@ -234,7 +244,7 @@ class TimeZone rescue LoadError # Tzinfo gem is not available # re-raise LoadError only when a tzinfo-dependent method is called: - %w(local now today utc_to_local local_to_utc period_for_utc period_for_local tzinfo).each do |method| + %w(local at now today utc_to_local local_to_utc period_for_utc period_for_local tzinfo).each do |method| define_method(method) {|*args| raise LoadError, "TZInfo gem is required for TimeZone##{method}. `gem install tzinfo` and try again."} end end |