0

I am developing an application to get the local time of city. I am using google Geocode API to get the latitude and longitude of a city and the same is passed to the timezone API.

Time Zone API return the following

{
"dstOffset" : 0,
"rawOffset" : 0,
"status" : "OK",
"timeZoneId" : "Europe/London",
"timeZoneName" : "Greenwich Mean Time"
}

I need to calculate the time for the following. From this SO answer, I got the following code to get the local time from rawOffset.

function calcTime(offset) {
var d = new Date();
var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
var nd = new Date(utc + (3600000*offset));

alert("The local time is " + nd.toLocaleString());
}
calcTime(-28800.0);

But this is having one hour delay in time for cities like London, Washington, NewYork and returns correct time for Arizona.

Also. I am not aware of this relation between local time and rawOffset parameter to calculate the time. Please explain what this rawOffset parameter is and how to fix this one hour time delay for some cities.

In Google TimeZone API Documentation, I read that

The local time of a given location is the sum of the timestamp parameter, and the dstOffset and rawOffset fields from the result.

What is the timestamp parameter here? I couldn't give anything other than 1331161200.

Any help is greatly appreciated. Thanks in advance.

UPDATE

FIX : This is because I am passing timestamp parameter as 1331161200 in the timezone API. When I replaced timestamp with the following code, I am getting correct response from the Google APIs with dstOffset and rawOffset.

myDate = new Date('1/1/1970');
timeStamp = myDate.getTime();

Can anyone give me the technical explanation of how it worked?

Community
  • 1
  • 1
Vishnu Sureshkumar
  • 2,246
  • 6
  • 35
  • 52

1 Answers1

2

You haven't considered the Daylight Savings Time offset, which is why certain cities are off by an hour.

calcTime(rawOffset+dstOffset);

Timestamp contains both the date and time for as DST is only occurs in certain periods of the year. https://stackoverflow.com/a/10428184/3583980

Community
  • 1
  • 1
riddler
  • 467
  • 3
  • 13
  • The dstOffset returned is also '0'. Consider for Washington the rawOffset returned now is -18000 and dstOffset is 0 and the calcTime return 4:13AM but the actual time is 5:26AM – Vishnu Sureshkumar May 13 '15 at 09:27
  • I suspect it's only the date that's giving DST confusion. Encode today's date and time using http://www.esqsoft.com/javascript_examples/date-to-epoch.htm and put this in for the timestamp for the API. – riddler May 13 '15 at 09:55
  • I have updated the question, please check. The issue is fixed. This was due to the timestamp I passed to the timezone API. – Vishnu Sureshkumar May 13 '15 at 10:25
  • You had hardcoded a date that occurred in the past (Wed Mar 07 2012 23:00:00 GMT+0000 (GMT Standard Time)) with no DST as the timestamp. `myDate = new Date(); timeStamp = myDate.getTime();` – riddler May 13 '15 at 10:27
  • It is not due to the past, I guess. The TimeStamp must be for 1/1/1970. When I did new Date('1/1/1970') it worked. So, is the timestamp browser dependent.? I think the timestamp that I hard coded is not for my timezone which caused the issue. – Vishnu Sureshkumar May 13 '15 at 10:30
  • myDate = new Date(); timeStamp = myDate.getTime(); gives me an error from the API. The timestamp must be for 1/1/1970 – Vishnu Sureshkumar May 13 '15 at 10:32
  • You don't need to specify a value when creating the myDate object as `myDate.getTime()` sets the value of myDate to the current epoch (see the link I posted to encode/decode timestamp. – riddler May 13 '15 at 10:33
  • Thanks! The epoch value from the link got worked. Is there any script to get the current epoch? – Vishnu Sureshkumar May 13 '15 at 10:35
  • It's getTime() as mentioned in the answer I linked to http://stackoverflow.com/a/10428184/3583980 – riddler May 13 '15 at 10:41