Hi Vincent, On Fri, Aug 23, 2024 at 02:53:13PM GMT, Vincent Lefevre wrote: > On 2024-08-23 14:28:13 +0200, Alejandro Colomar wrote: > [about mktime] > > tm_wday is guaranteed to be left unmodified on a failed call. > > Where did you see that? Hmmm, it seems a novelty of C23. I don't find it in C11. Here's the text in C23: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#subsubsection.7.29.2.3> If the calendar time cannot be represented in the time_t encoding used for the return value or the value to be returned in the tm_year component of the structure pointed to by timeptr cannot be represented as an int, the function returns the value (time_t)(-1) and does not change the value of the tm_wday component of the structure. And the example code has also been modified in C23 to use this feature: static const char *const wday[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "-unknown-" }; ... time_str.tm_wday = 7; mktime(&time_str); printf("%s\n", wday[time_str.tm_wday]); > I cannot see any guarantee in case of a failed call, so that I would > say that tm_wday could have been modified, e.g. if the values are > set before checking whether the calendar time can be represented. Looking at the WG14 document logs, it seems it was added in n3147: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3147.txt> I've CCed Robert (author of that paper) and Jens (proposed other changes to mktime(3) recently), which may know what was discussed there. I assume they checked that this is true in all existing implementations, but that's just my assumption, so maybe they can say something. > > This provides a way to determine if the call failed. > > The example in C17 does not use the above claim that "tm_wday is > guaranteed to be left unmodified on a failed call" to determine > whether the call failed. Instead, it uses > > if (mktime(&time_str) == (time_t)(-1)) > > which is not 100% correct, since -1 can be a valid value (as already > noticed). > > > Indeed, this is the only way to determine if the call failed: > > > > tm.tm_wday = INT_MAX; > > mktime(&tm); > > if (tm.tm_wday == INT_MAX) > > err(1, "mktime"); > > Because of my above remark, I think that a mktime(&tm) == (time_t)(-1) > test is needed *in addition to* the tm.tm_wday == INT_MAX test. Have a lovely day! Alex -- <https://www.alejandro-colomar.es/>
Attachment:
signature.asc
Description: PGP signature