0

My server is located in country A and I am getting a UTC timestamp from server in country B. I also receive the timezone offset and daylight savings from that server.

What I need to do is calculate the difference (in seconds) between server A local time and the received timestamp from server B.

My current solution is (implemented on server A) :

long localUTCTimestamp = System.currentTimeMillis() / 1000;
long timeDiff = localUTCTime - serverBUTCTime;

My question is should I take daylight savings in account here ? or is this solution should work regardless.

EDIT:

The time from server B is a timestamp of an event the happened some time ago

giorashc
  • 13,691
  • 3
  • 35
  • 71
  • First you need to check where the server B time is including day-light saving ? If no, you should take into account – Ved Mar 14 '14 at 09:15
  • If you have UTC time stamps from both servers, the difference should be sufficient. – Thorbjørn Ravn Andersen Mar 14 '14 at 09:15
  • @ThorbjørnRavnAndersen so the solution above should give me the correct difference ? I have no control on server B I just know that it sends its local time in UTC – giorashc Mar 14 '14 at 09:17
  • 2
    @Ved Why? UTC has a precise definition - in particular there is not time change due to day light saving, ever. – assylias Mar 14 '14 at 09:17
  • @giorashc see also: http://stackoverflow.com/a/5495816/829571 – assylias Mar 14 '14 at 09:18
  • 2
    UTC is UTC, minor discrepancies in the server clocks aside, `localUTCTime - serverBUTCTime` would equal zero. The U stands for Universal – Nick Holt Mar 14 '14 at 09:19
  • @NickHolt you a right, I should have specified the the time from server B is of an event that happened some time ago (usually a few seconds) – giorashc Mar 14 '14 at 09:23
  • @giorashc in that case, the difference between an event that occurred a 10:00 UTC and a current time of 10:01 UTC is simply the later subtracted from the former or 1 minute. There's no daylight saving to worry about – Nick Holt Mar 14 '14 at 09:26
  • UTC is a global time. Local time is UTC interpreted with regard to timezone and daylight savings time etc. – Thorbjørn Ravn Andersen Mar 14 '14 at 09:27
  • You should, however, be certain that both servers synchronize against timeservers (typically with NTP), so their clocks are accurate. – Thorbjørn Ravn Andersen Mar 14 '14 at 09:28
  • @ThorbjørnRavnAndersen I have no control nor access to that information on server B. I'll have to check it once we start testing the whole thing. Thank you for the help! – giorashc Mar 14 '14 at 09:31
  • Ok, then as I understand the solution I provided should work and give me the correct difference (in case server B is indeed synchronized as mentioned by ThorbjørnRavnAndersen). I'll keep the TZ and DST on a shelf for now;). Thank you all for your help! – giorashc Mar 14 '14 at 09:32

0 Answers0