Bug 14176 - Some date values not handled consistently with IE/Firefox
Summary: Some date values not handled consistently with IE/Firefox
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 523.x (Safari 3)
Hardware: All All
: P2 Normal
Assignee: Nobody
URL: javascript:var d = new Date("November...
Keywords:
Depends on:
Blocks:
 
Reported: 2007-06-15 13:56 PDT by Arno Gourdol
Modified: 2011-11-30 18:15 PST (History)
4 users (show)

See Also:


Attachments
Patch (4.54 KB, patch)
2011-11-22 15:58 PST, Max Vujovic
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Arno Gourdol 2007-06-15 13:56:13 PDT
The following code snippet does not return the expected result.

var d = new Date("November 06 2006, 17:53:01");
document.write( d + "<br>Above document.write should have written: 'Mon Nov 6 17:53:01 PST 2006' );

The correct answer is returned by IE and FireFox. Instead, the date returned is the string "Invalid Date".
Comment 1 David Gatwood 2008-04-10 19:34:24 PDT
I just ran into a very closely related bug in an existing website that probably subtly impacts a lot of other websites as well.  The following script should show exactly the same date in both cases, and in FireFox and IE, it does.  In Safari, it subtracts four minutes instead of four hours!

Please spend some time on the date parser and fix these issues....

<script language="javascript" type="text/javascript">
        var mydate = new Date("4/13/2008 12:00 AM GMT-4");
        var mydate_b = new Date("4/13/2008 12:00 AM GMT-0400");

        document.write(mydate + ' = ' + mydate_b);
</script>
Comment 2 Max Vujovic 2011-11-22 15:58:24 PST
Created attachment 116287 [details]
Patch

I couldn't reproduce Arno's original bug in r100958. The code snippet outputs "Mon Nov 06 2006 17:53:01 GMT-0800 (PST)" instead of "Invalid Date" for me.

However, I did reproduce David's bug in the second comment. WebKit interprets "GMT-4" as 4 minutes. On the other hand, IE, Firefox, and Opera interpret "GMT-4" as 4 hours. This case is defined as implementation specific in the ECMA Standard (See Section 15.9.4.2), but I think WebKit should match IE, FireFox, and Opera's behavior. 

WebKit's implementation reads in the number after "GMT-" and mods by 100 for the minute offset and divides by 100 for the hour offset for all numbers. On the other hand, IE, FireFox, and Opera only do this if the number is greater than or equal to 24. If the number is less than 24, they interpret it as an hour offset with no minutes.

Interestingly, even "GMT-0004" is interpreted as four hours in IE, FireFox, and Opera.

I used this test script to compare the behaviors:

<script language="javascript" type="text/javascript">
     document.write('"4/13/2008 12:00 AM GMT-4" parsed: ' + new Date('4/13/2008 12:00 AM GMT-4') + '<br/>');
     document.write('"4/13/2008 12:00 AM GMT-23" parsed: ' + new Date('4/13/2008 12:00 AM GMT-23') + '<br/>');
     document.write('"4/13/2008 12:00 AM GMT-023" parsed: ' + new Date('4/13/2008 12:00 AM GMT-023') + '<br/>');
     document.write('"4/13/2008 12:00 AM GMT-0023" parsed: ' + new Date('4/13/2008 12:00 AM GMT-0023') + '<br/>');
     document.write('"4/13/2008 12:00 AM GMT-24" parsed: ' + new Date('4/13/2008 12:00 AM GMT-24') + '<br/>');
     document.write('"4/13/2008 12:00 AM GMT-024" parsed: ' + new Date('4/13/2008 12:00 AM GMT-024') + '<br/>');
     document.write('"4/13/2008 12:00 AM GMT-0024" parsed: ' + new Date('4/13/2008 12:00 AM GMT-0024') + '<br/>');
     document.write('"4/13/2008 12:00 AM GMT-0123" parsed: ' + new Date('4/13/2008 12:00 AM GMT-0123') + '<br/>');              
</script>

The results were:

GMT-4 = 4 hours in IE, FireFox, and Opera vs. 4 minutes in WebKit

GMT-23 = 23 hours in IE, FireFox, and Opera vs. 23 minutes in WebKit
GMT-023 = same as above
GMT-0023 = same as above

GMT-24 = 24 minutes in all implementations (IE, FireFox, Opera, and WebKit)
GMT-024= same as above
GMT-0024 = same as above

GMT-0123 = 1 hour, 23 minutes in all implementations (IE, FireFox, Opera, and WebKit)

I've uploaded a patch that makes WebKit match the IE, FireFox, and Opera behavior.
Comment 3 Gavin Barraclough 2011-11-30 11:21:14 PST
Comment on attachment 116287 [details]
Patch

Looks great.
Comment 4 WebKit Review Bot 2011-11-30 18:15:31 PST
Comment on attachment 116287 [details]
Patch

Clearing flags on attachment: 116287

Committed r101582: <http://trac.webkit.org/changeset/101582>
Comment 5 WebKit Review Bot 2011-11-30 18:15:35 PST
All reviewed patches have been landed.  Closing bug.