Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to convert a string to a time_t ??

Reply
Thread Tools

How to convert a string to a time_t ??

 
 
Herby
Guest
Posts: n/a
 
      01-22-2009
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.
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      01-22-2009
Herby wrote:
> 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

--
Michael
 
Reply With Quote
 
 
 
 
Herby
Guest
Posts: n/a
 
      01-23-2009
On Jan 22, 1:11 pm, Michael DOUBEZ <(E-Mail Removed)> wrote:
> Herby wrote:
> > 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
>
> --
> Michael


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.
 
Reply With Quote
 
Herby
Guest
Posts: n/a
 
      01-23-2009
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.


 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      01-23-2009
Herby wrote:
> 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.

--
Michael
 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      01-23-2009
Herby wrote:
> On Jan 22, 1:11 pm, Michael DOUBEZ <(E-Mail Removed)> wrote:
>> Herby wrote:
>>> 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/libr...1b(VS.80).aspx

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

--
Michael
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to convert string to time_t or struct tm in c++? inbaly C++ 0 12-22-2009 02:07 PM
Convert time_t to char? Jonathan C++ 4 03-22-2008 10:25 AM
How to convert from a struct tm to a time_t ? Angus Comber C Programming 7 05-15-2006 01:25 AM
Convert date & time to time_t HMS Surprise C Programming 1 05-16-2005 04:08 AM



Advertisments