Calculation of the time difference between the server and the client with different time zones

I have a client application that works with JavaScript. and a server application working with Java. When a client performs a specific action, I use it Date.getTime()to record the time it took to complete and send it to the server. Somewhere along the way, I want to calculate how much time has passed since the user performed this action so far - and I want to do this calculation on the server side.

My problem - the difference that I get between the current server time and the action time is big, big. probably due to time differences.

I want this calculation to be correct, regardless of where the client or server is actually. I know that I have to use GMT / UTC, but I just can't find the right way to do this.

+4
source share
2 answers

As Wakey said, it is impractical to trust a client’s watch. And not necessary in your case.

Your second suggestion:

When a client performs a specific action, I use Date.getTime () to register the time it took to execute and send it to the server.

... has a downside. The server application must manage the log and send faxes to the client.

, Java, Joda-Time, UTC. ISO 8601 . , , epoch - Unix, .

, - . , . - - .

, , , .

- , : .

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

org.joda.time.DateTime now = new org.joda.time.DateTime(); // Using host system clock and default time zone.
org.joda.time.DateTime utc = now.toDateTime( org.joda.time.DateTimeZone.UTC );
System.out.println( "Server default local time in ISO 8601 format: " + now );
System.out.println( "UTC (Zulu) time zone: " + utc );

...

Server default local time in ISO 8601 format: 2013-11-08T19:50:56.990-08:00
UTC (Zulu) time zone: 2013-11-09T03:50:56.990Z

, .

Joda-Time ISO 8601. .

java.util.Date -, Joda-Time Date.

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

org.joda.time.DateTimeZone romeTimeZone = org.joda.time.DateTimeZone.forID("Europe/Rome");
org.joda.time.DateTime userDateTime = utc.toDateTime( romeTimeZone );
System.out.println( "In User time zone: " + userDateTime );

...

In User time zone: 2013-11-09T04:50:56.990+01:00

Joda-Time :

    // Joda-Time - The popular alternative to Sun/Oracle notoriously bad date, time, and calendar classes bundled with Java 7 and earlier.
    // http://www.joda.org/joda-time/

    // Joda-Time will become outmoded by the JSR 310 Date and Time API introduced in Java 8.
    // JSR 310 was inspired by Joda-Time but is not directly based on it.
    // http://jcp.org/en/jsr/detail?id=310

    // By default, Joda-Time produces strings in the standard ISO 8601 format.
    // https://en.wikipedia.org/wiki/ISO_8601

    // About Daylight Saving Time (DST): https://en.wikipedia.org/wiki/Daylight_saving_time

    // Time Zone list: http://joda-time.sourceforge.net/timezones.html

. . . , , , ( ).

+1

UTC, (Gregorian) Calendar:

Calendar cal = new GregorianCalendar();
System.out.println("UTC ms from the epoch: " + cal.getTimeInMillis());
0

All Articles