aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/values/time_zone.rb
diff options
context:
space:
mode:
authorGeoff Buesing <gbuesing@gmail.com>2008-03-17 03:45:32 +0000
committerGeoff Buesing <gbuesing@gmail.com>2008-03-17 03:45:32 +0000
commitb132413885cff785851980dc1740a003109214b7 (patch)
tree074620a90d288f7dc0d291fde30ce763dd79b621 /activesupport/lib/active_support/values/time_zone.rb
parent895487c1224bf85047f317e7bddf4f2abdea0822 (diff)
downloadrails-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/values/time_zone.rb')
-rw-r--r--activesupport/lib/active_support/values/time_zone.rb14
1 files changed, 12 insertions, 2 deletions
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