diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-04-02 01:21:22 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-04-02 01:21:22 +0100 |
| commit | 9ac3f699a9d67f8203234be5f5e98bd586c3c531 (patch) | |
| tree | 7460e45a5e37f524b3c07a0896d5aca1c757f1cf | |
| parent | 53c8b0a71f6e456fcf6041b760987ba8e8274454 (diff) | |
| download | perlweeklychallenge-club-9ac3f699a9d67f8203234be5f5e98bd586c3c531.tar.gz perlweeklychallenge-club-9ac3f699a9d67f8203234be5f5e98bd586c3c531.tar.bz2 perlweeklychallenge-club-9ac3f699a9d67f8203234be5f5e98bd586c3c531.zip | |
Add C solution
| -rw-r--r-- | challenge-183/paulo-custodio/c/ch-2.c | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/challenge-183/paulo-custodio/c/ch-2.c b/challenge-183/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..8c71bbadd0 --- /dev/null +++ b/challenge-183/paulo-custodio/c/ch-2.c @@ -0,0 +1,113 @@ +/* +Challenge 183 + +Task 2: Date Difference +Submitted by: Mohammad S Anwar + +You are given two dates, $date1 and $date2 in the format YYYY-MM-DD. + +Write a script to find the difference between the given dates in terms on years +and days only. +Example 1 + +Input: $date1 = '2019-02-10' + $date2 = '2022-11-01' +Output: 3 years 264 days + +Example 2 + +Input: $date1 = '2020-09-15' + $date2 = '2022-03-29' +Output: 1 year 195 days + +Example 3 + +Input: $date1 = '2019-12-31' + $date2 = '2020-01-01' +Output: 1 day + +Example 4 + +Input: $date1 = '2019-12-01' + $date2 = '2019-12-31' +Output: 30 days + +Example 5 + +Input: $date1 = '2019-12-31' + $date2 = '2020-12-31' +Output: 1 year + +Example 6 + +Input: $date1 = '2019-12-31' + $date2 = '2021-12-31' +Output: 2 years + +Example 7 + +Input: $date1 = '2020-09-15' + $date2 = '2021-09-16' +Output: 1 year 1 day + +Example 8 + +Input: $date1 = '2019-09-15' + $date2 = '2021-09-16' +Output: 2 years 1 day +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +// https://pdc.ro.nu/jd-code.html +long gregorian_date_to_jd(int y, int m, int d) { + y += 8000; + if (m < 3) { y--; m += 12; } + return (y * 365) + (y / 4) - (y / 100) + (y / 400) - 1200820 + + (m * 153 + 3) / 5 - 92 + d - 1; +} + +void jd_to_gregorian_date(long jd, int* yp, int* mp, int* dp) { + int y, m, d; + for (y = jd / 366 - 4715; gregorian_date_to_jd(y + 1, 1, 1) <= jd; y++); + for (m = 1; gregorian_date_to_jd(y, m + 1, 1) <= jd; m++); + for (d = 1; gregorian_date_to_jd(y, m, d + 1) <= jd; d++); + *yp = y; *mp = m; *dp = d; +} + +int main(int argc, char* argv[]) { + if (argc != 3) { + fputs("usage: ch-2 yyyy-mm-dd yyyy-mm-dd", stderr); + return EXIT_FAILURE; + } + + int y1, m1, d1; + if (sscanf(argv[1], "%d-%d-%d", &y1, &m1, &d1) != 3) { + fputs("invalid date", stderr); + return EXIT_FAILURE; + } + long jd1 = gregorian_date_to_jd(y1, m1, d1); + + int y2, m2, d2; + if (sscanf(argv[2], "%d-%d-%d", &y2, &m2, &d2) != 3) { + fputs("invalid date", stderr); + return EXIT_FAILURE; + } + long jd2 = gregorian_date_to_jd(y2, m2, d2); + + int years = 0; + while (gregorian_date_to_jd(y1 + years + 1, m1, d1) <= jd2) + years++; + jd1 = gregorian_date_to_jd(y1 + years, m1, d1); + + if (years != 0) + printf("%d year%s ", years, years == 1 ? "" : "s"); + + int days = jd2 - jd1; + if (days != 0) + printf("%d day%s ", days, days == 1 ? "" : "s"); + printf("\n"); +} |
