Última actividad 5 hours ago

jordan's Avatar jordan revisó este gist 5 hours ago. Ir a la revisión

1 file changed, 63 insertions

arvelie.c(archivo creado)

@@ -0,0 +1,63 @@
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 +
13 + void usage();
14 + int main(int argc, char* argv[]);
15 +
16 + void
17 + usage()
18 + {
19 + fprintf(stderr, "usage: arvelie [-d YYYY-MM-DD]\n");
20 + exit(EXIT_FAILURE);
21 + }
22 +
23 + int
24 + main(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 + }
Siguiente Anterior