H
Henrik Goldman
I have strings like "2006-03-26 21.51" which I would like to convert into
time_t. I know that the string is in localtime.
So far I've written the following code:
time_t LocalTimeFromString(string str)
{
struct tm t;
memset(&t, 0, sizeof(t));
if (str.length() != 16)
return 0;
sscanf(str.c_str(), "%04d-%02d-%02d %02d:%02d",
&t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min);
t.tm_year -= 1900;
t.tm_mon -= 1;
return mktime(&t);
}
However it seems that time_t must be in UTC time. If I would use ctime() on
the time_t result I don't get the right result back.
How would I go about adjusting the result for localtime? The solution must
be platform independent since it's suppose to work on both Unix and Windows.
Thanks in advance.
-- Henrik
time_t. I know that the string is in localtime.
So far I've written the following code:
time_t LocalTimeFromString(string str)
{
struct tm t;
memset(&t, 0, sizeof(t));
if (str.length() != 16)
return 0;
sscanf(str.c_str(), "%04d-%02d-%02d %02d:%02d",
&t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min);
t.tm_year -= 1900;
t.tm_mon -= 1;
return mktime(&t);
}
However it seems that time_t must be in UTC time. If I would use ctime() on
the time_t result I don't get the right result back.
How would I go about adjusting the result for localtime? The solution must
be platform independent since it's suppose to work on both Unix and Windows.
Thanks in advance.
-- Henrik