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
@@ -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:
@@ -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


Reply via email to