diff options
author | Mattias Andrée <maandree@kth.se> | 2021-04-04 13:52:47 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2021-04-04 13:52:47 +0200 |
commit | 08ac19b665fceed3be23739075390a41b5dbfcfb (patch) | |
tree | 6776e45948cb8a15b4e89490cdc6a7c42d8cf2b7 | |
parent | m + add list-birthdays and set-contact-birthday (diff) | |
download | contacts-08ac19b665fceed3be23739075390a41b5dbfcfb.tar.gz contacts-08ac19b665fceed3be23739075390a41b5dbfcfb.tar.bz2 contacts-08ac19b665fceed3be23739075390a41b5dbfcfb.tar.xz |
m + move common code from get-contact-birthday.c and list-birthdays.c to common-birthday.c
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | Makefile | 12 | ||||
-rw-r--r-- | common-birthday.c | 112 | ||||
-rw-r--r-- | common.h | 6 | ||||
-rw-r--r-- | config.mk | 6 | ||||
-rw-r--r-- | get-contact-birthday.c | 109 | ||||
-rw-r--r-- | list-birthdays.c | 111 |
6 files changed, 128 insertions, 228 deletions
@@ -60,8 +60,8 @@ BIN =\ HDR =\ common.h -OBJ = $(BIN:=.o) -BOBJ = $(BIN:=.bo) +OBJ = $(BIN:=.o) common-birthday.o +BOBJ = $(OBJ:.o=.bo) $(OBJ): $(@:.o=.c) $(HDR) @@ -109,8 +109,8 @@ find-contact-by-photo: find-contact-by-photo.o find-contact-by-site: find-contact-by-site.o $(CC) -o $@ $@.o $(LDFLAGS) -get-contact-birthday: get-contact-birthday.o - $(CC) -o $@ $@.o $(LDFLAGS) +get-contact-birthday: get-contact-birthday.o common-birthday.o + $(CC) -o $@ $@.o common-birthday.o $(LDFLAGS) get-contact-chats: get-contact-chats.o $(CC) -o $@ $@.o $(LDFLAGS) @@ -151,8 +151,8 @@ get-contact-sites: get-contact-sites.o is-contact-ice: is-contact-ice.o $(CC) -o $@ $@.o $(LDFLAGS) -list-birthdays: list-birthdays.o - $(CC) -o $@ $@.o $(LDFLAGS) +list-birthdays: list-birthdays.o common-birthday.o + $(CC) -o $@ $@.o common-birthday.o $(LDFLAGS) list-chat-contacts: list-chat-contacts.o $(CC) -o $@ $@.o $(LDFLAGS) diff --git a/common-birthday.c b/common-birthday.c new file mode 100644 index 0000000..d6088ec --- /dev/null +++ b/common-birthday.c @@ -0,0 +1,112 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +static const int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + +int +get_age(struct libcontacts_birthday *bday, const struct tm *now) +{ + int age = now->tm_year + 1900 - (int)bday->year; + if (now->tm_mon + 1 < bday->month) + age -= 1; + else if (now->tm_mon + 1 == bday->month) + age -= (now->tm_mday < bday->day); + return age; +} + +void +print_birthdate(struct libcontacts_birthday *bday, const struct tm *now) +{ + int age, days = 0, y, m, d; + if (bday->year) + printf("%04u-", bday->year); + else + printf("??""??""-"); + if (bday->month) { + printf("(%02u)%.3s-", (unsigned)bday->month, + &"JanFebMarAprMayJunJulAugSepOctNovDec"[3 * ((bday->month - 1) % 12)]); + } else { + printf("(??"")??""?-"); + } + if (bday->day) + printf("%02u", (unsigned)bday->day); + else + printf("??"); + if ((bday->month - 1) % 12 == 1 && bday->day == 29) { + bday->day -= bday->before_on_common; + printf(" (%s on common years)", bday->before_on_common ? "(02)Feb-28" : "(03)Mar-01"); + } + if (bday->year && bday->month && bday->day) { + age = get_age(bday, now); + 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"); +} + +void +print_birthday(struct libcontacts_birthday *bday, const struct tm *now) +{ + int next_year = 0, leap_year = 0, year, age, days = 0, y, m, d; + struct tm when; + if (now->tm_mon + 1 > bday->month) + next_year = 1; + else if (now->tm_mon + 1 == bday->month) + next_year = (now->tm_mday > bday->day); + if (bday->year) { + printf("%04i-", now->tm_year + next_year + 1900); + } else { + printf("??""??""-"); + } + year = now->tm_year + next_year + 1900; + if (year % 4 == 0 && (year % 100 || year % 400 == 0)) + leap_year = 1; + if (((bday->month - 1) % 12 == 1 && bday->day == 29) && leap_year) { + if (bday->before_on_common) { + bday->day -= 1; + } else { + bday->day = 1; + bday->month += 1; + } + } + 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; + when.tm_mday = bday->day; + if (bday->year) { + age = get_age(bday, &when); + printf("%i %s ", age, age == 1 ? "year" : "years"); + } + if (when.tm_mon == now->tm_mon && when.tm_mday == now->tm_mday) { + printf("today)\n"); + } else { + y = now->tm_year; + m = now->tm_mon; + d = now->tm_mday; + while (m != when.tm_mon || d > when.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 += when.tm_mday - d; + printf("in %i %s)\n", days, days == 1 ? "day" : "days"); + } +} @@ -11,3 +11,9 @@ #ifndef BUFSIZ # define BUFSIZ 4096 #endif + + +/* common-birthday.c */ +int get_age(struct libcontacts_birthday *bday, const struct tm *now); +void print_birthdate(struct libcontacts_birthday *bday, const struct tm *now); +void print_birthday(struct libcontacts_birthday *bday, const struct tm *now); @@ -3,6 +3,6 @@ MANPREFIX = $(PREFIX)/share/man CC = cc -CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -I../libcontacts -CFLAGS = -std=c99 -Wall -O2 -LDFLAGS = -s -L../libcontacts -lcontacts -lsimple +CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 +CFLAGS = -std=c99 -Wall -Os +LDFLAGS = -s -lcontacts -lsimple diff --git a/get-contact-birthday.c b/get-contact-birthday.c index 173d6a2..c92c7ff 100644 --- a/get-contact-birthday.c +++ b/get-contact-birthday.c @@ -4,115 +4,6 @@ USAGE("[-n] contact-id ..."); -static int -get_age(struct libcontacts_birthday *bday, const struct tm *now) -{ - int age = now->tm_year + 1900 - (int)bday->year; - if (now->tm_mon + 1 < bday->month) - age -= 1; - else if (now->tm_mon + 1 == bday->month) - age -= (now->tm_mday < bday->day); - return age; -} - -static void -print_birthdate(struct libcontacts_birthday *bday, const struct tm *now) -{ - 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 - printf("??""??""-"); - if (bday->month) { - printf("(%02u)%.3s-", (unsigned)bday->month, - &"JanFebMarAprMayJunJulAugSepOctNovDec"[3 * ((bday->month - 1) % 12)]); - } else { - printf("(??"")??""?-"); - } - if (bday->day) - printf("%02u", (unsigned)bday->day); - else - printf("??"); - if ((bday->month - 1) % 12 == 1 && bday->day == 29) { - bday->day -= bday->before_on_common; - printf(" (%s on common years)", bday->before_on_common ? "(02)Feb-28" : "(03)Mar-01"); - } - if (bday->year && bday->month && bday->day) { - age = get_age(bday, now); - 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"); -} - -static void -print_birthday(struct libcontacts_birthday *bday, const struct tm *now) -{ - static const int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - int next_year = 0, leap_year = 0, year, age, days = 0, y, m, d; - struct tm when; - if (now->tm_mon + 1 > bday->month) - next_year = 1; - else if (now->tm_mon + 1 == bday->month) - next_year = (now->tm_mday > bday->day); - if (bday->year) { - printf("%04i-", now->tm_year + next_year + 1900); - } else { - printf("??""??""-"); - } - year = now->tm_year + next_year + 1900; - if (year % 4 == 0 && (year % 100 || year % 400 == 0)) - leap_year = 1; - if (((bday->month - 1) % 12 == 1 && bday->day == 29) && leap_year) { - if (bday->before_on_common) { - bday->day -= 1; - } else { - bday->day = 1; - bday->month += 1; - } - } - 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; - when.tm_mday = bday->day; - if (bday->year) { - age = get_age(bday, &when); - printf("%i %s ", age, age == 1 ? "year" : "years"); - } - if (when.tm_mon == now->tm_mon && when.tm_mday == now->tm_mday) { - printf("today)\n"); - } else { - y = now->tm_year; - m = now->tm_mon; - d = now->tm_mday; - while (m != when.tm_mon || d > when.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 += when.tm_mday - d; - printf("in %i %s)\n", days, days == 1 ? "day" : "days"); - } -} - int main(int argc, char *argv[]) { diff --git a/list-birthdays.c b/list-birthdays.c index 950df2a..13d75ed 100644 --- a/list-birthdays.c +++ b/list-birthdays.c @@ -4,114 +4,7 @@ USAGE("[-n] (-L | contact-id ...)"); -static int -get_age(struct libcontacts_birthday *bday, const struct tm *now) -{ - int age = now->tm_year + 1900 - (int)bday->year; - if (now->tm_mon + 1 < bday->month) - age -= 1; - else if (now->tm_mon + 1 == bday->month) - age -= (now->tm_mday < bday->day); - return age; -} - -static void -print_birthdate(struct libcontacts_birthday *bday, const struct tm *now) -{ - 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 - printf("??""??""-"); - if (bday->month) { - printf("(%02u)%.3s-", (unsigned)bday->month, - &"JanFebMarAprMayJunJulAugSepOctNovDec"[3 * ((bday->month - 1) % 12)]); - } else { - printf("(??"")??""?-"); - } - if (bday->day) - printf("%02u", (unsigned)bday->day); - else - printf("??"); - if ((bday->month - 1) % 12 == 1 && bday->day == 29) { - bday->day -= bday->before_on_common; - printf(" (%s on common years)", bday->before_on_common ? "(02)Feb-28" : "(03)Mar-01"); - } - if (bday->year && bday->month && bday->day) { - age = get_age(bday, now); - 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"); -} - -static void -print_birthday(struct libcontacts_birthday *bday, const struct tm *now) -{ - static const int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - int next_year = 0, leap_year = 0, year, age, days = 0, y, m, d; - struct tm when; - if (now->tm_mon + 1 > bday->month) - next_year = 1; - else if (now->tm_mon + 1 == bday->month) - next_year = (now->tm_mday > bday->day); - if (bday->year) { - printf("%04i-", now->tm_year + next_year + 1900); - } else { - printf("??""??""-"); - } - year = now->tm_year + next_year + 1900; - if (year % 4 == 0 && (year % 100 || year % 400 == 0)) - leap_year = 1; - if (((bday->month - 1) % 12 == 1 && bday->day == 29) && leap_year) { - if (bday->before_on_common) { - bday->day -= 1; - } else { - bday->day = 1; - bday->month += 1; - } - } - 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; - when.tm_mday = bday->day; - if (bday->year) { - age = get_age(bday, &when); - printf("%i %s ", age, age == 1 ? "year" : "years"); - } - if (when.tm_mon == now->tm_mon && when.tm_mday == now->tm_mday) { - printf("today)\n"); - } else { - y = now->tm_year; - m = now->tm_mon; - d = now->tm_mday; - while (m != when.tm_mon || d > when.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 += when.tm_mday - d; - printf("in %i %s)\n", days, days == 1 ? "day" : "days"); - } -} +static struct tm *now; static int compare_by_birthdate(const void *apv, const void *bpv) @@ -133,8 +26,6 @@ compare_by_birthdate(const void *apv, const void *bpv) return 0; } -static struct tm *now; - static int compare_by_birthday(const void *apv, const void *bpv) { |