How to convert a string to a time_t ??

Discussion in 'C++' started by Herby, Jan 22, 2009.

  1. Herby

    Herby Guest

    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.
     
    Herby, Jan 22, 2009
    #1
    1. Advertising

  2. 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
     
    Michael DOUBEZ, Jan 22, 2009
    #2
    1. Advertising

  3. Herby

    Herby Guest

    On Jan 22, 1:11 pm, Michael DOUBEZ <> 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.
     
    Herby, Jan 23, 2009
    #3
  4. Herby

    Herby Guest

    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.
     
    Herby, Jan 23, 2009
    #4
  5. 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
     
    Michael DOUBEZ, Jan 23, 2009
    #5
  6. Herby wrote:
    > On Jan 22, 1:11 pm, Michael DOUBEZ <> 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/library/kys1801b(VS.80).aspx

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

    --
    Michael
     
    Michael DOUBEZ, Jan 23, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HMS Surprise

    Convert date & time to time_t

    HMS Surprise, May 16, 2005, in forum: C Programming
    Replies:
    1
    Views:
    626
    Gordon Burditt
    May 16, 2005
  2. Angus Comber

    How to convert from a struct tm to a time_t ?

    Angus Comber, May 12, 2006, in forum: C Programming
    Replies:
    7
    Views:
    1,060
    Ben Hetland
    May 15, 2006
  3. Nick Keighley

    UTC string -> time_t

    Nick Keighley, Dec 18, 2007, in forum: C Programming
    Replies:
    11
    Views:
    4,853
    Richard Heathfield
    Dec 22, 2007
  4. Jonathan

    Convert time_t to char?

    Jonathan, Mar 20, 2008, in forum: C++
    Replies:
    4
    Views:
    945
    James Kanze
    Mar 22, 2008
  5. inbaly
    Replies:
    0
    Views:
    2,862
    inbaly
    Dec 22, 2009
Loading...

Share This Page