 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
tinyabs Guest
|
Posted: Wed Oct 12, 2005 12:15 pm Post subject: time_t to TDateTime |
|
|
How do I convert a time_t to a TDateTime? I try
TDateTime::FileTimeToDateTime() but it fails.
|
|
| Back to top |
|
 |
Steve Aletto Guest
|
Posted: Wed Oct 12, 2005 2:44 pm Post subject: Re: time_t to TDateTime |
|
|
| 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
|
Posted: Wed Oct 12, 2005 3:00 pm Post subject: Re: time_t to TDateTime |
|
|
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
|
Posted: Wed Oct 12, 2005 3:22 pm Post subject: Re: time_t to TDateTime |
|
|
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
|
|
| Back to top |
|
 |
Steve Aletto Guest
|
Posted: Wed Oct 12, 2005 3:33 pm Post subject: Re: time_t to TDateTime |
|
|
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
|
Posted: Wed Oct 12, 2005 4:09 pm Post subject: Re: time_t to TDateTime |
|
|
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 |
|
 |
|
|
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
|
|