Results 1 to 8 of 8

Thread: Ext.encode() handles Dates badly

    You found a bug! We've classified it as EXTJS-6084 . We encourage you to continue the discussion and to find an acceptable workaround while we work on a permanent fix.
  1. #1
    Sencha User
    Join Date
    Feb 2012
    Posts
    117

    Default Ext.encode() handles Dates badly

    Hello,

    I was wrestling for a long time with writes to my REST server just to discover that Ext.encode() is using a very old version of Crockford's code that does not properly factor in timezones when parsing Date objects.

    Ext.encode(new Date()) will return something of the format:

    Code:
    "2012-04-18T18:49:10"
    This is very wrong! This will get sent up to a server and it can only guess at what to do with it. In our case, the server was assuming this was in UTC time, which meant that when it was sent back it was being converted back to Eastern and we lost 4 hours. It can be worked around by storing the user's timezone server-side and compensating, but why should we have to do this because Ext.encode() doesn't write UTC time?

    JSON.stringify(new Date()) on all modern browsers writes Dates in the format:

    Code:
    "2012-04-18T22:50:51.811Z"
    Notice how it converted to UTC first? This is because it uses Crockford's updated code, which uses date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), etc.

    For now I am setting Ext.USE_NATIVE_JSON = true to get around this, but it is not guaranteed to work on all browsers (e.g. old FF, old IE). Because of this, I must include Crockford's json2.js in my site.

    The code in Crockford's json2.js should be a mostly drop-in replacement for the code in Ext.encode(). Please use it so we can have sane Date parsing.

  2. #2
    Sencha User mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236

    Default

    Or just use this override (and let the server handle timezone conversion):
    Code:
    Ext.JSON.encodeDate = function(o) {
        return Ext.Date.format(o, 'c');
    };
    which when used should give you something like
    Code:
    Ext.encode(new Date()); // returns "2012-04-19T14:05:07+08:00" for me

    [edit]
    if you need millisecond info as well, then use this override:
    Code:
    Ext.JSON.encodeDate = function(o) {
        return Ext.Date.format(o, 'Y-m-d\\TH:i:s.uP');
    };
    which when used gives
    Code:
    Ext.encode(new Date()); // returns "2012-04-19T14:07:49.752+08:00" for me

  3. #3
    Sencha User
    Join Date
    Feb 2012
    Posts
    117

    Default

    Sure. That's similar to the effect you get when setting Ext.USE_NATIVE_JSON, which throws date objects in UTC with milliseconds. The benefit of your code is that it will work in browsers not supporting native JSON, which are thankfully very few.

    The correct solution to this issue IMO is to default Ext.USE_NATIVE_JSON to true so that all browsers supporting native JSON use it, which has numerous speed benefits. And if the browser does not have support (simple check for window.JSON), use Ext's built-in JSON support but fix its Date handling.

    I for one am very surprised this bug has lasted this long - without overriding Ext code, it is not possible to handle Dates correctly!

  4. #4
    Sencha User mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236

    Default

    Quote Originally Posted by Sottilde View Post
    Sure. That's similar to the effect you get when setting Ext.USE_NATIVE_JSON, which throws date objects in UTC with milliseconds. The benefit of your code is that it will work in browsers not supporting native JSON, which are thankfully very few.
    ... but which sadly form the bulk of browsers being used.

    Quote Originally Posted by Sottilde View Post
    The correct solution to this issue IMO is to default Ext.USE_NATIVE_JSON to true so that all browsers supporting native JSON use it, which has numerous speed benefits. And if the browser does not have support (simple check for window.JSON), use Ext's built-in JSON support but fix its Date handling.

    I for one am very surprised this bug has lasted this long - without overriding Ext code, it is not possible to handle Dates correctly!
    Wholeheartedly agree. That code's been there (iirc) since the 2.x / 3.0.x days.

    The only kink would be for upgraders -- they will need to add code to handle the new date strings containing milliseconds + timezones (which they should be doing anyway if they haven't already done so).

  5. #5
    Sencha User
    Join Date
    Feb 2012
    Posts
    117

    Default

    Quote Originally Posted by mystix View Post
    ... but which sadly form the bulk of browsers being used.
    That's an exaggeration: Native JSON is part of Internet Explorer 8+, Firefox 3.1+, Safari 4+, Chrome 3+, and Opera 10.5+. According to W3 and as of March 2012, that leaves the 3.4% of people still on IE <= 7, and statistically insignificant numbers of users using FF < 3.1.

    Quote Originally Posted by mystix View Post
    The only kink would be for upgraders -- they will need to add code to handle the new date strings containing milliseconds + timezones (which they should be doing anyway if they haven't already done so).
    I agree. But Sencha has not been afraid to introduce breaking changes before. A simple flag to revert to the old behavior if needed could cover the awkward cases for a while.

  6. #6
    Sencha User
    Join Date
    Feb 2012
    Posts
    117

    Default

    Devs, any input? I don't want to see this slide into obscurity.

  7. #7
    Sencha Premium Member
    Join Date
    Apr 2012
    Posts
    9

    Default

    I second the opinion that encodeDates needs to encode dates differently. We use a REST api that can handle almost any date format, but can't assume information that isn't present. I think ExtJS should either submit the date in UTC with the 'Z' indicator at the end or include the time zone information.

  8. #8
    Sencha User
    Join Date
    Feb 2012
    Posts
    117

    Default

    This bug is a testament to why I don't use ExtJS anymore - the default behavior is verifiably incorrect, the fix is straightforward, and yet no fix is forthcoming for over a year. My experience with this framework was full of gotchas caused by these sorts of bugs. Handling dates correctly is a basic feature of any platform and should be a priority.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •