Zuletzt aktiv 6 hours ago

Änderung 24c67d76425d5fb7cb509c8849c278feb4613046

arvelie.c Originalformat
1/* POSIX arvelie.c
2 *
3 * a simple program to convert dates to arvelie.
4 * read more: https://wiki.xxiivv.com/site/arvelie.html
5 */
6
7#include <math.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <time.h>
11#include <unistd.h>
12
13void usage();
14int main(int argc, char* argv[]);
15
16void
17usage()
18{
19 fprintf(stderr, "usage: arvelie [-d YYYY-MM-DD]\n");
20 exit(EXIT_FAILURE);
21}
22
23int
24main(int argc, char* argv[])
25{
26 time_t t = -1;
27 char* inputDate = NULL;
28
29 int opt;
30 while ((opt = getopt(argc, argv, ":d:")) != -1) {
31 switch (opt) {
32 case 'd':
33 inputDate = optarg;
34 break;
35 case '?':
36 usage();
37 break;
38 case ':':
39 usage();
40 break;
41 default:
42 usage();
43 }
44 }
45
46 if (inputDate != NULL) {
47 struct tm tm = {0};
48 strptime(inputDate, "%Y-%m-%d", &tm);
49 t = mktime(&tm);
50 } else {
51 t = time(NULL);
52 }
53
54 struct tm* date = localtime(&t);
55
56 int yday = date->tm_yday;
57 int year = date->tm_year % 100;
58 char month = (yday < 364) ? (char)65 + (yday / 14) : '+';
59 int day = yday % 14;
60
61 printf("%02d%c%02d\n", year, month, day);
62 exit(EXIT_SUCCESS);
63}