aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWalt Mankowski <waltman@pobox.com>2025-06-22 10:30:14 -0400
committerWalt Mankowski <waltman@pobox.com>2025-06-22 10:30:14 -0400
commiteda098773eb04c06351dc6f52d732897b3944e0c (patch)
tree65653376f8db3066f20253eae91f5b46174316b5
parent293857df71b36b13686768eceaf8658a7ca707da (diff)
downloadperlweeklychallenge-club-eda098773eb04c06351dc6f52d732897b3944e0c.tar.gz
perlweeklychallenge-club-eda098773eb04c06351dc6f52d732897b3944e0c.tar.bz2
perlweeklychallenge-club-eda098773eb04c06351dc6f52d732897b3944e0c.zip
C code for challenge 1
-rw-r--r--challenge-326/walt-mankowski/c/ch-1.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/challenge-326/walt-mankowski/c/ch-1.c b/challenge-326/walt-mankowski/c/ch-1.c
new file mode 100644
index 0000000000..f325530a39
--- /dev/null
+++ b/challenge-326/walt-mankowski/c/ch-1.c
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <string.h>
+
+void usage() {
+ printf("Usage: ch-1 YYYY-MM-DD\n");
+ exit(1);
+}
+
+int main(int argc, char *argv[]) {
+ struct tm tm;
+ if (argc != 2)
+ usage();
+
+ /* Make a copy of argv[1] so we can call strtok() on it */
+ 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 */
+ memset(&tm, 0, sizeof(tm));
+ tm.tm_year = year - 1900;
+ tm.tm_mon = month - 1;
+ tm.tm_mday = mday;
+
+ /* get the epoch time for midnight on that date */
+ time_t t = mktime(&tm);
+
+ /* get the yday for that date */
+ struct tm *new_tm = gmtime(&t);
+ printf("%d\n", new_tm->tm_yday + 1);
+}