On 6/6/22 10:35, Almaz Mingaleev wrote:
1) According to the man page mktime sets errno if tm is invalid. Why is it not safe to just check errno value (saving and restoring it if needed)?
Because mktime can set errno if tm is valid. mktime is like most C syscalls - when they fail, they set errno to a value that tells you what the error was, but when they succeed they set errno to an unspecified value.
2) man page says that in error case mktime "returns (time_t) -1 and does not alter the members of the broken-down time structure". Wouldn't it be enough to check that tm.tm_yday is >= 0?
That's the man page for TZDB mktime; unfortunately the C standard does not guarantee this and strftime.c is attempting to be portable to non-TZDB mktime. Now that I look at the code again, I see that the heuristic was still too trusting of non-TZDB mktime implementations, so I installed the attached to tighten things up. It's just a heuristic, so it can still do the wrong thing on even weirder implementations (though not TZDB). Perhaps some day we can optimize strftime.c if we know that TZDB-compatible mktime is being used.