This commits backport few things from the 64-bit time branch:
* Add VALID_TIME() macro that checks the valid range for seconds and
nanoseconds in one go
* Add internal TIME_S_MAX defined to UINT_MAX, so we can change the
maximum range for seconds in one place.
* Add more specific buffer sizes for formatting the dates and require
the buffers passed to the formatting functions to be at least that
big.
* When error happens when formatting the string, always return a valid
buffer with formatted mock datetime string instead of garbage.
Because the formatting functions doesn't return status, the caller
could print garbage (unterminated string).