]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
timeutils: match today day and this year correctly
authorSami Kerola <kerolasa@iki.fi>
Sat, 5 Jan 2019 21:32:23 +0000 (21:32 +0000)
committerSami Kerola <kerolasa@iki.fi>
Sat, 12 Jan 2019 08:46:29 +0000 (08:46 +0000)
Assumption all years since 1970 have been exactly 365 days long has it's
problems when leap years happen.  Lets use struct tm fields that are
provided by localtime_r(), making year and day to be correctly compared even
when it's late new years eve somewhere else than UTC-0.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
include/timeutils.h
lib/timeutils.c

index 230e6db5f90224022353d4f7a24b1d9f93ad3167..95f1b44daabe7885ec5ca0ef53ca35ab9856e9c2 100644 (file)
@@ -82,9 +82,6 @@ int strtime_iso(const time_t *t, int flags, char *buf, size_t bufsz);
 
 #define UL_SHORTTIME_THISYEAR_HHMM (1 << 1)
 
-int time_is_today(const time_t *t, struct timeval *now);
-int time_is_thisyear(const time_t *t, struct timeval *now);
-
 int strtime_short(const time_t *t, struct timeval *now, int flags, char *buf, size_t bufsz);
 
 #ifndef HAVE_TIMEGM
index 9c286aebc663d3e34e6e3881cd7002c88a2e04dd..d403ced9031296b0e1be6c9f42712d3b6f58f107 100644 (file)
@@ -503,34 +503,37 @@ int strtime_iso(const time_t *t, int flags, char *buf, size_t bufsz)
 }
 
 /* relative time functions */
-int time_is_today(const time_t *t, struct timeval *now)
+static inline int time_is_thisyear(struct tm const *const tm,
+                                  struct tm const *const tmnow)
 {
-       if (now->tv_sec == 0)
-               gettimeofday(now, NULL);
-       return *t / (3600 * 24) == now->tv_sec / (3600 * 24);
+       return tm->tm_year == tmnow->tm_year;
 }
 
-int time_is_thisyear(const time_t *t, struct timeval *now)
+static inline int time_is_today(struct tm const *const tm,
+                               struct tm const *const tmnow)
 {
-       if (now->tv_sec == 0)
-               gettimeofday(now, NULL);
-       return *t / (3600 * 24 * 365) == now->tv_sec / (3600 * 24 * 365);
+       return (tm->tm_yday == tmnow->tm_yday &&
+               time_is_thisyear(tm, tmnow));
 }
 
 int strtime_short(const time_t *t, struct timeval *now, int flags, char *buf, size_t bufsz)
 {
-        struct tm tm;
+       struct tm tm, tmnow;
        int rc = 0;
 
-        localtime_r(t, &tm);
+       if (now->tv_sec == 0)
+               gettimeofday(now, NULL);
+
+       localtime_r(t, &tm);
+       localtime_r(&now->tv_sec, &tmnow);
 
-       if (time_is_today(t, now)) {
+       if (time_is_today(&tm, &tmnow)) {
                rc = snprintf(buf, bufsz, "%02d:%02d", tm.tm_hour, tm.tm_min);
                if (rc < 0 || (size_t) rc > bufsz)
                        return -1;
                rc = 1;
 
-       } else if (time_is_thisyear(t, now)) {
+       } else if (time_is_thisyear(&tm, &tmnow)) {
                if (flags & UL_SHORTTIME_THISYEAR_HHMM)
                        rc = strftime(buf, bufsz, "%b%d/%H:%M", &tm);
                else