diff options
author | sinanmohd <pcmsinan@gmail.com> | 2022-06-20 14:17:29 +0530 |
---|---|---|
committer | sinanmohd <pcmsinan@gmail.com> | 2022-06-20 14:17:29 +0530 |
commit | 00814fe8d3de822d9042e035fd1fc367d771b2c8 (patch) | |
tree | 25c2fa791be0a7861a748a9e8fdd7c3e98cee2cc /5.8.c | |
parent | 103fbaa88a682cbeb017a57716f12a1439c32f03 (diff) |
5.8
Diffstat (limited to '5.8.c')
-rw-r--r-- | 5.8.c | 63 |
1 files changed, 63 insertions, 0 deletions
@@ -0,0 +1,63 @@ +#include <stdio.h> + +#define LEAP_YEAR_DAYS 366 +#define NON_LEAP_YEAR_DAYS 365 +#define NO_OF_MONTHS 12 + +int year_day(int day, int month, int year); +void month_day(int *day, int *month, int year, int year_day_val); + +int +main(void) +{ + int day, month, year, year_day_val; + + year = 2020; + printf("yd: %d\n", (year_day_val = year_day(15, 2, year))); + + month_day(&day, &month, year, year_day_val); + printf("md: %d-%d\n", day, month); +} + +static char daytab[][12] = { + {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, + {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, +}; + +int +year_day(int day, int month, int year) +{ + int i, leap; + + leap = ((!year%4 && year%100 ) || !year%400); + + if (month > NO_OF_MONTHS || day > daytab[leap][month-1]) + return -1; + + for (i = 0, --month; i < month; i++) + day += daytab[leap][i]; + + return day; +} + +void +month_day(int *day, int *month, int year, int year_day_val) +{ + int i, leap; + + leap = ((!year%4 && year%100 ) || !year%400); + + if ((leap && year_day_val > LEAP_YEAR_DAYS) || (!leap && year_day_val > NON_LEAP_YEAR_DAYS)) { + *month = -1; + *day = -1; + return; + } + + for (i = 1; year_day_val > daytab[leap][i-1]; i++) + year_day_val -= daytab[leap][i-1]; + + *month = i; + *day = year_day_val; + + return; +} |