aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/values
diff options
context:
space:
mode:
authorGeoff Buesing <gbuesing@gmail.com>2008-03-17 05:07:50 +0000
committerGeoff Buesing <gbuesing@gmail.com>2008-03-17 05:07:50 +0000
commit2366fdbdb1e8a5ba4ee07d94e79c2011f5821820 (patch)
tree953002ac4f33bfa52fbaab3efa879c988ccf42aa /activesupport/lib/active_support/values
parentff8b9e6b08190c45ad1e0b7c33b5687f0b5cf56c (diff)
downloadrails-2366fdbdb1e8a5ba4ee07d94e79c2011f5821820.tar.gz
rails-2366fdbdb1e8a5ba4ee07d94e79c2011f5821820.tar.bz2
rails-2366fdbdb1e8a5ba4ee07d94e79c2011f5821820.zip
Adding TimeZone#parse
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9045 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/values')
-rw-r--r--activesupport/lib/active_support/values/time_zone.rb16
1 files changed, 15 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb
index 391ebc644e..1d33dcbf32 100644
--- a/activesupport/lib/active_support/values/time_zone.rb
+++ b/activesupport/lib/active_support/values/time_zone.rb
@@ -197,6 +197,20 @@ class TimeZone
utc.in_time_zone(self)
end
+ # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from parsed string. Example:
+ #
+ # Time.zone = "Hawaii" # => "Hawaii"
+ # Time.zone.parse('1999-12-31 14:00:00') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
+ #
+ # If upper components are missing from the string, they are supplied from TimeZone#now:
+ #
+ # Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
+ # Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
+ def parse(str, now=now)
+ time = Time.parse(str, now) rescue DateTime.parse(str)
+ ActiveSupport::TimeWithZone.new(nil, self, time)
+ end
+
# Returns an ActiveSupport::TimeWithZone instance representing the current time
# in the time zone represented by +self+. Example:
#
@@ -244,7 +258,7 @@ class TimeZone
rescue LoadError # Tzinfo gem is not available
# re-raise LoadError only when a tzinfo-dependent method is called:
- %w(local at now today utc_to_local local_to_utc period_for_utc period_for_local tzinfo).each do |method|
+ %w(local at parse 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