Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-02

How to get UTC time

10 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michel Meunier

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

Reply by email

Pete Cervasio
to Michel Meunier
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.

Reply by email

Marco van de Voort
to Pete Cervasio
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;

Reply by email

Pete Cervasio
to Marco van de Voort
Marco van de Voort wrote 5 lines
> 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.

Reply by email

josepascual
to Pete Cervasio

Hi Everyone

I have made a example with lua inside freepascal. I have test it in i386 fpc 2.1.4 with no problem
ann in ARM with fpc 2.1.4. I hav a problem:

I have tried to debug it (gdb):

(gdb)
LUATOVARIANT (L=0xdda08, INDEX=-2, result=void) at LuaUtils.pas:438 438 end;
(gdb)
438 end;
(gdb)
LUACOPYTABLE (L=0xdda08, IDXFROM=4, IDXTO=2, MTTO=3) at lua_WrapUp.pas:44 44 if CompareText(key, '__') = 1 then
(gdb)
47 tbl := IdxTo;
(gdb)
48 case lua_type(L, -1) of
(gdb)
50 cf := lua_tocfunction(L, -1);
(gdb)
51 LuaPushVariant(L, key);
(gdb)
LUAPUSHVARIANT (L=0xdda08, N=void) at LuaUtils.pas:443 443 begin
(gdb)
444 case VarType(N) of
(gdb)
450 varString :LuaPushString(L, N); (gdb)
LUAPUSHSTRING (L=0xdda08, S=0x40213180) at LuaUtils.pas:478 478 lua_pushstring(L, PChar(S));
(gdb)
479 end;
(gdb)
LUAPUSHVARIANT (L=0xdda08, N=void) at LuaUtils.pas:464 464 end;
(gdb)

Program received signal SIGSEGV, Segmentation fault. 0x0002a550 in SYSTEM_TRY_CONCAT_FREE_CHUNK_FORWARD$PMEMCHUNK_VAR () (gdb)
Single stepping until exit from function
SYSTEM_TRY_CONCAT_FREE_CHUNK_FORWARD$PMEMCHUNK_VAR, which has no line number information.
0x0002a468 in SYSTEM_CONCAT_TWO_BLOCKS$PMEMCHUNK_VAR$PMEMCHUNK_VAR () (gdb) bt
#0 0x0002a468 in SYSTEM_CONCAT_TWO_BLOCKS$PMEMCHUNK_VAR$PMEMCHUNK_VAR () #1 0x0002a560 in SYSTEM_TRY_CONCAT_FREE_CHUNK_FORWARD$PMEMCHUNK_VAR () 1#2 0x0002a574 in SYSTEM_TRY_CONCAT_FREE_CHUNK$PMEMCHUNK_VAR$$PMEMCHUNK_VAR ()
#3 0x0002acc0 in SYSTEM_SYSFREEMEM_VAR$PMEMCHUNK_VAR$$LONGINT () #4 0x0002ad0c in SYSTEM_SYSFREEMEM$POINTER$$LONGINT () #5 0x00029ad8 in SYSTEM_FREEMEM$POINTER$$LONGINT () #6 0x0001cd0c in fpc_ansistr_decr_ref ()
#7 0x00081c68 in VARIANTS_SYSVARCLEARPROC$TVARDATA () #8 0x0002436c in SYSTEM_VARIANT_CLEAR$TVARDATA () #9 0x0002693c in fpc_finalize ()
#10 0x00069b0c in LUACOPYTABLE (L=0xdda08, IDXFROM=4, IDXTO=2, MTTO=3) at lua_WrapUp.pas:67
#11 0x00069dd8 in NEW_TMYTYPE (L=0xdda08) at lua_WrapUp.pas:125 #12 0x000961bc in luaD_precall ()
#13 0x0009f084 in luaV_execute ()
#14 0x00000000 in ?? ()
========================================================================

Any suggestion?
thank you a lot,

J.P.

Reply by email

Paul Nicholls
to josepascual

"josepascual" wrote in
message

Show quoted text 9 lines
Hi Everyone I have made a example with lua inside freepascal. I have test it in i386 fpc 2.1.4 with no problem ann in ARM with fpc 2.1.4. I hav a problem: I have tried to debug it (gdb):

Hi Jose :-)
I can't help you with your problem, but I am curious...

1) is your ARM system using Linux?
2) for Lua under ARM, are you using dynamic linking or static linking? For dynamic linking, where did you get your Lua 'DLL' from, and for static linking, how did you do this?

I would like to get into using Lua under arm-linux myself (already used Lua and freepacal under Win32 with no problems)
cheers,
Paul.

Reply by email

josepascual
to Paul Nicholls

Hi Paul,

Hi Jose :-) I can't help you with your problem, but I am curious...

thanks anyway,

1) is your ARM system using Linux?

yes,

2) for Lua under ARM, are you using dynamic linking or static linking?

i'm using dynamic linking but you can use static if you want.

For dynamic linking, where did you get your Lua 'DLL' from, and for static linking, how did you do this?

You only need to download lua sources to your arm-linux and make and make install it's so easy

I would like to get into using Lua under arm-linux myself (already used Lua and freepacal under Win32 with no problems)

There is differences between freepascal for linux-i386 and linux-arm because same program compiling for i386 work okey
but for ARM.

I'm testing with a wrapper (Pas2Lua) for freepascal and It work okey in delphi-windows, freepascal-i386 but it does not
work in freepascal for ARM due some problem with heap implementation in fpc-arm.

how are you using (in this way) lua and freepascal in windows? Are you also using Pas2Lua?

regards
Jose Pascual.

Reply by email

Paul Nicholls
to josepascual
Show quoted text 17 lines
Hi Paul, > Hi Jose :-) > I can't help you with your problem, but I am curious... thanks anyway, > 1) is your ARM system using Linux? yes, > 2) for Lua under ARM, are you using dynamic linking or static > linking? i'm using dynamic linking but you can use static if you want. > For dynamic linking, where did you get your Lua 'DLL' from, and for > static > linking, how did you do this? You only need to download lua sources to your arm-linux and make and make install it's so easy

ok...

Show quoted text 17 lines
> I would like to get into using Lua under arm-linux myself (already used > Lua > and freepacal under Win32 with no problems) There is differences between freepascal for linux-i386 and linux-arm because same program compiling for i386 work okey but for ARM. I'm testing with a wrapper (Pas2Lua) for freepascal and It work okey in delphi-windows, freepascal-i386 but it does not work in freepascal for ARM due some problem with heap implementation in fpc-arm. how are you using (in this way) lua and freepascal in windows? Are you also using Pas2Lua?

I think I know the pas2lua you are talking about - created by jdarling (www.eonclash.com)? who lurks in the www.pascalgamedevelopment.com site... I haven't used that myself, but I did make my own unit with helper functions, and a class that encapsulates a lua state so I can easily call lua procedures, etc. and also call Delphi procedures from Lua.

cheers,
Paul.

Reply by email

Paul Nicholls
Show quoted text 6 lines
Hi Paul, I'm having problem with wrapper implementation from jdarling, It's working in Delphi (windows) and Fpc (linux-i386) but I'm having problem with heap with fpc (linux-arm).

For any information pertaining to the pas2lua wrapper stuff you are trying from jdarling, you could ask in the www.pascalgamedevelopment.com forums :-)

Show quoted text 14 lines
> I think I know the pas2lua you are talking about - created by jdarling > (www.eonclash.com)? who lurks in the www.pascalgamedevelopment.com > site... > I haven't used that myself, but I did make my own unit with helper > functions, and a class that encapsulates a lua state so I can easily > call > lua procedures, etc. and also call Delphi procedures from Lua. I also need, as you, to call lua procedures from delphi/fpc and I need Lua call procedures inside delphi/fpc. Should it posible to share with me your implementation in order to check it works with fpc (linux-arm)?

Sure, I am not sure if I am allowed to add attachments to this mailing list but I will attempt to add my Lua units and an example program for you to use :)

LuaTest.dpr is a Delphi program (also works under Freepascal) showing how to use my TLuaScript class.
LuaPas.pas is the interface file for Lua version 5.1 LuaObjects.pas is the pascal file containing my TLuaScript class and other helper functions, etc.

Enjoy, and I hope it helps you :-)
cheers,
Paul

Reply by email

Paul Nicholls
Hi Paul,

<SNIP>

Show quoted text 6 lines
I also need, as you, to call lua procedures from delphi/fpc and I need Lua call procedures inside delphi/fpc. Should it posible to share with me your implementation in order to check it works with fpc (linux-arm)?

Sure, I am not sure if I am allowed to add attachments to this mailing list but I will attempt to add my Lua units and an example program for you to use :)

EDIT: the previous post I sent hasn't appeared yet so I am assuming I can't post with attachments to the group so I have uploaded the files, you can download them here:

fpc4gp2x.eonclash.com/downloads/lua5.1.dll
fpc4gp2x.eonclash.com/downloads/luaobjects.pas
fpc4gp2x.eonclash.com/downloads/LuaPas.pas
fpc4gp2x.eonclash.com/downloads/LuaTest.dpr

Lua5.1.dll is the Windows Lua DLL for this version. LuaTest.dpr is a Delphi program (also works under Freepascal) showing how to use my TLuaScript class.
LuaPas.pas is the interface file for Lua version 5.1 LuaObjects.pas is the pascal file containing my TLuaScript class and other helper functions, etc.

Enjoy, and I hope it helps you :-)
cheers,
Paul

Reply by email