diff options
author | Leah Neukirchen <leah@vuxu.org> | 2020-04-03 20:59:59 +0200 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2020-04-03 20:59:59 +0200 |
commit | 7db19e430c8d10a34f0f2db574e37d4c54299e20 (patch) | |
tree | f84a2af18534e7fbeaadf6a9a9130dfdcdff6859 /wcal.c | |
parent | 5597886318d0b4ec578b261b49f5bd80baf29538 (diff) | |
download | wcal-7db19e430c8d10a34f0f2db574e37d4c54299e20.tar.gz wcal-7db19e430c8d10a34f0f2db574e37d4c54299e20.tar.xz wcal-7db19e430c8d10a34f0f2db574e37d4c54299e20.zip |
write own -d parser with better defaults
Diffstat (limited to 'wcal.c')
-rw-r--r-- | wcal.c | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/wcal.c b/wcal.c index 9390f22..1435996 100644 --- a/wcal.c +++ b/wcal.c @@ -16,12 +16,44 @@ */ #define _XOPEN_SOURCE 700 -#include <time.h> +#include <ctype.h> #include <stdio.h> +#include <stdlib.h> +#include <time.h> #include <unistd.h> int flag1, flag3, flagc, flagi, flagy; +void +parse_isodate(char *optarg, struct tm *tm) +{ + tm->tm_hour = 12; /* avoid DST problems */ + tm->tm_min = tm->tm_sec = 0; + tm->tm_mday = 1; + tm->tm_mon = 0; + + if (isdigit(optarg[0]) && + isdigit(optarg[1]) && + isdigit(optarg[2]) && + isdigit(optarg[3]) && + (!optarg[4] || optarg[4] == '-')) { + tm->tm_year = atoi(optarg) - 1900; + + if (!optarg[4]) { + flagy = 1; + } else if (isdigit(optarg[5]) && isdigit(optarg[6]) && + (!optarg[7] || optarg[7] == '-')) { + tm->tm_mon = atoi(optarg+5) - 1; + + if (isdigit(optarg[8]) && isdigit(optarg[9]) && + (!optarg[10] || optarg[10] == '-')) + tm->tm_mday = atoi(optarg+8); + } + } + + mktime(tm); +} + int main(int argc, char *argv[]) { @@ -36,8 +68,7 @@ main(int argc, char *argv[]) case 'c': flagc = 1; break; case 'y': flagy = 1; break; case 'i': flagi = 1; break; - // XXX error handling, or write a parser oneself... - case 'd': strptime(optarg, "%Y-%m-%d", tm); break; + case 'd': parse_isodate(optarg, tm); break; } tm->tm_hour = 12; /* avoid DST problems */ |