From 5fbb02f1f92d9a96664e085c3598998bc50d065f Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Sun, 22 Jun 2025 11:01:56 -0400 Subject: parse the date directly into the struct tm --- challenge-326/walt-mankowski/c/ch-1.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/challenge-326/walt-mankowski/c/ch-1.c b/challenge-326/walt-mankowski/c/ch-1.c index f325530a39..0d27be33d7 100644 --- a/challenge-326/walt-mankowski/c/ch-1.c +++ b/challenge-326/walt-mankowski/c/ch-1.c @@ -17,21 +17,16 @@ int main(int argc, char *argv[]) { char *yyyymmdd = malloc(strlen(argv[1]) + 1); strcpy(yyyymmdd, argv[1]); - /* parse the date */ - int year = atoi(strtok(yyyymmdd, "-")); - int month = atoi(strtok(NULL, "-")); - int mday = atoi(strtok(NULL, "-")); - - /* construct a struct tm with the year, month, and day */ + /* parse the date and construct a struct tm with the year, month, and day */ memset(&tm, 0, sizeof(tm)); - tm.tm_year = year - 1900; - tm.tm_mon = month - 1; - tm.tm_mday = mday; + tm.tm_year = atoi(strtok(yyyymmdd, "-")) - 1900; + tm.tm_mon = atoi(strtok(NULL, "-")) - 1; + tm.tm_mday = atoi(strtok(NULL, "-")); /* get the epoch time for midnight on that date */ time_t t = mktime(&tm); - /* get the yday for that date */ + /* get the yday for that time */ struct tm *new_tm = gmtime(&t); printf("%d\n", new_tm->tm_yday + 1); } -- cgit