* * *

Author Topic: TIdSNTP problem  (Read 216 times)

mrdebug

  • Full Member
  • ***
  • Posts: 156
TIdSNTP problem
« on: September 08, 2017, 09:54:18 am »
I'm using these lines of code
Code: Pascal  [Select]
  1.       TSNTPServer.Host:= 'time.windows.com';
  2.       TSNTPServer.Port:= 123;
  3.       TSNTPServer.ReceiveTimeout:= 5000;
  4.       TDTOraInternet:= TSNTPServer.DateTime;
  5.       if MonthsBetween(Now, TDTOraInternet)< 1 then begin
  6.         TMLog.Lines.Append(DateTimeToStr(TDTOraInternet));
  7.       end;
  8.  
to get internet time. The problem is that on Linux its take the time in utc but on Windows the local time.
How can I have always the time in utc?
www.denisgottardello.it
Skype: mrdebug
Videosurveillance and home automation!
http://www.denisgottardello.it/DomusBoss

rvk

  • Hero Member
  • *****
  • Posts: 2666
Re: TIdSNTP problem
« Reply #1 on: September 08, 2017, 10:42:08 am »
Code: Pascal  [Select]
  1. TSNTPServer.Host:= 'time.windows.com';
I take it that's an Indy component TIdSNTP.

That should always return the local time.
Internally it uses NTPToDateTime() function in TIdSNTP to correct the retrieved utc time to the local time.
Code: Pascal  [Select]
  1.   Result := ((Value1 + Value2) / 86400) - TimeZoneBias + 2;

On Windows idGlobal.pas uses GetTimeZoneInformation to get the local Timezone information to correct the utc time. Somehow either the Linux part of the function OffsetFromUTC doesn't work correctly or you have the wrong timezone information set in your Linux installation.

Remy Lebeau

  • Sr. Member
  • ****
  • Posts: 322
    • Lebeau Software
Re: TIdSNTP problem
« Reply #2 on: September 13, 2017, 01:26:01 am »
On Windows idGlobal.pas uses GetTimeZoneInformation to get the local Timezone information to correct the utc time. Somehow either the Linux part of the function OffsetFromUTC doesn't work correctly or you have the wrong timezone information set in your Linux installation.

TimeZoneBias() is a function in Indy's IdGlobalProtocols unit.

On most compilers and platforms, TimeZoneBias() is just a wrapper for returning the negative of the return value of the OffsetFromUTC() function in the IdGlobal unit:

Code: [Select]
Result := -OffsetFromUTC;

However, on *Nix systems, depending on how Indy is configured during compiling, OffsetFromUTC() will EITHER:

- use gettimeofday() and calculate an offset from it.

- use the global IdGlobal.GOffsetFromUTC variable, which is 0 by default and must be set manually in user code.

Make sure you are not falling into the latter case!
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) open source project - Admin, Developer

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus