arvelie.c
· 1.1 KiB · C
原始檔案
/* POSIX arvelie.c
*
* a simple program to convert dates to arvelie.
* read more: https://wiki.xxiivv.com/site/arvelie.html
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
void usage();
int main(int argc, char* argv[]);
void
usage()
{
fprintf(stderr, "usage: arvelie [-d YYYY-MM-DD]\n");
exit(EXIT_FAILURE);
}
int
main(int argc, char* argv[])
{
time_t t = -1;
char* inputDate = NULL;
int opt;
while ((opt = getopt(argc, argv, ":d:")) != -1) {
switch (opt) {
case 'd':
inputDate = optarg;
break;
case '?':
usage();
break;
case ':':
usage();
break;
default:
usage();
}
}
if (inputDate != NULL) {
struct tm tm = {0};
strptime(inputDate, "%Y-%m-%d", &tm);
t = mktime(&tm);
} else {
t = time(NULL);
}
struct tm* date = localtime(&t);
int yday = date->tm_yday;
int year = date->tm_year % 100;
char month = (yday < 364) ? (char)65 + (yday / 14) : '+';
int day = yday % 14;
printf("%02d%c%02d\n", year, month, day);
exit(EXIT_SUCCESS);
}
| 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 | } |