[fpc-pascal] How to get UTC time

Pete Cervasio cervasio at airmail.net
Tue Feb 13 23:50:02 CET 2007

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;
  st: TSystemTime;
  GetSystemTime (st);
  Result := SystemTimeToDateTime (st);

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.

More information about the fpc-pascal mailing list