Reputation: 39
I'm using JavaScript and trying to get GMT+0/UTC+0 time (otherwhise called Zulu time) in Unix format.
I've managed to get my local time in Unix format like this:
var datetime = new Date()
var unixtime = Math.floor(Number(datetime/1000));
but when I try to do the same with UTC time...
var datetime = new Date()
var year = datetime.getUTCFullYear()
var month = datetime.getUTCMonth()
var day = datetime.getUTCDate()
var hours = datetime.getUTCHours()
var minutes = datetime.getUTCMinutes()
var seconds = datetime.getUTCSeconds()
var unixtime = (Date.UTC(year,month,day,hours,minutes,seconds,00)/1000);
it fails. I simply get my local time in Unix format.
You can run it live here: http://jsfiddle.net/wC8XH/1/ (Also on pastebin: http://pastebin.com/uDD5zUah)
This is an example of output:
2012-01-30 23:15:19 = 1327958119
2012-01-30 21:15:19 = 1327958119
Doing:
date -d "2012-01-30 21:15:19" +%s
on Linux gives me 1327950919, not 1327958119. The difference is 7200 seconds, which is 2 hours, which is my timezone (+0200).
So I can get UTC+0 time if I simply want it in human readable format but when I request Date.UTC to convert that into Unix format, it instead chooses to convert my local time.
Am I missing something?
Upvotes: 2
Views: 1859
Reputation: 340903
First of all, as @tialaramex already stated, both JavaScript code snippets are correct, so you should prefer the much shorter one.
The problem is with Linux date
command. When you pass a date, it interprets it in your current time zone, not in UTC! Look:
$ date -d "2012-01-30 21:15:19" +%s
1327954519
$ scala
scala> new java.util.Date(1327954519 * 1000L)
res0: java.util.Date = Mon Jan 30 21:15:19 CET 2012
I live in CET, note that date
output is different than yours.
UPDATE: following the great suggestion of @derobert below in comments you can specify the time zone explicitly when invoking date
:
$ date -d "2012-01-30 21:15:19+0000" +%s
1327958119
$ scala
scala> new java.util.Date(1327958119 * 1000L)
res0: java.util.Date = Mon Jan 30 22:15:19 CET 2012
21:15 in UTC is 22:15 CET, everything in order.
Upvotes: 2
Reputation: 3801
Yes, you have the problem upside down.
Javascript is always telling you the number of seconds in "Unix format" as UTC or GMT. Because Unix always works internally in UTC or GMT. It doesn't make sense to speak about the "Unix time" without it being UTC.
So your two Javascript code fragments are finding out the same correct answer (What's the current "unix time"?) in a simple way and a very long-winded complicated way, that's why they give the same result.
... and the date command is locale sensitive. In your locale, you have a 2 hour offset, so it assumes your date string has a 2 hour offset. You can tell it not to behave that way by using the -u flag, which means you promise your strings are in UTC time.
Here are some examples, by setting the TZ environment variable the apparent time zone of the provided strings is altered (the values of TZ shown are for a common Unix timezone file, the names available on your specific system may vary).
$ TZ="America/New_York" date -d "2012-01-30 21:15:19" +%s
1327976119
$ TZ="Europe/Paris" date -d "2012-01-30 21:15:19" +%s
1327954519
$ date -u -d "2012-01-30 21:15:19" +%s
1327958119
Upvotes: 3