Which time function checks the correct time range?

L

loudking

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!
 
B

Ben Bacarisse

loudking said:
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.
 
D

David Thompson

loudking <[email protected]> writes:

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.)
 
B

Ben Bacarisse

David Thompson said:
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>
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top