diff options
author | Paul A Jungwirth <pj@illuminatedcomputing.com> | 2015-04-02 05:15:38 +0000 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2015-04-22 12:05:07 -0400 |
commit | a5e507fa0b8180c3d97458a9b86c195e9857d8f6 (patch) | |
tree | f365dd94b975e86211821d0e17dae54f62093631 /activesupport/lib/active_support/values | |
parent | 9a034bc3b99cbd219229e88aacf0ec2f53722fe1 (diff) | |
download | rails-a5e507fa0b8180c3d97458a9b86c195e9857d8f6.tar.gz rails-a5e507fa0b8180c3d97458a9b86c195e9857d8f6.tar.bz2 rails-a5e507fa0b8180c3d97458a9b86c195e9857d8f6.zip |
Add ActiveSupport::TimeZone#strptime.
This makes it easier to parse user-inputted times as from a given time zone.
Diffstat (limited to 'activesupport/lib/active_support/values')
-rw-r--r-- | activesupport/lib/active_support/values/time_zone.rb | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index da39f0d245..ceeb9d3e08 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -368,6 +368,26 @@ module ActiveSupport end end + # Parses +str+ according to +spec+ and returns an ActiveSupport::TimeWithZone. + # + # Assumes that +str+ is a time in the time zone +self+, + # unless +spec+ includes an explicit time zone. + # (This is the same behavior as +parse+.) + # In either case, the returned TimeWithZone has the timezone of +self+. + # + # Time.zone = 'Hawaii' # => "Hawaii" + # Time.zone.strptime('1999-12-31 14:00:00', '%Y-%m-%d %H:%M:%S') # => Fri, 31 Dec 1999 14:00:00 HST -10:00 + # + def strptime(str, spec) + case spec + when /(^|[^%](%%)*)%(Z|:{0,3}z)/ + DateTime.strptime(str, spec).in_time_zone(self) + else + t = DateTime.strptime(str, spec) + local(t.year, t.month, t.mday, t.hour, t.min, t.sec, t.usec) + end + end + # Returns an ActiveSupport::TimeWithZone instance representing the current # time in the time zone represented by +self+. # |