Sign in Join

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

Mailing lists / lazarus / 2010-09

UnixToDateTime and time zone

10 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

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

Krzysztof

Hi,

I have a problem. Example this unix timestamp: 1280166617. DateTimeToString(UnixToDateTime(1280166617)) should give me 2010-07-26 19:50:17 (in country: Poland) but it give me 2010-07-26 17:50:17. I must add two hours:
UnixToDateTime(1280166617 + (120*60));

But what about when someone run my program in a different time zone? On linux, lazarus have TZSeconds in unixutils, but on windows there is no unixutils. I found this: http://wiki.lazarus.freepascal.org/PascalTZ. Have not tested this yet, because maybe there is a better solution?

Regards, Dibo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20100916/fff94421/attachment.html>

Reply by email

Jürgen Hestermann
to Krzysztof
I have a problem. Example this unix timestamp: 1280166617. DateTimeToString(UnixToDateTime(1280166617)) should give me 2010-07-26 19:50:17 (in country: Poland) but it give me 2010-07-26 17:50:17. I must add two hours:

As far as I know UnixTime is a global time.
It's UTC time format.
See http://en.wikipedia.org/wiki/Unix_time

So if you want a local time you need to adjust
according to the local time zone.

Reply by email

Dibo
to Jürgen Hestermann

Dnia 2010-09-16, czw o godzinie 19:49 +0200, J?rgen Hestermann pisze:

Show quoted text 12 lines
Krzysztof schrieb: > I have a problem. Example this unix timestamp: 1280166617. > DateTimeToString(UnixToDateTime(1280166617)) should give me 2010-07-26 > 19:50:17 (in country: Poland) but it give me 2010-07-26 17:50:17. I must > add two hours: As far as I know UnixTime is a global time. It's UTC time format. See http://en.wikipedia.org/wiki/Unix_time So if you want a local time you need to adjust according to the local time zone.

How can I do this? I'm confused. Example: If someone send mi unix time "1984-10-10 12:12:12" how can I know which time was in my time zone?

Show quoted text 5 lines
______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Alberto Narduzzi
to Dibo
How can I do this? I'm confused. Example: If someone send mi unix time "1984-10-10 12:12:12" how can I know which time was in my time zone?

if someone sends you a unixtime, the time in your zone was the time he sent you + your UTC offset.
And the time in his zone was the time he sent you + his UTC offset.

Of course, you also need to add the DST, just in case.

Cheers, A.

Reply by email

Dibo
to Alberto Narduzzi

Dnia 2010-09-16, czw o godzinie 22:50 +0300, Alberto Narduzzi pisze:

Show quoted text 11 lines
> How can I do this? I'm confused. Example: If someone send mi unix time > "1984-10-10 12:12:12" how can I know which time was in my time zone? if someone sends you a unixtime, the time in your zone was the time he sent you + your UTC offset. And the time in his zone was the time he sent you + his UTC offset. Of course, you also need to add the DST, just in case. Cheers, A.

Is there a function in FPC that returns me my local UTC offset?

Reply by email

Alberto Narduzzi
to Dibo

Dibo,

Is there a function in FPC that returns me my local UTC offset?

never tested them, thou' have a look at DateTimeToUnix and UnixToDateTime, in unit DateUtils

HTH

Cheers, A.

Reply by email

waldo kitty
to Alberto Narduzzi
Alberto Narduzzi wrote 6 lines
Dibo, > Is there a function in FPC that returns me my local UTC offset? never tested them, thou' have a look at DateTimeToUnix and UnixToDateTime, in unit DateUtils

something else to consider is that you should always pass the UTC offset =OR= always pass time from GMT and then convert to local time...

FWIW: i ran into something kinda similar recently with some perl code... i found that the perl mktime routine was returning results that were +/- 1 hour depending on how i was subtracting to find the difference between two unixtime stamps... it took me a few hours to find that i needed to be using the timelocal routine instead of mktime and that's all i changed... since then, i've remembered my history and taken to storing all times as local time converted to GMT and then converting back to local time when displaying... the main thing this helps with is the sending of the time data elsewhere and having them get the proper local time for the activity in their location...

HTH in some small way...

Reply by email

Dibo
to Alberto Narduzzi: never tested them, thou' have a look at DateTimeToUnix and UnixToDateTime, in unit…

Dnia 2010-09-17, pi? o godzinie 01:55 +0300, Alberto Narduzzi pisze:

Show quoted text 8 lines
Dibo, > Is there a function in FPC that returns me my local UTC offset? never tested them, thou' have a look at DateTimeToUnix and UnixToDateTime, in unit DateUtils HTH

UnixToDateTime and DateTimeToUnix don't uses any GMT/UTC calculation. They just convert date time just as it is. I search internet for time zone information in delphi and I found this article: http://delphicikk.atw.hu/listaz.php?id=2667&oldal=52

I change his code to free pascal cross platform and this is working function (tested on windows and linux):

uses dateutils, {$IFDEF MSWINDOWS} windows {$ENDIF} {$IFDEF UNIX} unixutil {$ENDIF}

function UniversalTimeToLocal(UT: TDateTime): TDateTime;
var
  LT: TDateTime;
  TZOffset: Integer;
  {$IFDEF MSWINDOWS}
  BiasType: Byte;
  TZInfo: TTimeZoneInformation;
  {$ENDIF}
begin
  LT := UT;
  {$IFDEF MSWINDOWS}
  BiasType := GetTimeZoneInformation(TZInfo);
  if (BiasType=0) then begin
    Result := UT;
    Exit;
  end;
  // Determine offset in effect for DateTime UT.
  if (BiasType=2) then
    TZOffset := TZInfo.Bias + TZInfo.DaylightBias
  else
    TZOffset := TZInfo.Bias + TZInfo.StandardBias;
  {$ENDIF}
  {$IFDEF UNIX}
    TZOffset := -Tzseconds div 60;
  {$ENDIF}
  // Apply offset.
  if (TZOffset > 0) then
    // Time zones west of Greenwich.
    LT := UT - EncodeTime(TZOffset div 60, TZOffset mod 60, 0, 0)
  else if (TZOffset = 0) then
    // Time Zone = Greenwich.
    LT := UT
  else if (TZOffset < 0) then
    // Time zones east of Greenwich.
    LT := UT + EncodeTime(Abs(TZOffset) div 60, Abs(TZOffset) mod 60, 0,
0);
  // Return Local Time.
  Result := LT;
end;

Demo:

procedure TForm1.Button1Click(Sender: TObject);
var
  d: TDateTime;
begin
  // This is unix time 2010-07-26 17:50:17
  d := UnixToDateTime(1280166617);
  d := UniversalTimeToLocal(d);
  // After convert we get 2010-07-26 19:50:17 (Poland GMT + 2h)
  Edit1.Text := DateTimeToStr(d);
end;

I am surprised that Free Pascal does not have so basic function. Should I post it on mantis as proposition?

Show quoted text 8 lines
Cheers, A. -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Felipe Monteiro de Carvalho
to Dibo
Dibo wrote
I am surprised that Free Pascal does not have so basic function. Should I post it on mantis as proposition?

Probably (unless it already exists), but I think your design could be improved. More precisely:

Add a function GetLocalTimeOffset() and make it's contents be completely ifdefed, only keep the header the same for windows and linux, so that it matches better how FPC implements it's functions.

Now function UniversalTimeToLocal should be fully cross-platform and require no ifdefs

Felipe Monteiro de Carvalho

Reply by email

José Mejuto
to Dibo: Dnia 2010-09-17, pi? o godzinie 01:55 +0300, Alberto Narduzzi pisze: UnixToDateTime and…

Hello Lazarus-List,

Friday, September 17, 2010, 3:37:35 PM, you wrote:

D> UnixToDateTime and DateTimeToUnix don't uses any GMT/UTC calculation. D> They just convert date time just as it is. I search internet for time D> zone information in delphi and I found this article: D> http://delphicikk.atw.hu/listaz.php?id=2667&oldal=52 D> I change his code to free pascal cross platform and this is working D> function (tested on windows and linux):

Be carefull with that functions, they only apply to NOW! They are completly useless and error prone as they only consider the "just now" situation, so any other GMT/UTC time different than now could be wrongly converted and apparently be ok.

Example, you record a time today 21 sept. 2010, in my local time (CET-1CEST) there is an offset of +2 hours from GMT, you can go GMT to local and back succesfully up to the last sunday of october where DST is not applied anymore up to the next year. So any conversion done in november over thet data recorded the 21 sept. 2010 will be converted with an offset of +1 (november) instead the +2 offset (when the time was recorded).

To solve this you need PascalTZ or similar functions.

Best regards, Jos?

Reply by email