diff options
author | Grzegorz Witek <arnvald.to@gmail.com> | 2015-11-15 01:04:28 +0800 |
---|---|---|
committer | Grzegorz Witek <arnvald.to@gmail.com> | 2016-01-22 09:25:28 +0800 |
commit | a3ddd5f1572f6ebf95f4c0a789413ee1b2ecbab5 (patch) | |
tree | 07ab0ac46ff35676fa754989ee031b6a93eb70be /activesupport/lib/active_support/json | |
parent | 9a99c7cef533e985e67af5de0e65a39f452b7db9 (diff) | |
download | rails-a3ddd5f1572f6ebf95f4c0a789413ee1b2ecbab5.tar.gz rails-a3ddd5f1572f6ebf95f4c0a789413ee1b2ecbab5.tar.bz2 rails-a3ddd5f1572f6ebf95f4c0a789413ee1b2ecbab5.zip |
Use correct timezone when parsing date in json
Fixes https://github.com/rails/rails/issues/22171
Time specified in ISO 8601 format without `Z` should be considered as local
time, yet until now it was treated as UTC.
This commit fixes problem by parsing time using timezone specified in
application config.
The downside of this solution is performance hit (`Time.zone.parse` is ~ 1.6x
slower than `Time.parse`), so maybe there's a better solution.
Diffstat (limited to 'activesupport/lib/active_support/json')
-rw-r--r-- | activesupport/lib/active_support/json/decoding.rb | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb index 2932954f03..64e4b0e7a9 100644 --- a/activesupport/lib/active_support/json/decoding.rb +++ b/activesupport/lib/active_support/json/decoding.rb @@ -8,7 +8,8 @@ module ActiveSupport module JSON # matches YAML-formatted dates - DATE_REGEX = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[T \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?))$/ + DATE_REGEX = /^\d{4}-\d{2}-\d{2}$/ + DATETIME_REGEX = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[T \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?)?)$/ class << self # Parses a JSON string (JavaScript Object Notation) into a hash. @@ -48,7 +49,13 @@ module ActiveSupport nil when DATE_REGEX begin - DateTime.parse(data) + Date.parse(data) + rescue ArgumentError + data + end + when DATETIME_REGEX + begin + Time.zone.parse(data) rescue ArgumentError data end |