Reputation: 11
I am working on google app engine. And I am working on something that requires me to do something if the time-difference between the time sent by the client and the time at server is less than 15 second. The code works perfectly when I try it on the test server (on my own machine). But fails when I the code is deployed on app-engine. I am guess that is probably because if the timezone at server is different, there might be few hours added/subtracted when that occurs.
What I basically do is let the client send his timestamp along with so other data. And when the server subsequently needs to calculate the time difference, I subract server's timestamp from the client's.
If the timezone is different then clearly I will run into problems.
I know that one solution to avoid this timezone fiasco is to let the server timestamp both the initial request and subsequent processing later on, but for my application, it is essential that a timer starts ticking right from when the client makes a request and that if 15 seconds have passed, with respect to the client, then no action be taken by the server.
Since I am the developer of both client side and server side I can show you what I have done.
Sample client side call
new Register().sendToServer(username,location,new TimeStamp(new Date().getTime()));
this is stored in data-store and retrieved a little bit later, if some conditions are met.
Server side sample to find difference
Date date= new Timestamp(new Date().getTime());
Date d1 = (Date) timeStampList[i];
long timedif = Math.abs(d1.getTime() - date.getTime());
if(timedif <= 15000)
do something;
else
do something else;
So basically, my question is how do I normalize the timezone variations ?
thanks
Upvotes: 1
Views: 981
Reputation: 13057
As @Diego Basch pointed out if the time difference is in the 30 minutes to full hours magnitude, you should deal with the timezone difference, because your client is not in the same timezone as the server.
You can determine the client timezone in JavaScript with new Date().getTimezoneOffset()
. Note: the offset to UTC is returned in minutes.
On the server you should be able to determine the timezone with Calendar.getInstance().getTimeZone()
.
Upvotes: 0
Reputation: 80340
Simpy use absolute unix time: System.currentTimeMillis()
.
This time is absolute, i.e. no of miliseconds since Jan 1st, 1970, UTC midnight), so you can simply calculate the difference.
Upvotes: 1