________________________________________
Glenn Benes
InfoComp International, Inc.
gjb...@infocompii.com
http://www.infocompii.com/
In article <6oisgq$6c...@forums.borland.com>,
"Glenn Benes" <gjb...@infocompii.com> wrote:
> Is there an easy way to convert a timestamp given as time_t (C time) to
> TDateTime (Pascal time)?
Glenn
Break open the time_t structure then use the EncodeDate and EncodeTime
functions. If you didn't know, the TDataeTime is in fact a double, so you can
safely add the results of the above functions together.
Chris Lowe
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum
Hmm, only if it were so easy. That would work if I were using the tm
structure instead. The time_t is actually an unsigned long integer. I did
find documentation finally which states that the value given is the number
of seconds since January 1, 1970. The only difficulty is that Delphi
doesn't have any unsigned integer types!
Cheers
Chris Lowe
In article <6olipt$9a...@forums.borland.com>,
"Glenn Benes" <gjb...@infocompii.com> wrote:
> The time_t is actually an unsigned long integer. I did
> find documentation finally which states that the value given is the number
> of seconds since January 1, 1970. The only difficulty is that Delphi
> doesn't have any unsigned integer types!
>
> chrisl....@episys.com wrote in message
> >Break open the time_t structure then use the EncodeDate and EncodeTime
> >functions.
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
{ Convert a DOS integer date and time to TDateTime
Where:
DOSDate specifies the MS-DOS date.
The date is a packed 16-bit value with the following format:
Bits Contents
0-4 Day of the month (1-31)
5-8 Month (1 = January, 2 = February, and so on)
9-15 Year offset from 1980 (add 1980 to get actual year)
DOSTime specifies the MS-DOS time.
The time is a packed 16-bit value with the following format:
Bits Contents
0-4 Second divided by 2
5-10 Minute (0-59)
11-15 Hour (0-23 on a 24-hour clock)
Place "SysUtils" in your uses clause.
Test for 0 in your call to the function to see if an error occured.
i.e. if DOSDateTimeToStr(DOSDate, DOSTime) = 0 then "handle the
error"
}
function DOSDateTimeToStr(DOSDate, DOSTime: integer): TDateTime;
var
i, second, minute, hour, day, month, year: word;
DBit, TBit: boolean;
dat, tim: TDateTime;
begin
result := 0;
second := 0; minute := 0; hour := 0; day := 0; month := 0; year := 0;
for i := 15 downto 0 do begin
if DOSDate and (1 shl i) = 0 then DBit := false else DBit := true;
if DOSTime and (1 shl i) = 0 then TBit := false else TBit := true;
case i of
0: begin
if DBit then day := day + 1;
if TBit then second := second + 1;
end;
1: begin
if DBit then day := day + 2;
if TBit then second := second + 2;
end;
2: begin
if DBit then day := day + 4;
if TBit then second := second + 4;
end;
3: begin
if DBit then day := day + 8;
if TBit then second := second + 8;
end;
4: begin
if DBit then day := day + 16;
if TBit then second := second + 16;
end;
5: begin
if DBit then month := month + 1;
if TBit then minute := minute + 1;
end;
6: begin
if DBit then month := month + 2;
if TBit then minute := minute + 2;
end;
7: begin
if DBit then month := month + 4;
if TBit then minute := minute + 4;
end;
8: begin
if DBit then month := month + 8;
if TBit then minute := minute + 8;
end;
9: begin
if DBit then year := year + 1;
if TBit then minute := minute + 16;
end;
10: begin
if DBit then year := year + 2;
if TBit then minute := minute + 32;
end;
11: begin
if DBit then year := year + 4;
if TBit then hour := hour + 1;
end;
12: begin
if DBit then year := year + 8;
if TBit then hour := hour + 2;
end;
13: begin
if DBit then year := year + 16;
if TBit then hour := hour + 4;
end;
14: begin
if DBit then year := year + 32;
if TBit then hour := hour + 8;
end;
15: begin
if DBit then year := year + 64;
if TBit then hour := hour + 16;
end;
end;
end;
second := second div 2;
year := year + 1980;
TRY
dat := EncodeDate(year, month, day);
EXCEPT
exit;
END;
TRY
tim := EncodeTime(hour, minute, second, 0);
EXCEPT
exit;
END;
result := dat + tim;
end;
Donn
Glenn Benes <gjb...@infocompii.com> wrote in message
6olipt$9a...@forums.borland.com...
function time_tToDateTime (t : Integer) : TDateTime;
type
PComp = ^Comp;
var
baseDate : TDateTime;
systemBaseDate : TSystemTime;
filetimeBaseDate : TFileTime;
timet_comp, c : comp;
begin
baseDate := EncodeDate (1970, 1, 1);
DateTimeToSystemTime (baseDate, systemBaseDate);
SystemTimeToFileTime (systemBaseDate, filetimeBaseDate);
timet_comp := PComp (@filetimeBaseDate)^;
c := t;
c := c * 10000000 + timet_comp;
filetimeBaseDate := PFileTime (@c)^;
FileTimeToSystemTime (filetimeBaseDate, systemBaseDate);
result := SystemTimeToDateTime (systemBaseDate)
end;
Colin
e-mail :co...@wilsonc.demon.co.uk
web: http://www.wilsonc.demon.co.uk/delphi.htm