[hackers] [sbase][PATCH] strptime: tm should be initialized as the current time

From: Elie Le Vaillant <eolien55_AT_disroot.org>
Date: Thu, 29 Feb 2024 17:54:20 +0100

localtime() provides the necessary informations to make mktime() work
even in the absence of most informations.

That way, strptime "%d/%m" 26/08 outputs a timestamp corresponding to
the 26th of August of the current year, rather than the 1st January of
1900.
---
 strptime.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/strptime.c b/strptime.c
index 381a804..9d65219 100644
--- a/strptime.c
+++ b/strptime.c
_AT_@ -12,9 +12,8 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
-	struct tm tm;
-	time_t time;
-	char *fmt;
+	struct tm *tm;
+	time_t curtime;
 
 	ARGBEGIN {
 	default:
_AT_@ -24,15 +23,17 @@ main(int argc, char *argv[])
 	/* This make mktime(3) guess whether Daylight Saving Time is in effect.
 	 * If not specified, the output is non-deterministic, considering that
 	 * tm is initially random */
-	tm.tm_isdst = -1;
+	curtime = time(NULL);
+	tm = localtime(&curtime);
+	tm->tm_isdst = -1;
 
 	if (argc != 2)
 		usage();
-	if (strptime(argv[1], argv[0], &tm) == NULL)
+	if (strptime(argv[1], argv[0], tm) == NULL)
 		eprintf("unable to parse\n");
-	if ((time = mktime(&tm)) == -1)
+	if ((curtime = mktime(tm)) == -1)
 		eprintf("mktime:");
 
-	printf("%ld\n", time);
+	printf("%ld\n", curtime);
 	return 0;
 }
-- 
2.44.0
Received on Thu Feb 29 2024 - 17:54:20 CET

This archive was generated by hypermail 2.3.0 : Thu Feb 29 2024 - 18:00:40 CET