aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWalt Mankowski <waltman@pobox.com>2025-06-22 11:01:56 -0400
committerWalt Mankowski <waltman@pobox.com>2025-06-22 11:01:56 -0400
commit5fbb02f1f92d9a96664e085c3598998bc50d065f (patch)
tree958eba7efa26c6277f2fdc1e31c29ff6ab4188d4
parentc9918ff51ba59e4778ec4b2c9580610faaf009ba (diff)
downloadperlweeklychallenge-club-5fbb02f1f92d9a96664e085c3598998bc50d065f.tar.gz
perlweeklychallenge-club-5fbb02f1f92d9a96664e085c3598998bc50d065f.tar.bz2
perlweeklychallenge-club-5fbb02f1f92d9a96664e085c3598998bc50d065f.zip
parse the date directly into the struct tm
-rw-r--r--challenge-326/walt-mankowski/c/ch-1.c15
1 files 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);
}