diff options
author | Mattias Andrée <maandree@kth.se> | 2021-04-04 13:37:31 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2021-04-04 13:37:31 +0200 |
commit | d831e279cb856ec720ed770ce9d0c39eb347b7e1 (patch) | |
tree | 51dac38fdba5dfdee06a46fa7bed3dc0f01c035f /get-contact-birthday.c | |
parent | misc (diff) | |
download | contacts-d831e279cb856ec720ed770ce9d0c39eb347b7e1.tar.gz contacts-d831e279cb856ec720ed770ce9d0c39eb347b7e1.tar.bz2 contacts-d831e279cb856ec720ed770ce9d0c39eb347b7e1.tar.xz |
m + add list-birthdays and set-contact-birthday
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'get-contact-birthday.c')
-rw-r--r-- | get-contact-birthday.c | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/get-contact-birthday.c b/get-contact-birthday.c index 29be768..173d6a2 100644 --- a/get-contact-birthday.c +++ b/get-contact-birthday.c @@ -18,7 +18,8 @@ get_age(struct libcontacts_birthday *bday, const struct tm *now) static void print_birthdate(struct libcontacts_birthday *bday, const struct tm *now) { - int age; + static const int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + int age, days = 0, y, m, d; if (bday->year) printf("%04u-", bday->year); else @@ -39,7 +40,20 @@ print_birthdate(struct libcontacts_birthday *bday, const struct tm *now) } if (bday->year && bday->month && bday->day) { age = get_age(bday, now); - printf(", %i %s old today", age, age == 1 ? "year" : "years"); + y = (int)bday->year - 1900; + m = (int)bday->month - 1; + d = (int)bday->day; + while (m != now->tm_mon || d > now->tm_mday) { + days += days_in_month[m] - (d - 1); + days += (m == 1 && y % 4 == 0 && (y % 100 || y % 400 == 0)); + d = 1; + if (++m == 12) { + y += 1; + m = 0; + } + } + days += now->tm_mday - d; + printf(", %i %s and %i %s old", age, age == 1 ? "year" : "years", days, days == 1 ? "day" : "days"); } printf("\n"); } @@ -70,7 +84,7 @@ print_birthday(struct libcontacts_birthday *bday, const struct tm *now) bday->month += 1; } } - printf("(%02u)%.3s-%02u (", (unsigned)bday->month % 12, + printf("(%02u)%.3s-%02u (", (unsigned)bday->month, &"JanFebMarAprMayJunJulAugSepOctNovDec"[3 * ((bday->month - 1) % 12)], (unsigned)bday->day); when.tm_year = now->tm_year + next_year; when.tm_mon = (bday->month - 1) % 12; @@ -97,7 +111,6 @@ print_birthday(struct libcontacts_birthday *bday, const struct tm *now) days += when.tm_mday - d; printf("in %i %s)\n", days, days == 1 ? "day" : "days"); } - } int |