From d831e279cb856ec720ed770ce9d0c39eb347b7e1 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 4 Apr 2021 13:37:31 +0200 Subject: m + add list-birthdays and set-contact-birthday MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- get-contact-birthday.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'get-contact-birthday.c') 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 -- cgit v1.2.3-70-g09d2