Browse files

Modify TimeWithZone#as_json to return 3DP of sub-second accuracy by d…

…efault, since it's allowed by the spec and is very useful.
  • Loading branch information...
jamesotron committed Jan 31, 2013
1 parent 162f7c1 commit 28ab79d7c579fa1d76ac868be02b38b02818428a
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##

* Modify `TimeWithZone#as_json` to include 3 decimal places of sub-second accuracy
by default, which is optional as per the ISO8601 spec, but extremely useful. Also
the default behaviour of Date#toJSON() in recent versions of Chrome, Safari and

* Improve `String#squish` to handle Unicode whitespace. *Antoine Lyset*

* Standardise on `to_time` returning an instance of `Time` in the local system timezone
@@ -154,7 +154,7 @@ def xmlschema(fraction_digits = 0)
# # => "2005/02/01 15:15:10 +0000"
def as_json(options = nil)
if ActiveSupport::JSON::Encoding.use_standard_json_time_format
%(#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
@@ -75,7 +75,7 @@ def test_to_json_with_use_standard_json_time_format_config_set_to_false

def test_to_json_with_use_standard_json_time_format_config_set_to_true
old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, true
assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(@twz)
assert_equal "\"1999-12-31T19:00:00.000-05:00\"", ActiveSupport::JSON.encode(@twz)
ActiveSupport.use_standard_json_time_format = old

3 comments on commit 28ab79d


This comment has been minimized.

Copy link

palexvs replied Nov 5, 2013

Is there some right way to return old behavior: exclude milliseconds?


This comment has been minimized.

Copy link

dmathieu replied Nov 5, 2013

The only way is to monkey-patch TimeWithZone to change the behavior of it's as_json method.


This comment has been minimized.

Copy link

twonegatives replied Sep 8, 2014

for those who use rails 4.1:
you can set ActiveSupport::JSON::Encoding.time_precision = 0 to return the old behavior

Please sign in to comment.