From 9e160fac16e81c42ac368f450b9cc4df29753e00 Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Sun, 27 Mar 2016 12:54:10 +0200 Subject: Do not assume that days always have 86400 seconds Make that date membership is computed correctly, even if a day has less than 86400 seconds (e.g. after changing clocks). Reported-by: Hakan Jerning Signed-off-by: Lukas Fleischer --- src/utils.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index 4300b59..50f2507 100644 --- a/src/utils.c +++ b/src/utils.c @@ -418,6 +418,30 @@ time_t utcdate2sec(struct date day, unsigned hour, unsigned min) return t; } +/* Compare two dates (without comparing times). */ +int date_cmp_day(time_t d1, time_t d2) +{ + struct tm lt1, lt2; + + localtime_r((time_t *)&d1, <1); + localtime_r((time_t *)&d2, <2); + + if (lt1.tm_year < lt2.tm_year) + return -1; + if (lt1.tm_year > lt2.tm_year) + return 1; + if (lt1.tm_mon < lt2.tm_mon) + return -1; + if (lt1.tm_mon > lt2.tm_mon) + return 1; + if (lt1.tm_mday < lt2.tm_mday) + return -1; + if (lt1.tm_mday > lt2.tm_mday) + return 1; + + return 0; +} + /* Return a string containing the date, given a date in seconds. */ char *date_sec2date_str(long sec, const char *datefmt) { -- cgit v1.2.3-54-g00ecf