How to convert a string to a time_t ??

H

Herby

Hi I have a string representation of a date in the following format:
'%Y-%m-%d'

As an example an actual value would be something like '2009-01-22'

I want to covert this to a time_t. I can only use the C functions for
this, so I have the following method that attempts this conversion:

Where mRawTime is time_t

void DateTime::SetDate( const String& date )
{
assert( date.empty() == false );

int yy=0, mm=0, dd=0;
struct tm when;

sscanf_s(date.c_str(), "%d-%d-%d", &yy, &mm, &dd );


when.tm_year = yy;
when.tm_mon = mm;
when.tm_mday = dd;

mRawTime = mktime(&when);
assert(mRawTime != -1 );
}


Trouble is the the mktime function always returns -1 so getting an
error on the conversion.
Can anyone see what is wrong?

Thanks in advance.
 
M

Michael DOUBEZ

Herby said:
Hi I have a string representation of a date in the following format:
'%Y-%m-%d'

As an example an actual value would be something like '2009-01-22'

I want to covert this to a time_t. I can only use the C functions for
this, so I have the following method that attempts this conversion:

Where mRawTime is time_t

void DateTime::SetDate( const String& date )
{
assert( date.empty() == false );

int yy=0, mm=0, dd=0;
struct tm when;

sscanf_s(date.c_str(), "%d-%d-%d", &yy, &mm, &dd );


when.tm_year = yy;

tm_year is the number of year since 1900.
when.tm_mon = mm;

tm_mon is in range 0-11. (Which is highly incoherent with tm_mday being
in the range 1-31)
when.tm_mday = dd;

mRawTime = mktime(&when);
assert(mRawTime != -1 );
}


Trouble is the the mktime function always returns -1 so getting an
error on the conversion.
Can anyone see what is wrong?

Reading the specs of struct tm :)
 
H

Herby

tm_year is the number of year since 1900.


tm_mon is in range 0-11. (Which is highly incoherent with tm_mday being
in the range 1-31)




Reading the specs of struct tm :)

Thanks that solved my problem, i have read the f***** manual but i did
not see where it said the year and month had to be treated this way
mind you it was msdn i was reading as im on the MS platform.

Thanks again.
 
H

Herby

I still have a problem now with the day.
I have '2009-01-20' that I convert via tm as above now with

when.tm_year = yy - 1900;
when.tm_mon = mm-1;
when.tm_mday = dd;


But when I convert this back to a string I get '2009-01-19'
So I have lost a day! Any ideas now what is wrong?

Thanks in Advance.
 
M

Michael DOUBEZ

Herby said:
I still have a problem now with the day.
I have '2009-01-20' that I convert via tm as above now with

when.tm_year = yy - 1900;
when.tm_mon = mm-1;
when.tm_mday = dd;


But when I convert this back to a string I get '2009-01-19'
So I have lost a day! Any ideas now what is wrong?

As noted in my post earlier, unlike months, days are noted in the range
1-31. So you only need:
when.tm_mon = mm;

That's what inconsistent APIs bring.
 
M

Michael DOUBEZ

Herby said:
Herby said:
Hi I have a string representation of a date in the following format:
'%Y-%m-%d'
As an example an actual value would be something like '2009-01-22'
I want to covert this to a time_t. I can only use the C functions for
this, so I have the following method that attempts this conversion: [snip]
Trouble is the the mktime function always returns -1 so getting an
error on the conversion.
Can anyone see what is wrong?
Reading the specs of struct tm :)

Thanks that solved my problem, i have read the f***** manual but i did
not see where it said the year and month had to be treated this way
mind you it was msdn i was reading as im on the MS platform.

You would have to look for the struct tm in asctime page:
http://msdn.microsoft.com/en-us/library/kys1801b(VS.80).aspx

I use man pages and I don't have this problem :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top