ctime.3: Document how to check errors from mktime(3)
Alejandro Colomar
maintainerPATCH 1/1 ctime.3: Document how to check errors from mktime(3)
-1 is a valid successful time_t, for one second before the Epoch. And mktime(3) is allowed (like most libc calls) to set errno on success. This makes it impossible to determine errors from the return value or errno.
ISO C specifies that tp->tm_wday is unmodified after a failed call, and puts an example where this is used to determine errors. It is indeed the only way to check for errors from this call.
Document this detail in the RETURN VALUE section, add a CAVEATS section that warns about this, and write an example program that shows how to properly call this function.
All the code I've been able to find in several search engines either doesn't check for errors after mktime(3), or checks them incorrectly, so this documentation should help fix those.
---
Hi!
This patch only documents how to check for errors after mktime(3). It does not cover the corner cases reported by DJ. This patch should be uncontroversial. I'll rebase the other one after this.
Cheers,
Alex
man/man3/ctime.3 | 100 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-)
man/man3/ctime.3 +99 -1
@@ -241,7 +241,10 @@ .SH RETURN VALUE On error, .BR mktime () returns the value -.IR "(time_t)\ \-1" . +.IR "(time_t)\ \-1" , +and leaves the +.I tm->tm_wday +member unmodified. The remaining functions return NULL on error. On error, .I errno
@@ -412,6 +415,101 @@ .SH NOTES object types may overwrite the information in any object of the same type pointed to by the value returned from any previous call to any of them." This can occur in the glibc implementation. +.SH CAVEATS +.SS mktime() +.I (time_t) \-1 +can represent a valid time +(one second before the Epoch). +To determine if +.BR mktime () +failed, +one must use the +.I tm->tm_wday +field. +See the example program in EXAMPLES. +.SH EXAMPLES +The following shell session shows sample runs of the program: +.P +.in +4n +.EX +.RB $\~ "TZ=UTC ./a.out 1969 12 31 23 59 59 0" ; +\-1 +$ +.RB $\~ "export TZ=Europe/Madrid" ; +$ +.RB $\~ "./a.out 2147483647 2147483647 00 00 00 00 -1" ; +a.out: mktime: Value too large for defined data type +$ +.RB $\~ "./a.out 2024 08 23 00 17 53 \-1" ; +1724365073 +.RB $\~ "./a.out 2024 08 23 00 17 53 0" ; +1724368673 +.RB $\~ "./a.out 2024 08 23 00 17 53 1" ; +1724365073 +$ +.RB $\~ "./a.out 2024 02 23 00 17 53 \-1" ; +1708643873 +.RB $\~ "./a.out 2024 02 23 00 17 53 0" ; +1708643873 +.RB $\~ "./a.out 2024 02 23 00 17 53 1" ; +1708640273 +$ +.RB $\~ "./a.out 2024 03 26 02 17 53 \-1" ; +1679793473 +$ +.RB $\~ "./a.out 2024 10 29 02 17 53 \-1" ; +1698542273 +.RB $\~ "./a.out 2024 10 29 02 17 53 0" ; +1698542273 +.RB $\~ "./a.out 2024 10 29 02 17 53 1" ; +1698538673 +$ +.RB $\~ "./a.out 2024 02 29 12 00 00 \-1" ; +1677668400 +.EE +.SS Program source: mktime.c +\& +.\" SRC BEGIN (mktime.c) +.EX +#include <err.h> +#include <errno.h> +#include <limits.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +\& +int +main(int argc, char *argv[]) +{ + char **p; + time_t t; + struct tm tm; +\& + if (argc != 8) { + fprintf(stderr, "Usage: %s yyyy mm dd HH MM SS isdst\[rs]n", argv[0]); + exit(EXIT_FAILURE); + } +\& + p = &argv[1]; + tm.tm_year = atoi(*p++) \- 1900; + tm.tm_mon = atoi(*p++) \- 1; + tm.tm_mday = atoi(*p++); + tm.tm_hour = atoi(*p++); + tm.tm_min = atoi(*p++); + tm.tm_sec = atoi(*p++); + tm.tm_isdst = atoi(*p++); +\& + tm.tm_wday = INT_MIN; + t = mktime(&tm); + if (tm.tm_wday == INT_MIN) + err(EXIT_FAILURE, "mktime"); +\& + printf("%jd\[rs]n", (intmax_t) t); + exit(EXIT_SUCCESS); +} +.EE +.\" SRC END .SH SEE ALSO .BR date (1), .BR gettimeofday (2),
base-commit: 0813c125d8bf754c40015aa1b31f23e0650584ac
Signature · 1 line
2.45.2
Basically saying the same thing as in the other thread.
Quoted 2 lines · Alejandro Colomar
ISO C specifies that
tp->tm_wdayis unmodified after a failed call, and puts an example where this is used to determine errors.
This is not what I can see in C17.
Quoted 1 line
It is indeed the only way to check for errors from this call.
So you *additionally* need to test whether the return value is -1.
Signature · 3 lines
Vincent Lefèvre <[email protected]> - Web: <https://www.vinc17.net/> 100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/> Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)
Alejandro Colomar
maintainerPATCH v2 1/1 ctime.3: Document how to check errors from mktime(3)
In reply to Alejandro Colomar-1 is a valid successful time_t, for one second before the Epoch. And mktime(3) is allowed (like most libc calls) to set errno on success. This makes it impossible to determine errors from the return value or errno.
ISO C specifies that tp->tm_wday is unmodified after a failed call, and puts an example where this is used to determine errors. It is indeed the only way to check for errors from this call.
Document this detail in the RETURN VALUE section, add a CAVEATS section that warns about this, and write an example program that shows how to properly call this function.
All the code I've been able to find in several search engines either doesn't check for errors after mktime(3), or checks them incorrectly, so this documentation should help fix those.
This is guaranteed since ISO C23 and POSIX.1-2024. Prior to those standards, there was no standard way to check for errors. However, there are no known implementations that do not conform to this, according to WG14 (which themselves refer to the Austin Group for having researched that).
---
Hi!
This v2 changes the use of INT_MIN to just -1, which is simpler, and is blessed by POSIX.
Cheers,
Alex
Range-diff against v1:
1: 433eddc3e ! 1: 664cd54a8 ctime.3: Document how to check errors from mktime(3)
@@ Commit message
doesn't check for errors after mktime(3), or checks them incorrectly, so this documentation should help fix those.
+ This is guaranteed since ISO C23 and POSIX.1-2024. Prior to those
+ standards, there was no standard way to check for errors. However,
+ there are no known implementations that do not conform to this,
+ according to WG14 (which themselves refer to the Austin Group for having
+ researched that).
+
+ Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3147.txt>
+ Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3148.doc>
+ Link: <https://austingroupbugs.net/view.php?id=1614>
+ Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#subsubsection.7.29.2.3>
+ Cc: Vincent Lefevre <[email protected]>
Cc: DJ Delorie <[email protected]>
Cc: Carlos O'Donell <[email protected]>
Cc: Xi Ruoyao <[email protected]>
- Cc: Vincent Lefevre <[email protected]>
+ Cc: Brian Inglis <[email protected]>
@@ man/man3/ctime.3: .SH NOTES
+.EX
+#include <err.h>
+#include <errno.h>
-+#include <limits.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
@@ man/man3/ctime.3: .SH NOTES
+ tm.tm_sec = atoi(*p++);
+ tm.tm_isdst = atoi(*p++);
+\&
-+ tm.tm_wday = INT_MIN;
++ tm.tm_wday = \-1;
+ t = mktime(&tm);
-+ if (tm.tm_wday == INT_MIN)
++ if (tm.tm_wday == \-1)
+ err(EXIT_FAILURE, "mktime");
+\&
+ printf("%jd\[rs]n", (intmax_t) t);
man/man3/ctime.3 | 99 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-)
man/man3/ctime.3 +98 -1
@@ -241,7 +241,10 @@ .SH RETURN VALUE On error, .BR mktime () returns the value -.IR "(time_t)\ \-1" . +.IR "(time_t)\ \-1" , +and leaves the +.I tm->tm_wday +member unmodified. The remaining functions return NULL on error. On error, .I errno
@@ -412,6 +415,100 @@ .SH NOTES object types may overwrite the information in any object of the same type pointed to by the value returned from any previous call to any of them." This can occur in the glibc implementation. +.SH CAVEATS +.SS mktime() +.I (time_t) \-1 +can represent a valid time +(one second before the Epoch). +To determine if +.BR mktime () +failed, +one must use the +.I tm->tm_wday +field. +See the example program in EXAMPLES. +.SH EXAMPLES +The following shell session shows sample runs of the program: +.P +.in +4n +.EX +.RB $\~ "TZ=UTC ./a.out 1969 12 31 23 59 59 0" ; +\-1 +$ +.RB $\~ "export TZ=Europe/Madrid" ; +$ +.RB $\~ "./a.out 2147483647 2147483647 00 00 00 00 -1" ; +a.out: mktime: Value too large for defined data type +$ +.RB $\~ "./a.out 2024 08 23 00 17 53 \-1" ; +1724365073 +.RB $\~ "./a.out 2024 08 23 00 17 53 0" ; +1724368673 +.RB $\~ "./a.out 2024 08 23 00 17 53 1" ; +1724365073 +$ +.RB $\~ "./a.out 2024 02 23 00 17 53 \-1" ; +1708643873 +.RB $\~ "./a.out 2024 02 23 00 17 53 0" ; +1708643873 +.RB $\~ "./a.out 2024 02 23 00 17 53 1" ; +1708640273 +$ +.RB $\~ "./a.out 2024 03 26 02 17 53 \-1" ; +1679793473 +$ +.RB $\~ "./a.out 2024 10 29 02 17 53 \-1" ; +1698542273 +.RB $\~ "./a.out 2024 10 29 02 17 53 0" ; +1698542273 +.RB $\~ "./a.out 2024 10 29 02 17 53 1" ; +1698538673 +$ +.RB $\~ "./a.out 2024 02 29 12 00 00 \-1" ; +1677668400 +.EE +.SS Program source: mktime.c +\& +.\" SRC BEGIN (mktime.c) +.EX +#include <err.h> +#include <errno.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +\& +int +main(int argc, char *argv[]) +{ + char **p; + time_t t; + struct tm tm; +\& + if (argc != 8) { + fprintf(stderr, "Usage: %s yyyy mm dd HH MM SS isdst\[rs]n", argv[0]); + exit(EXIT_FAILURE); + } +\& + p = &argv[1]; + tm.tm_year = atoi(*p++) \- 1900; + tm.tm_mon = atoi(*p++) \- 1; + tm.tm_mday = atoi(*p++); + tm.tm_hour = atoi(*p++); + tm.tm_min = atoi(*p++); + tm.tm_sec = atoi(*p++); + tm.tm_isdst = atoi(*p++); +\& + tm.tm_wday = \-1; + t = mktime(&tm); + if (tm.tm_wday == \-1) + err(EXIT_FAILURE, "mktime"); +\& + printf("%jd\[rs]n", (intmax_t) t); + exit(EXIT_SUCCESS); +} +.EE +.\" SRC END .SH SEE ALSO .BR date (1), .BR gettimeofday (2),
base-commit: 0813c125d8bf754c40015aa1b31f23e0650584ac
Signature · 1 line
2.45.2
Quoted 1 line · Alejandro Colomar
++ if (tm.tm_wday == \-1)
'if (tm.tm_wday < 0)' is a bit faster on typical machines. (There are multiple instances of this issue.)
Quoted 1 line
+To determine if
"if" -> "whether"
Quoted 1 line
+
printf("%jd\[rs]n", (intmax_t) t);
This is not portable in general, as time_t might be unsigned. You could use strftime instead of printf. But see below for a better suggestion.
Quoted 1 line
+ tm.tm_year =
atoi(*p++)\- 1900;
This doesn't work for the year 2147485547 (2**31 + 1899), which mktime can handle on typical machines with 32-bit int and 64-bit time_t. Also, all the atoi calls silently mess up if the argument overflows or is syntactically invalid.
To simplify the example, I suggest not doing I/O or parsing. Just have a function that accepts a struct tm *, and returns true or false and updates the struct tm when it returns true. That would avoid the issues with printf and atoi.
Alejandro Colomar
maintainer[TO += mtk]
Hi Paul,
Quoted 5 lines · Paul Eggert
Quoted 1 line · Alejandro Colomar
++ if (tm.tm_wday == \-1)'if (tm.tm_wday < 0)' is a bit faster on typical machines. (There are multiple instances of this issue.)
I had the costume of checking errors with <0, and Michael Kerrisk convinced me of using ==-1 because it's more explicit for the reader. I now prefer ==-1. I find that when I read <0, it's sometimes unclear whether the author really intended to reject all negatives values or not, and it also requires that I check the specific API to know if other negative values are possible. On the contrary, -1 is usual for meaning an error, and rarely is the time when I need to check that it actually means an error.
(And tangentially, I don't think anyone who calls mktime(3) will be
worried about a single instruction. But I see that this discussion applies in other places where that might be more meaningful.)
Quoted 3 lines
Quoted 1 line
+To determine if
"if" -> "whether"
Thanks.
Quoted 5 lines
Quoted 2 lines
+
printf("%jd\[rs]n", (intmax_t) t);This is not portable in general, as
time_tmight be unsigned. You could use strftime instead of printf. But see below for a better suggestion.
Out of curiosity, in systems where time_t is unsigned, does a mktime(3) call with a time representing a time before Epoch result in an error, or is it stored as a huge time_t value?
Does any existing system (or historic one) use an unsigned time_t, or is that only hypothetical?
Quoted 6 lines
Quoted 1 line
+ tm.tm_year =
atoi(*p++)\- 1900;This doesn't work for the year 2147485547 (2**31 + 1899), which mktime can handle on typical machines with 32-bit int and 64-bit
time_t. Also, all the atoi calls silently mess up if the argument overflows or is syntactically invalid.
We extensively use atoi(3) in the EXAMPLES sections where we intend to communicate to the user that they should add error handling and call strtol(3) (or something better, cough) but that for brevity we don't.
The program serves to illustrate how mktime(3) works, and how its errors are handled.
Quoted 5 lines
To simplify the example, I suggest not doing I/O or parsing. Just have a function that accepts a struct tm *, and returns true or false and updates the struct tm when it returns true. That would avoid the issues with printf and atoi.
But then it wouldn't allow the reader to interact with the function as easily. They'd need to recompile the program every time. We try that example programs are interactive when it makes sense, and I think this one makes sense. The small oddities in the parsing are obviously wrong so that they are not copied.
Thanks for the review!
Have a lovely day!
Alex
Signature · 1 line
<https://www.alejandro-colomar.es/>
Quoted 2 lines · Alejandro Colomar
in systems where
time_tis unsigned, does a mktime(3)
call with a time representing a time before Epoch result in an error,
Yes, just like any out-of-range mktime would.
Quoted 1 line
Does any existing system (or historic one) use an unsigned
time_t
Yes.
Alejandro Colomar
maintainerHi Paul,
Quoted 6 lines · Paul Eggert
Quoted 2 lines · Alejandro Colomar
in systems where
time_tis unsigned, does a mktime(3)
call with a time representing a time before Epoch result in an error,Yes, just like any out-of-range mktime would.
Thanks! (I thought it might wrap around for things that fit the signed counterpart.)
Quoted 3 lines
Quoted 1 line
Does any existing system (or historic one) use an unsigned
time_tYes.
Hmmm, I guess we can do
if (is_signed(time_t))
printf("%jd\n", (intmax_t) t);
else
printf("%ju\n", (uintmax_t) t);strftime("%s") would be interesting, but it accepts a struct tm, not a time_t, so I can't pass that value directly from mktime(3) to strftime(3).
Have a lovely night!
Alex
Signature · 1 line
<https://www.alejandro-colomar.es/>
Quoted 2 lines · Alejandro Colomar
strftime("%s")would be interesting, but it accepts a struct tm, not atime_t
That's not a problem, since mktime effectively returns a struct tm as well as a time_t.
You should not use strftime with %s, though, since that might not work on some platforms during fall-back periods. You can use strftime with "%Y-%m-%d %H:%M:%S %z".
Alejandro Colomar
maintainerHi Paul,
Quoted 10 lines · Paul Eggert
Quoted 2 lines · Alejandro Colomar
strftime("%s")would be interesting, but it accepts a struct tm, not atime_tThat's not a problem, since mktime effectively returns a struct tm as well as a
time_t.You should not use strftime with %s, though, since that might not work on some platforms during fall-back periods. You can use strftime with "%Y-%m-%d %H:%M:%S %z".
That still does not allow to see the return value of mktime, which I think is interesting in the example (for example, it exemplifies how a valid date can return -1). It also shows the different return values when different tm_isdst values are passed for the same date. I'll keep the printf(3).
Cheers,
Alex
Signature · 1 line
<https://www.alejandro-colomar.es/>
Alejandro Colomar
maintainerPATCH v3 1/1 ctime.3: Document how to check errors from mktime(3)
In reply to Alejandro Colomar-1 is a valid successful time_t, for one second before the Epoch. And mktime(3) is allowed (like most libc calls) to set errno on success. This makes it impossible to determine errors from the return value or errno.
ISO C specifies that tp->tm_wday is unmodified after a failed call, and puts an example where this is used to determine errors. It is indeed the only way to check for errors from this call.
Document this detail in the RETURN VALUE section, add a CAVEATS section that warns about this, and write an example program that shows how to properly call this function.
All the code I've been able to find in several search engines either doesn't check for errors after mktime(3), or checks them incorrectly, so this documentation should help fix those.
This is guaranteed since ISO C23 and POSIX.1-2024. Prior to those standards, there was no standard way to check for errors. However, there are no known implementations that do not conform to this, according to WG14 (which themselves refer to the Austin Group for having researched that).
---
Hi!
v3 changes:
- Typos in the example runs (s/2024/2023/) - s/if/whether/ [Paul] - Respect the sign of time_t. [Paul] - Remove unused include. [IWYU] - Add links and CCs.
Cheers,
Alex
Range-diff against v2:
1: 664cd54a8 ! 1: 795308497 ctime.3: Document how to check errors from mktime(3)
@@ Commit message
according to WG14 (which themselves refer to the Austin Group for having
researched that).
+ Link: <https://lore.kernel.org/linux-man/[email protected]/T/#t>
+ Link: <https://lore.kernel.org/linux-man/6un6baaq5tez23irtycuvzqtuh7a4sdrf2px7tnyb3y6iqoxmq@2ofln4cd27ep/T/#t>
Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3147.txt>
Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3148.doc>
Link: <https://austingroupbugs.net/view.php?id=1614>
@@ Commit message
Cc: Carlos O'Donell <[email protected]>
Cc: Xi Ruoyao <[email protected]>
Cc: Brian Inglis <[email protected]>
+ Cc: "Robert C. Seacord" <[email protected]>
+ Cc: Jens Gustedt <[email protected]>
+ Cc: Robert Elz <[email protected]>
+ Cc: Andrew Josey <[email protected]>
+ Cc: Geoff Clare <[email protected]>
Cc: GNU C Library <[email protected]>
+ Cc: Austin Group <[email protected]>
+ Cc: Hans Åberg <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
## man/man3/ctime.3 ##
@@ man/man3/ctime.3: .SH NOTES
+.I (time_t) \-1
+can represent a valid time
+(one second before the Epoch).
-+To determine if
++To determine whether
+.BR mktime ()
+failed,
+one must use the
@@ man/man3/ctime.3: .SH NOTES
+.RB $\~ "./a.out 2024 02 23 00 17 53 1" ;
+1708640273
+$
-+.RB $\~ "./a.out 2024 03 26 02 17 53 \-1" ;
++.RB $\~ "./a.out 2023 03 26 02 17 53 \-1" ;
+1679793473
+$
-+.RB $\~ "./a.out 2024 10 29 02 17 53 \-1" ;
++.RB $\~ "./a.out 2023 10 29 02 17 53 \-1" ;
+1698542273
-+.RB $\~ "./a.out 2024 10 29 02 17 53 0" ;
++.RB $\~ "./a.out 2023 10 29 02 17 53 0" ;
+1698542273
-+.RB $\~ "./a.out 2024 10 29 02 17 53 1" ;
++.RB $\~ "./a.out 2023 10 29 02 17 53 1" ;
+1698538673
+$
-+.RB $\~ "./a.out 2024 02 29 12 00 00 \-1" ;
++.RB $\~ "./a.out 2023 02 29 12 00 00 \-1" ;
+1677668400
+.EE
+.SS Program source: mktime.c
@@ man/man3/ctime.3: .SH NOTES
+.\" SRC BEGIN (mktime.c)
+.EX
+#include <err.h>
-+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+\&
++#define is_signed(T) ((T) \-1 < 1)
++\&
+int
+main(int argc, char *argv[])
+{
@@ man/man3/ctime.3: .SH NOTES
+ if (tm.tm_wday == \-1)
+ err(EXIT_FAILURE, "mktime");
+\&
-+ printf("%jd\[rs]n", (intmax_t) t);
++ if (is_signed(time_t))
++ printf("%jd\[rs]n", (intmax_t) t);
++ else
++ printf("%ju\[rs]n", (uintmax_t) t);
++\&
+ exit(EXIT_SUCCESS);
+}
+.EEman/man3/ctime.3 | 104 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-)
man/man3/ctime.3 +103 -1
@@ -241,7 +241,10 @@ .SH RETURN VALUE On error, .BR mktime () returns the value -.IR "(time_t)\ \-1" . +.IR "(time_t)\ \-1" , +and leaves the +.I tm->tm_wday +member unmodified. The remaining functions return NULL on error. On error, .I errno
@@ -412,6 +415,105 @@ .SH NOTES object types may overwrite the information in any object of the same type pointed to by the value returned from any previous call to any of them." This can occur in the glibc implementation. +.SH CAVEATS +.SS mktime() +.I (time_t) \-1 +can represent a valid time +(one second before the Epoch). +To determine whether +.BR mktime () +failed, +one must use the +.I tm->tm_wday +field. +See the example program in EXAMPLES. +.SH EXAMPLES +The following shell session shows sample runs of the program: +.P +.in +4n +.EX +.RB $\~ "TZ=UTC ./a.out 1969 12 31 23 59 59 0" ; +\-1 +$ +.RB $\~ "export TZ=Europe/Madrid" ; +$ +.RB $\~ "./a.out 2147483647 2147483647 00 00 00 00 -1" ; +a.out: mktime: Value too large for defined data type +$ +.RB $\~ "./a.out 2024 08 23 00 17 53 \-1" ; +1724365073 +.RB $\~ "./a.out 2024 08 23 00 17 53 0" ; +1724368673 +.RB $\~ "./a.out 2024 08 23 00 17 53 1" ; +1724365073 +$ +.RB $\~ "./a.out 2024 02 23 00 17 53 \-1" ; +1708643873 +.RB $\~ "./a.out 2024 02 23 00 17 53 0" ; +1708643873 +.RB $\~ "./a.out 2024 02 23 00 17 53 1" ; +1708640273 +$ +.RB $\~ "./a.out 2023 03 26 02 17 53 \-1" ; +1679793473 +$ +.RB $\~ "./a.out 2023 10 29 02 17 53 \-1" ; +1698542273 +.RB $\~ "./a.out 2023 10 29 02 17 53 0" ; +1698542273 +.RB $\~ "./a.out 2023 10 29 02 17 53 1" ; +1698538673 +$ +.RB $\~ "./a.out 2023 02 29 12 00 00 \-1" ; +1677668400 +.EE +.SS Program source: mktime.c +\& +.\" SRC BEGIN (mktime.c) +.EX +#include <err.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +\& +#define is_signed(T) ((T) \-1 < 1) +\& +int +main(int argc, char *argv[]) +{ + char **p; + time_t t; + struct tm tm; +\& + if (argc != 8) { + fprintf(stderr, "Usage: %s yyyy mm dd HH MM SS isdst\[rs]n", argv[0]); + exit(EXIT_FAILURE); + } +\& + p = &argv[1]; + tm.tm_year = atoi(*p++) \- 1900; + tm.tm_mon = atoi(*p++) \- 1; + tm.tm_mday = atoi(*p++); + tm.tm_hour = atoi(*p++); + tm.tm_min = atoi(*p++); + tm.tm_sec = atoi(*p++); + tm.tm_isdst = atoi(*p++); +\& + tm.tm_wday = \-1; + t = mktime(&tm); + if (tm.tm_wday == \-1) + err(EXIT_FAILURE, "mktime"); +\& + if (is_signed(time_t)) + printf("%jd\[rs]n", (intmax_t) t); + else + printf("%ju\[rs]n", (uintmax_t) t); +\& + exit(EXIT_SUCCESS); +} +.EE +.\" SRC END .SH SEE ALSO .BR date (1), .BR gettimeofday (2),
base-commit: 0813c125d8bf754c40015aa1b31f23e0650584ac
Signature · 1 line
2.45.2
Alejandro Colomar
maintainerHi all,
Quoted 44 lines · Alejandro Colomar
-1 is a valid successful
time_t, for one second before the Epoch. And mktime(3) is allowed (like most libc calls) to set errno on success. This makes it impossible to determine errors from the return value or errno.ISO C specifies that
tp->tm_wdayis unmodified after a failed call, and puts an example where this is used to determine errors. It is indeed the only way to check for errors from this call.Document this detail in the RETURN VALUE section, add a CAVEATS section that warns about this, and write an example program that shows how to properly call this function.
All the code I've been able to find in several search engines either doesn't check for errors after mktime(3), or checks them incorrectly, so this documentation should help fix those.
This is guaranteed since ISO C23 and POSIX.1-2024. Prior to those standards, there was no standard way to check for errors. However, there are no known implementations that do not conform to this, according to WG14 (which themselves refer to the Austin Group for having researched that).
Link <https://lore.kernel.org/linux-man/6un6baaq5tez23irtycuvzqtuh7a4sdrf2px7tnyb3y6iqoxmq@2ofln4cd27ep/T/#t> open hereReported-by Paul Eggert <[email protected]>Cc Vincent Lefevre <[email protected]>Cc DJ Delorie <[email protected]>Cc Carlos O'Donell <[email protected]>Cc Xi Ruoyao <[email protected]>Cc Brian Inglis <[email protected]>Cc "Robert C. Seacord" <[email protected]>Cc Jens Gustedt <[email protected]>Cc Robert Elz <[email protected]>Cc Andrew Josey <[email protected]>Cc Geoff Clare <[email protected]>Cc GNU C Library <[email protected]>Cc Austin Group <[email protected]>Cc Hans Åberg <[email protected]>Signed-off-by Alejandro Colomar <[email protected]>
Quoted 1 line
---
I've finally applied this patch. Thank you all for your feedback! Especially Paul for finding so many problems in my code! :)
Have a lovely night!
Alex
Quoted 231 lines
Hi!
v3 changes:
(quoted diff) +0
-5- Typos in the example runs (s/2024/2023/) - s/if/whether/ [Paul] - Respect the sign of time_t. [Paul] - Remove unused include. [IWYU] - Add links and CCs.Cheers,
AlexRange-diff against v2:
1:664cd54a8! 1: 795308497 ctime.3: Document how to check errors from mktime(3)@@ Commit message according to WG14 (which themselves refer to the Austin Group for having researched that). + Link: <https://lore.kernel.org/linux-man/[email protected]/T/#t> + Link: <https://lore.kernel.org/linux-man/6un6baaq5tez23irtycuvzqtuh7a4sdrf2px7tnyb3y6iqoxmq@2ofln4cd27ep/T/#t> Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3147.txt> Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3148.doc> Link: <https://austingroupbugs.net/view.php?id=1614> @@ Commit message Cc: Carlos O'Donell <[email protected]> Cc: Xi Ruoyao <[email protected]> Cc: Brian Inglis <[email protected]> + Cc: "Robert C. Seacord" <[email protected]> + Cc: Jens Gustedt <[email protected]> + Cc: Robert Elz <[email protected]> + Cc: Andrew Josey <[email protected]> + Cc: Geoff Clare <[email protected]> Cc: GNU C Library <[email protected]> + Cc: Austin Group <[email protected]> + Cc: Hans Åberg <[email protected]> Signed-off-by: Alejandro Colomar <[email protected]> ## man/man3/ctime.3 ## @@ man/man3/ctime.3: .SH NOTES +.I (time_t) \-1 +can represent a valid time +(one second before the Epoch). -+To determine if ++To determine whether +.BR mktime () +failed, +one must use the @@ man/man3/ctime.3: .SH NOTES +.RB $\~ "./a.out 2024 02 23 00 17 53 1" ; +1708640273 +$ -+.RB $\~ "./a.out 2024 03 26 02 17 53 \-1" ; ++.RB $\~ "./a.out 2023 03 26 02 17 53 \-1" ; +1679793473 +$ -+.RB $\~ "./a.out 2024 10 29 02 17 53 \-1" ; ++.RB $\~ "./a.out 2023 10 29 02 17 53 \-1" ; +1698542273 -+.RB $\~ "./a.out 2024 10 29 02 17 53 0" ; ++.RB $\~ "./a.out 2023 10 29 02 17 53 0" ; +1698542273 -+.RB $\~ "./a.out 2024 10 29 02 17 53 1" ; ++.RB $\~ "./a.out 2023 10 29 02 17 53 1" ; +1698538673 +$ -+.RB $\~ "./a.out 2024 02 29 12 00 00 \-1" ; ++.RB $\~ "./a.out 2023 02 29 12 00 00 \-1" ; +1677668400 +.EE +.SS Program source: mktime.c @@ man/man3/ctime.3: .SH NOTES +.\" SRC BEGIN (mktime.c) +.EX +#include <err.h> -+#include <errno.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +\& ++#define is_signed(T) ((T) \-1 < 1) ++\& +int +main(int argc, char *argv[]) +{ @@ man/man3/ctime.3: .SH NOTES + if (tm.tm_wday == \-1) + err(EXIT_FAILURE, "mktime"); +\& -+ printf("%jd\[rs]n", (intmax_t) t); ++ if (is_signed(time_t)) ++ printf("%jd\[rs]n", (intmax_t) t); ++ else ++ printf("%ju\[rs]n", (uintmax_t) t); ++\& + exit(EXIT_SUCCESS); +} +.EEman/man3/ctime.3 | 104 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-)man/man3/ctime.3 +103
-1@@ -241,7 +241,10 @@ .SH RETURN VALUE On error, .BR mktime () returns the value -.IR "(time_t)\ \-1" . +.IR "(time_t)\ \-1" , +and leaves the +.I tm->tm_wday +member unmodified. The remaining functions return NULL on error. On error, .I errno@@ -412,6 +415,105 @@ .SH NOTES object types may overwrite the information in any object of the same type pointed to by the value returned from any previous call to any of them." This can occur in the glibc implementation. +.SH CAVEATS +.SS mktime() +.I (time_t) \-1 +can represent a valid time +(one second before the Epoch). +To determine whether +.BR mktime () +failed, +one must use the +.I tm->tm_wday +field. +See the example program in EXAMPLES. +.SH EXAMPLES +The following shell session shows sample runs of the program: +.P +.in +4n +.EX +.RB $\~ "TZ=UTC ./a.out 1969 12 31 23 59 59 0" ; +\-1 +$ +.RB $\~ "export TZ=Europe/Madrid" ; +$ +.RB $\~ "./a.out 2147483647 2147483647 00 00 00 00 -1" ; +a.out: mktime: Value too large for defined data type +$ +.RB $\~ "./a.out 2024 08 23 00 17 53 \-1" ; +1724365073 +.RB $\~ "./a.out 2024 08 23 00 17 53 0" ; +1724368673 +.RB $\~ "./a.out 2024 08 23 00 17 53 1" ; +1724365073 +$ +.RB $\~ "./a.out 2024 02 23 00 17 53 \-1" ; +1708643873 +.RB $\~ "./a.out 2024 02 23 00 17 53 0" ; +1708643873 +.RB $\~ "./a.out 2024 02 23 00 17 53 1" ; +1708640273 +$ +.RB $\~ "./a.out 2023 03 26 02 17 53 \-1" ; +1679793473 +$ +.RB $\~ "./a.out 2023 10 29 02 17 53 \-1" ; +1698542273 +.RB $\~ "./a.out 2023 10 29 02 17 53 0" ; +1698542273 +.RB $\~ "./a.out 2023 10 29 02 17 53 1" ; +1698538673 +$ +.RB $\~ "./a.out 2023 02 29 12 00 00 \-1" ; +1677668400 +.EE +.SS Program source: mktime.c +\& +.\" SRC BEGIN (mktime.c) +.EX +#include <err.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +\& +#define is_signed(T) ((T) \-1 < 1) +\& +int +main(int argc, char *argv[]) +{ + char **p; + time_t t; + struct tm tm; +\& + if (argc != 8) { + fprintf(stderr, "Usage: %s yyyy mm dd HH MM SS isdst\[rs]n", argv[0]); + exit(EXIT_FAILURE); + } +\& + p = &argv[1]; + tm.tm_year = atoi(*p++) \- 1900; + tm.tm_mon = atoi(*p++) \- 1; + tm.tm_mday = atoi(*p++); + tm.tm_hour = atoi(*p++); + tm.tm_min = atoi(*p++); + tm.tm_sec = atoi(*p++); + tm.tm_isdst = atoi(*p++); +\& + tm.tm_wday = \-1; + t = mktime(&tm); + if (tm.tm_wday == \-1) + err(EXIT_FAILURE, "mktime"); +\& + if (is_signed(time_t)) + printf("%jd\[rs]n", (intmax_t) t); + else + printf("%ju\[rs]n", (uintmax_t) t); +\& + exit(EXIT_SUCCESS); +} +.EE +.\" SRC END .SH SEE ALSO .BR date (1), .BR gettimeofday (2),base-commit:
0813c125d8bf754c40015aa1b31f23e0650584acSignature · 1 line
2.45.2
Signature · 1 line
<https://www.alejandro-colomar.es/>