diff options
| -rw-r--r-- | date.c | 67 | ||||
| -rw-r--r-- | date.h | 2 | ||||
| -rw-r--r-- | main.c | 6 |
3 files changed, 34 insertions, 41 deletions
@@ -12,6 +12,28 @@ Date extract_date(char *str) { return ret; } +Date adjust_date(Date date) { + if (date.month == FEB && date.day > 28) { + if (date.year % 4 == 0 && date.day > 29) { + date.day -= 29; date.month++; + } else { + date.day -= 28; date.month++; + } + } else if (date.month == JAN || date.month == MAR || date.month == MAY || date.month == JUL || + date.month == AUG || date.month == OCT || date.month == DEC) { + if (date.day > 31 && date.month == DEC) { + date.day -= 31; date.month = 1; + } else if (date.day > 31) { + date.day -= 31; date.month++; + } + } else if (date.month == APR || date.month == JUN || date.month == SEP || date.month == NOV) { + if (date.day > 30) { + date.day -= 30; date.month++; + } + } + return date; +} + bool smaller(Date a, Date b) { if (zero(b)) return true; if (zero(a)) return false; @@ -63,46 +85,15 @@ Date today() { Date tomorrow(Date td) { Date tm = { td.day+1, td.month, td.year }; - if (tm.month == FEB && tm.day > 28) { - if (tm.year % 4 == 0 && tm.day > 29) { - tm.day -= 29; tm.month++; - } else { - tm.day -= 28; tm.month++; - } - } else if (tm.month == JAN || tm.month == MAR || tm.month == MAY || tm.month == JUL || - tm.month == AUG || tm.month == OCT || tm.month == DEC) { - if (tm.day > 31 && tm.month == DEC) { - tm.day -= 31; tm.month = 1; - } else if (tm.day > 31) { - tm.day -= 31; tm.month++; - } - } else if (tm.month == APR || tm.month == JUN || tm.month == SEP || tm.month == NOV) { - if (tm.day > 30) { - tm.day -= 30; tm.month++; - } - } - return tm; + return adjust_date(tm); } Date nextweek(Date td) { Date nw = { td.day+7, td.month, td.year }; - if (nw.month == FEB && nw.day > 28) { - if (nw.year % 4 == 0 && nw.day > 29) { - nw.day -= 29; nw.month++; - } else { - nw.day -= 28; nw.month++; - } - } else if (nw.month == JAN || nw.month == MAR || nw.month == MAY || nw.month == JUL || - nw.month == AUG || nw.month == OCT || nw.month == DEC) { - if (nw.day > 31 && nw.month == DEC) { - nw.day -= 31; nw.month = 1; - } else if (nw.day > 31) { - nw.day -= 31; nw.month++; - } - } else if (nw.month == APR || nw.month == JUN || nw.month == SEP || nw.month == NOV) { - if (nw.day > 30) { - nw.day -= 30; nw.month++; - } - } - return nw; + return adjust_date(nw); +} + +Date nextmonth(Date td) { + Date nm = { td.day, td.month+1, td.year }; + return adjust_date(nm); } @@ -11,6 +11,7 @@ typedef struct Date { } Date; Date extract_date(char *str); +Date adjust_date(Date date); bool smaller(Date a, Date b); bool strictly_smaller(Date a, Date b); bool eql(Date a, Date b); @@ -22,6 +23,7 @@ char *print_date_to_string(Date date); Date today(); Date tomorrow(Date td); Date nextweek(Date td); +Date nextmonth(Date td); #define __DATE_H__ #endif @@ -79,7 +79,7 @@ int main(int argc, char **argv) { char *e; Date td = today(); Date tm = tomorrow(td); - Date nw = nextweek(td); + Date end = nextmonth(td); Date last_date = (struct Date){ -1, -1, -1 }; for (int i = 0; i < idx; i++) { if (!eql(agenda[i].date, last_date)) { @@ -92,7 +92,7 @@ int main(int argc, char **argv) { printf("\n%s [TODAY]:\n", e); else if (eql(last_date, tm)) printf("\n%s [TOMORROW]:\n", e); - else if (smaller(last_date, nw)) + else if (smaller(last_date, end)) printf("\n%s:\n", e); free(e); } else { @@ -100,7 +100,7 @@ int main(int argc, char **argv) { } } e = format_entry(agenda[i]); - if (zero(agenda[i].date) || smaller(agenda[i].date, nw)) puts(e); + if (zero(agenda[i].date) || smaller(agenda[i].date, end)) puts(e); free(e); } |
