[fpc-pascal] How to get UTC time
Hello, I work on a program wich need the UTC time, and this program will run with Windows and Linux. So how is it possible to calculate the UTC time under these two OS. For example, the Now function with Windows, give me local time. Best regards -- Michel Meunier Web: www.etoiles-a-bleau.fr ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to get UTC time
On Tuesday 13 February 2007 03:37, Michel Meunier wrote: I work on a program wich need the UTC time, and this program will run with Windows and Linux. So how is it possible to calculate the UTC time under these two OS. Hello, Michel. I do not program under Windows, so I cannot answer that part of the question, but getting the UTC time under Linux is quite easy. I have written a utc_now function which I use under both Kylix and Free Pascal. If you can find the answer for Windows, then it should be easy enough to combine the two into one function with an {$ifdef }. There may be (and there probably are) better ways of doing it, but this works for me. It needs both the SysUtils and Libc units, which I'm already using for other declarations anyway. // // Routine: utc_now // // Purpose: Returns the current UTC time as a TDateTime value. // function utc_now : TDateTime; var timeval: TTimeVal; timezone: PTimeZone; a: Double; begin TimeZone := nil; GetTimeOfDay (TimeVal, TimeZone); // Convert to milliseconds a := (TimeVal.tv_sec * 1000.0) + (TimeVal.tv_usec / 1000.0); Result := (a / MSecsPerDay) + UnixDateDelta; end; I hope this helps, Pete C. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to get UTC time
for me. It needs both the SysUtils and Libc units, which I'm already using for other declarations anyway. Libc is a legacy unit, better use the proper (portable) units, and you'll spare yourself a libc dependancy, AND make it portable across unices: {$mode Delphi} uses unix,sysutils,baseunix; function utc_now : TDateTime; var timeval: TTimeVal; timezone: PTimeZone; a: Double; begin TimeZone := nil; fpGetTimeOfDay (@TimeVal, TimeZone); // Convert to milliseconds a := (TimeVal.tv_sec * 1000.0) + (TimeVal.tv_usec / 1000.0); Result := (a / MSecsPerDay) + UnixDateDelta; end; ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to get UTC time
On Tuesday 13 February 2007 14:25, Marco van de Voort wrote: for me. It needs both the SysUtils and Libc units, which I'm already using for other declarations anyway. Libc is a legacy unit, better use the proper (portable) units, and you'll spare yourself a libc dependancy, AND make it portable across unices: However, to compile in Kylix as well (which I believe my post stated I need) then there would have to be a bunch of ifdefs spread around since the 'unix' and 'baseunix' units don't exist there (and consequently, there is no fpGetTimeOfDay function). Also, since the original questioner wanted it to work under Windows, there would be even more ifdefs. It makes me glad that I only have to deal with Kylix and FPC under Linux. :) To answer that how to do this under Windows part of the original question, it looks like it's just as easy. The GetSystemTime API routine returns the current date and time in UTC form. There's a function in SysUtils to convert the TSystemTime into a TDateTime, so the following should work. I only tested it in Delphi 5 under Wine, because that's all I have on this system for such things: {$ifdef MSWINDOWS} { This uses SysUtils and Windows. } function emt_now: TDateTime; var st: TSystemTime; begin GetSystemTime (st); Result := SystemTimeToDateTime (st); end; {$endif} Adding this to the previous bit of code, and properly supporting compilation with FPC under *nix and Windows, as well as Delphi and Kylix, is left as an exercise for the interested reader. ;-) Best regards, Pete C. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal