Author: Jonas Bilinkevicius
There is a date/ time format that I'm trying to translate, but I can't find
anything that could match. This example is 2000-12-20 around 22:15. Integer:
977347109, Hex: 3A412225. Anyone know how to translate it?
Answer:
The value is a Unix Time, defined as seconds since 1970-01-01T00:00:00,0Z.
Important is the Letter Z, you live in Sweden, in consequence you must add 1 hour
for StandardDate and 2 hours for DaylightDate to the date. The infos you can get
with GetTimeZoneInformation. But you must determine, which Bias (Standard or
Daylight) is valid for the date (in this case -60). You can convert the date value
with the function below.
The Date for 977347109 is 2000-12-20T22:18:29+01:00.
1 const2 UnixDateDelta = 25569; { 1970-01-01T00:00:00,0 }3 SecPerMin = 60;
4 SecPerHour = SecPerMin * 60;
5 SecPerDay = SecPerHour * 24;
6 MinDayFraction = 1 / (24 * 60);
7 8 {Convert Unix time to TDatetime}9 10 function UnixTimeToDateTime(AUnixTime: DWord; ABias: Integer): TDateTime;
11 begin12 Result := UnixDateDelta + (AUnixTime div SecPerDay) { Days }13 + ((AUnixTime mod SecPerDay) / SecPerDay) { Seconds }14 - ABias * MinDayFraction { Bias to UTC in minutes };
15 end;
16 17 {Convert Unix time to String with locale settings}18 19 function UnixTimeToStr(AUnixTime: DWord; ABias: Integer): string;
20 begin21 Result := FormatDateTime('ddddd hh:nn:ss', UnixTimeToDateTime(AUnixTime, ABias));
22 end;
23 24 {Convert TDateTime to Unix time}25 26 function DateTimeToUnixTime(ADateTime: TDateTime; ABias: Integer): DWord;
27 begin28 Result := Trunc((ADateTime - UnixDateDelta) * SecPerDay) + ABias * SecPerMin;
29 end;
30 31 procedure TForm1.Button4Click(Sender: TObject);
32 begin33 Label1.Caption := UnixTimeToStr(977347109, -60);
34 end;