diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2012-12-11 06:32:28 +0000 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2012-12-11 06:32:28 +0000 |
commit | 331a82a1c8ec25cd8df3297b434cbb7cc020207a (patch) | |
tree | 7f54ce727d850323413ab0fe6e109359d9549a72 /activesupport/lib/active_support/core_ext | |
parent | 377692e9410922bc508fafb42602187728d421e7 (diff) | |
download | rails-331a82a1c8ec25cd8df3297b434cbb7cc020207a.tar.gz rails-331a82a1c8ec25cd8df3297b434cbb7cc020207a.tar.bz2 rails-331a82a1c8ec25cd8df3297b434cbb7cc020207a.zip |
Add String#in_time_zone method
This commit adds a convenience method for converting a string to an
ActiveSupport::TimeWithZone instance using the configured Time.zone or
another passed as an argument.
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r-- | activesupport/lib/active_support/core_ext/string.rb | 1 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/zones.rb | 13 |
2 files changed, 14 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/string.rb b/activesupport/lib/active_support/core_ext/string.rb index ad864765a3..5d7cb81e38 100644 --- a/activesupport/lib/active_support/core_ext/string.rb +++ b/activesupport/lib/active_support/core_ext/string.rb @@ -11,3 +11,4 @@ require 'active_support/core_ext/string/exclude' require 'active_support/core_ext/string/strip' require 'active_support/core_ext/string/inquiry' require 'active_support/core_ext/string/indent' +require 'active_support/core_ext/string/zones' diff --git a/activesupport/lib/active_support/core_ext/string/zones.rb b/activesupport/lib/active_support/core_ext/string/zones.rb new file mode 100644 index 0000000000..e3f20eee29 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/string/zones.rb @@ -0,0 +1,13 @@ +require 'active_support/core_ext/time/zones' + +class String + # Converts String to a TimeWithZone in the current zone if Time.zone or Time.zone_default + # is set, otherwise converts String to a Time via String#to_time + def in_time_zone(zone = ::Time.zone) + if zone + ::Time.find_zone!(zone).parse(self) + else + to_time + end + end +end |