Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting time_t to TDateTime

436 views
Skip to first unread message

Glenn Benes

unread,
Jul 15, 1998, 3:00:00 AM7/15/98
to
Is there an easy way to convert a timestamp given as time_t (C time) to
TDateTime (Pascal time)?

________________________________________

Glenn Benes
InfoComp International, Inc.
gjb...@infocompii.com
http://www.infocompii.com/


chrisl....@episys.com

unread,
Jul 16, 1998, 3:00:00 AM7/16/98
to


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

Glenn Benes

unread,
Jul 16, 1998, 3:00:00 AM7/16/98
to
chrisl....@episys.com wrote in message
<6ol0u1$hql$1...@nnrp1.dejanews.com>...

>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.

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!

Chr...@my-dejanews.com

unread,
Jul 17, 1998, 3:00:00 AM7/17/98
to
Apologies. Please try this: TDateTime type is a double, with an integer
number of days. 0 is 12/30/1899 12:00 am. Use a Cardinal to store the
time_t. (Cardinal is the big unsigned integer type) The corresponding date
time is therefore caluclated thus: dateTime := 25566.0 + double(time_t_val)
/ (3600.0 * 24.0); I may be a day or two off with 25566.0, you may need to
adjust this figure.

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 ==-----

David Tucker

unread,
Jul 17, 1998, 3:00:00 AM7/17/98
to
In article <6oisgq$6c...@forums.borland.com>, gjb...@infocompii.com
says...

> Is there an easy way to convert a timestamp given as time_t (C time) to
> TDateTime (Pascal time)?
>
> ________________________________________
>
> Glenn Benes
> InfoComp International, Inc.
> gjb...@infocompii.com
> http://www.infocompii.com/
>
>
>
>
Try the following code:

{ 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 Ault

unread,
Jul 18, 1998, 3:00:00 AM7/18/98
to
Sure it does. The type "Word" is an unsigned two byte integer.

Donn

Glenn Benes <gjb...@infocompii.com> wrote in message
6olipt$9a...@forums.borland.com...

Colin Wilson

unread,
Jul 20, 1998, 3:00:00 AM7/20/98
to
Something like...


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


0 new messages