Skip to content
This repository

Unable to find timezone abbreviation from Date.prototype.toString() #162

Closed
cid85 opened this Issue February 06, 2012 · 15 comments

6 participants

Dominic Reich Tim Wood Albin Jacobsson Brave Dick korg250 Khalid Jebbari
Dominic Reich

Hello I came across the moment.js site via a forum and clicked my way through it, when I came across the unit test - which failed 2 tests what I want to report here:

UserAgent:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7 (Chrome 16.0.912.77 m)

Timezone:
UTC+1

Failed tests (# 28):
http://pastebin.com/bQsDuxdH

*Title edited by timrwood

Tim Wood
Owner

Hmm, looks like the timezone abbreviation is failing. Sorry to do remote debugging, but what is the output of the following in chrome?

new Date().toString()

Also, what is the name of the timezone you are in?

Albin Jacobsson

I get the same error in the unit-tests and I'm in UTC+1 (previously known as GMT+1) as well.

new Date().toString() prints "Sat Feb 11 2012 02:15:10 GMT+0100" in Firefox 10, "Sat Feb 11 2012 02:18:29 GMT+0100 (W. Europe Standard Time)" in Chrome 16 and "Sat Feb 11 02:18:59 UTC+0100 2012" in Internet Explorer 9

Tim Wood
Owner

Hmm, I was afraid of this. The problem is that Date.prototype.toString returns such different results. That is the only place to get the timezone name (PST, CST, EST, etc). If this method is not returning any timezone information (as is the case with FF10 and IE9, there is no way to get it.

Perhaps it's time to depreciate? Or is it acceptable to just note the issue in the docs?

Brave Dick

4318 passed, 2 failed. 2715 milliseconds.

Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11

UTC +2 (Kyiv, Ukraine)

format timezone 2 passed,2 failed.
---> Something like "PST"
AssertionError: ---> Something like "PST"
at Object.ok (http://momentjs.com/js/tests.js?_=120203_183019:1526:39)
at http://momentjs.com/js/tests.js?_=120203_183019:2413:14
at Object.runTest (http://momentjs.com/js/tests.js?_=120203_183019:1702:9)
at http://momentjs.com/js/tests.js?_=120203_183019:1748:25
at http://momentjs.com/js/tests.js?_=120203_183019:1006:13
at http://momentjs.com/js/tests.js?_=120203_183019:616:13
at http://momentjs.com/js/tests.js?_=120203_183019:627:25
at http://momentjs.com/js/tests.js?_=120203_183019:1008:17
at http://momentjs.com/js/tests.js?_=120203_183019:1589:17
---> Something like "PST"
AssertionError: ---> Something like "PST"
at Object.ok (http://momentjs.com/js/tests.js?_=120203_183019:1526:39)
at http://momentjs.com/js/tests.js?_=120203_183019:2414:14
at Object.runTest (http://momentjs.com/js/tests.js?_=120203_183019:1702:9)
at http://momentjs.com/js/tests.js?_=120203_183019:1748:25
at http://momentjs.com/js/tests.js?_=120203_183019:1006:13
at http://momentjs.com/js/tests.js?_=120203_183019:616:13
at http://momentjs.com/js/tests.js?_=120203_183019:627:25
at http://momentjs.com/js/tests.js?_=120203_183019:1008:17
at http://momentjs.com/js/tests.js?_=120203_183019:1589:17
+02:00 ---> Something like "+07:30"
+0200 ---> Something like "+0700"

korg250

I also get the same failing test # 28. My timezone is GMT -3.

Agent:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11

Failed test: http://pastebin.com/dsWHMF3h

.:.

Agent:
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
or
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; Zune 4.7; .NET4.0E)

Failed test: http://pastebin.com/6t6JP9fu

Tim Wood
Owner

So looks like this is happening in UTC+1, UTC+2, and UTC-3.

Unfortunately, there is now way to fix this. It depends on hacking apart the Date().toString() output, and if there is no timezone information available in that string, there is no way to get that information.

I'm going to depreciate this feature and maybe make a plugin that has more reliable (although perhaps less accurate) results. It probably should be something like https://github.com/mde/timezone-js.

Tim Wood timrwood referenced this issue April 10, 2012
Closed

failed tests #271

Tim Wood
Owner

The z zz tokens are being deprecated as of 1.6.0. I'm closing this issue as a result.

Tim Wood timrwood closed this April 23, 2012
Khalid Jebbari

What about node.js ? Are z zz reliable or not in this environment ?

Tim Wood
Owner

They have been officially deprecated, so they will not work as of version 1.6.0.

For versions of code before that, I imagine the results would also be inconsistant, as Node uses V8 and some of the inconsistant results above are in Chrome, which also uses V8.

Khalid Jebbari

Thanks. So for a string like Thu Jan 10 2013 22:54:11 GMT+0100 (CET), what I ended up doing is removing with a regexp the timezone string (here (CET) with the leading space) and leaving only the timeshift info (here +0100). Do you think it's enough for moment.js or do I risk inconsistencies ?

Tim Wood
Owner

Hmm, I'm not sure what you are trying to do, are you trying to get CET or +0100?

+0100 is supported via the ZZ token (note the capitalization) and works fine when parsing and formatting.

CET was deprecated as we couldn't reliably get it from Date.toString. However, as we use Date.getTimezoneOffset for +0100, the support is much more consistant.

Khalid Jebbari

I'm doing as you say, using ZZ for +0100 and removing CET from the string to parse. Does it make sense ?

Tim Wood
Owner

My apologies, I'm still not understanding what the problem is here. Are you trying to parse Thu Jan 10 2013 22:54:11 GMT+0100 (CET)? You should be able to just do the following.

moment('Thu Jan 10 2013 22:54:11 GMT+0100 (CET)', 'ddd MMM DD YYYY HH:mm:ss [GMT]ZZ');
Khalid Jebbari

This is exactly what I'm doing at the end. It looks like you're confirming that I'm doing the right thing. My apologies If I wasn't clear, thanks for the answers !

Tim Wood
Owner

No problem!

Cosmo Wolfe templaedhel referenced this issue from a commit in Clever/gearman-admin February 28, 2014
Use 12 hour time and add timezone string cd49a7e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.