Skip to content

Commit

Permalink
Support full arvelie format. Fix XXIIVV#62
Browse files Browse the repository at this point in the history
  • Loading branch information
octref committed Oct 27, 2020
1 parent f5efda7 commit 6fb94aa
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,4 @@ fputcent(FILE *f, char c)
fputs("&", f);
else
fputc(c, f);
}
}
43 changes: 42 additions & 1 deletion src/projects/arvelie/arvelie.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ is_valid_arvelie(char* date)
return 1;
}

int
is_full_valid_arvelie(char* date)
{
int y = (date[0] - '0') * 1000 + (date[1] - '0') * 100 +
(date[2] - '0') * 10 + date[3] - '0';
int m = date[4] - 'A';
int d = ((date[5] - '0') * 10) + date[6] - '0';
if(slen(date) != 7) {
return 0;
}
if(y < 0 || y > 9999 || m < 0 || m > 26 || d < 0 || d > 14) {
return 0;
}
return 1;
}

int
is_valid_ymdstr(char* date)
{
Expand Down Expand Up @@ -81,6 +97,15 @@ arvelie_to_doty(char* date)
return doty == -307 ? 364 : doty;
}

int
full_arvelie_to_doty(char* date)
{
int m = date[4] - 'A';
int d = ((date[5] - '0') * 10) + date[6] - '0';
int doty = (m * 14) + d;
return doty == -307 ? 364 : doty;
}

int
arvelie_to_epoch(char* date)
{
Expand Down Expand Up @@ -146,7 +171,11 @@ print_arvelie_from_doty(int y, int doty)
"S", "T", "U", "V", "W", "X", "Y", "Z", "+"};
int d = (doty % 14) + 1;
char* m = months[doty / 14];
printf("%d%s%02d\n", y % 100, m, d);
if(y >= 2000 && y < 3000) {
printf("%d%s%02d\n", y % 100, m, d);
} else {
printf("%d%s%02d\n", y, m, d);
}
}

void
Expand Down Expand Up @@ -186,6 +215,18 @@ print_ymdstr_from_arvelie(char* date)
return 0;
}

print_ymdstr_from_full_arvelie(char* date)
{
int y = (date[0] - '0') * 1000 + (date[1] - '0') * 100 +
(date[2] - '0') * 10 + date[3] - '0';
if(!is_full_valid_arvelie(date)) {
printf("Error: Invalid arvelie date\n");
return 1;
}
print_ymdstr_from_doty(y, full_arvelie_to_doty(date));
return 0;
}

void
print_arvelie(void)
{
Expand Down
2 changes: 2 additions & 0 deletions src/projects/arvelie/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ cc -std=c89 -DDEBUG -Wall -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werr

echo "Valid:"
./arvelie
./arvelie 2020-10-27
./arvelie 1986-03-22
./arvelie 19Q13
./arvelie 1919Q13

echo "Errors:"
./arvelie 2020-04-AG
Expand Down
2 changes: 2 additions & 0 deletions src/projects/arvelie/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ main(int argc, char* argv[])
print_arvelie_from_ymdstr(argv[1]);
else if(slen(argv[1]) == 5)
print_ymdstr_from_arvelie(argv[1]);
else if(slen(argv[1]) == 7)
print_ymdstr_from_full_arvelie(argv[1]);
else {
return error("Misformatted Input", "");
}
Expand Down

0 comments on commit 6fb94aa

Please sign in to comment.