BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

time_t to TDateTime

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API)
View previous topic :: View next topic  
Author Message
tinyabs
Guest





PostPosted: Wed Oct 12, 2005 12:15 pm    Post subject: time_t to TDateTime Reply with quote



How do I convert a time_t to a TDateTime? I try
TDateTime::FileTimeToDateTime() but it fails.


Back to top
Steve Aletto
Guest





PostPosted: Wed Oct 12, 2005 2:44 pm    Post subject: Re: time_t to TDateTime Reply with quote



Quote:
How do I convert a time_t to a TDateTime? I try
TDateTime::FileTimeToDateTime() but it fails.

One way:

TDateTime timet2datetime(const time_t t)
{
TDateTime rv;
struct tm *stm = localtime(&t);
if (stm)
{
rv = TDateTime(stm->tm_year + 1900, stm->tm_mon + 1,
stm->tm_mday);
rv += TDateTime(stm->tm_hour, stm->tm_min, stm->tm_sec,
0);
}
return(rv);
}

I think this is not thread-safe. Furthermore, it introduces a
memory leak due to localtime(), but I don't know whether it can
be solved.

Steve.



Back to top
tinyabs
Guest





PostPosted: Wed Oct 12, 2005 3:00 pm    Post subject: Re: time_t to TDateTime Reply with quote



Is time_t is dosdatetime type? I got this type from Win32's IpHelper. If it
is, I can write a function to extract the date and time.


Back to top
Bob Gonder
Guest





PostPosted: Wed Oct 12, 2005 3:22 pm    Post subject: Re: time_t to TDateTime Reply with quote

Steve Aletto wrote:

Quote:
struct tm *stm = localtime(&t);
if (stm)
{..... }
return(rv);

I think this is not thread-safe. Furthermore, it introduces a
memory leak due to localtime(), but I don't know whether it can
be solved.

Not a leak...
"localtime returns a pointer to the structure containing the time
elements. This structure is a static that is overwritten with each
call."
One might assume that the multithread version of the libs store the
struct in thread local storage.

But you might want to give rv a default value if localtime fails.



Back to top
Steve Aletto
Guest





PostPosted: Wed Oct 12, 2005 3:29 pm    Post subject: Re: time_t to TDateTime Reply with quote

Quote:
Is time_t is dosdatetime type? I got this type from Win32's
IpHelper. If it is, I can write a function to extract the date
and time.

time_t is a long integer:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_CRT_Standard_Types.asp

Steve.



Back to top
Steve Aletto
Guest





PostPosted: Wed Oct 12, 2005 3:33 pm    Post subject: Re: time_t to TDateTime Reply with quote

Quote:
Not a leak...

That's what I thought as well, but CodeGuard signals it...

Quote:
But you might want to give rv a default value if localtime
fails.

The TDateTime's contructor already gives a default value to rv:
"when called with no arguments, the resulting TDateTime object
has a Val data member of zero. This date and time corresponds to
12/30/1899 12:00 am".

Steve.



Back to top
tinyabs
Guest





PostPosted: Wed Oct 12, 2005 4:09 pm    Post subject: Re: time_t to TDateTime Reply with quote

I did some findings and produced this code.
time_t is the number of seconds passed a fixed date/time.

TDateTime ToDateTime(time_t time)
{
TDateTime dt = IncSecond(EncodeDateTime(1970, 1, 1, 0, 0, 0, 0), time);
return dt;
}


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.