Which time function checks the correct time range?

Discussion in 'C Programming' started by loudking, Nov 18, 2009.

  1. loudking

    loudking Guest

    Hi there,

    I was using 'mktime' funciton to convert some integers into time_t
    struct. Here is my code:

    sscanf(str, "%04u-%02u-%02u", &year, &month, &day);

    expire.tm_year = year - 1900;
    expire.tm_mon = month - 1;
    expire.tm_mday = day;
    expire.tm_hour = 0;
    expire.tm_min = 0;
    expire.tm_sec = 1;
    expire.tm_isdst = -1;

    if(mktime(&expire) == -1) {
    ErrorFatal(0x0004, ERR_0004_EXPIRATION_DATE_NO_SENSE, str);
    }
    else {
    strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z",
    &expire);
    printf("Expire = %s\n", buf);
    }

    Now if the input is 1998-25-45, the output would be 2000-02-14.

    But I would like the language to check for tme if the input time data
    is correct or not, for example, 32 days in a month is appeartly wrong.

    Is there such a function or library?

    Thanks in advance!
     
    loudking, Nov 18, 2009
    #1
    1. Advertising

  2. loudking <> writes:

    > I was using 'mktime' funciton to convert some integers into time_t
    > struct. Here is my code:
    >
    > sscanf(str, "%04u-%02u-%02u", &year, &month, &day);


    Take care here. %u does not prevent negative numbers being read. It
    just means the objects pointer to must be unsigned so the results will
    never be negative. Your real program checks the returned result, yes?

    > expire.tm_year = year - 1900;
    > expire.tm_mon = month - 1;


    If month is unsigned, month - 1 may not be what you expect it to be.

    > expire.tm_mday = day;
    > expire.tm_hour = 0;
    > expire.tm_min = 0;
    > expire.tm_sec = 1;
    > expire.tm_isdst = -1;
    >
    > if(mktime(&expire) == -1) {
    > ErrorFatal(0x0004, ERR_0004_EXPIRATION_DATE_NO_SENSE, str);
    > }
    > else {
    > strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z",
    > &expire);
    > printf("Expire = %s\n", buf);
    > }
    >
    > Now if the input is 1998-25-45, the output would be 2000-02-14.
    >
    > But I would like the language to check for tme if the input time data
    > is correct or not, for example, 32 days in a month is appeartly wrong.
    >
    > Is there such a function or library?


    No, but you can check that the result of mktime "expands" to a struct
    tm that is the same as expire.

    --
    Ben.
     
    Ben Bacarisse, Nov 18, 2009
    #2
    1. Advertising

  3. On Wed, 18 Nov 2009 18:16:21 +0000, Ben Bacarisse
    <> wrote:

    > loudking <> writes:

    <snip>
    > > if(mktime(&expire) == -1) {
    > > ErrorFatal(0x0004, ERR_0004_EXPIRATION_DATE_NO_SENSE, str);
    > > }
    > > else {
    > > strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z",
    > > &expire);
    > > printf("Expire = %s\n", buf);
    > > }
    > >
    > > Now if the input is 1998-25-45, the output would be 2000-02-14.
    > >
    > > But I would like the language to check for tme if the input time data
    > > is correct or not, for example, 32 days in a month is appeartly wrong.
    > >
    > > Is there such a function or library?

    >
    > No, but you can check that the result of mktime "expands" to a struct
    > tm that is the same as expire.


    mktime() modifies the out-of-range fields in its argument (or strictly
    the struct tm its argument points to). Instead check if expire after
    mktime() is same as a copy of expire before mktime.

    (Remembering that you should compare the members of a struct
    individually, not the whole thing with memcmp, in case of padding.)
     
    David Thompson, Nov 30, 2009
    #3
  4. David Thompson <> writes:

    > On Wed, 18 Nov 2009 18:16:21 +0000, Ben Bacarisse
    > <> wrote:

    <snip>
    >> No, but you can check that the result of mktime "expands" to a struct
    >> tm that is the same as expire.

    >
    > mktime() modifies the out-of-range fields in its argument (or strictly
    > the struct tm its argument points to). Instead check if expire after
    > mktime() is same as a copy of expire before mktime.


    That is what I was trying to say! I was not at all clear.

    <snip>
    --
    Ben.
     
    Ben Bacarisse, Nov 30, 2009
    #4
    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. Leon
    Replies:
    1
    Views:
    350
    Yannick Le goc
    Feb 12, 2004
  2. Amit
    Replies:
    1
    Views:
    355
    Srini
    May 6, 2005
  3. Replies:
    46
    Views:
    1,000
    Antoon Pardon
    Jul 25, 2006
  4. Lambda
    Replies:
    2
    Views:
    425
    James Kanze
    Jul 16, 2008
  5. Replies:
    4
    Views:
    153
    Florian Gilcher
    Mar 12, 2008
Loading...

Share This Page