From 9b1657309055f6463f372f244683ac3a2f83d932 Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Fri, 9 Jan 2015 19:28:32 +0000 Subject: [PATCH] - stop doing time gymnastics for daylight savings. - use the _r functions for struct tm retrieval --- src/file.h | 8 +++++++- src/gmtime_r.c | 19 +++++++++++++++++++ src/localtime_r.c | 19 +++++++++++++++++++ src/print.c | 31 +++++++------------------------ 4 files changed, 52 insertions(+), 25 deletions(-) create mode 100644 src/gmtime_r.c create mode 100644 src/localtime_r.c diff --git a/src/file.h b/src/file.h index 5fc8d698..4eda387c 100644 --- a/src/file.h +++ b/src/file.h @@ -27,7 +27,7 @@ */ /* * file.h - definitions for file(1) program - * @(#)$File: file.h,v 1.164 2015/01/01 17:07:34 christos Exp $ + * @(#)$File: file.h,v 1.165 2015/01/02 21:29:39 christos Exp $ */ #ifndef __file_h__ @@ -564,6 +564,12 @@ char *ctime_r(const time_t *, char *); #ifndef HAVE_ASCTIME_R char *asctime_r(const struct tm *, char *); #endif +#ifndef HAVE_GMTIME_R +struct tm *gmtime_r(const time_t *, struct tm *); +#endif +#ifndef HAVE_LOCALTIME_R +struct tm *localtime_r(const time_t *, struct tm *); +#endif #ifndef HAVE_FMTCHECK const char *fmtcheck(const char *, const char *) __attribute__((__format_arg__(2))); diff --git a/src/gmtime_r.c b/src/gmtime_r.c new file mode 100644 index 00000000..2f4d0228 --- /dev/null +++ b/src/gmtime_r.c @@ -0,0 +1,19 @@ +/* $File: asctime_r.c,v 1.1 2012/05/15 17:14:36 christos Exp $ */ + +#include "file.h" +#ifndef lint +FILE_RCSID("@(#)$File: asctime_r.c,v 1.1 2012/05/15 17:14:36 christos Exp $") +#endif /* lint */ +#include +#include + +/* asctime_r is not thread-safe anyway */ +struct tm * +gmtime_r(const time_t t, struct tm *tm) +{ + struct tm *tmp = gmtime(t); + if (tmp == NULL) + return NULL; + memcpy(tm, tmp, sizeof(*tm)); + return tmp; +} diff --git a/src/localtime_r.c b/src/localtime_r.c new file mode 100644 index 00000000..a8eaaba3 --- /dev/null +++ b/src/localtime_r.c @@ -0,0 +1,19 @@ +/* $File: asctime_r.c,v 1.1 2012/05/15 17:14:36 christos Exp $ */ + +#include "file.h" +#ifndef lint +FILE_RCSID("@(#)$File: asctime_r.c,v 1.1 2012/05/15 17:14:36 christos Exp $") +#endif /* lint */ +#include +#include + +/* asctime_r is not thread-safe anyway */ +struct tm * +localtime_r(const time_t t, struct tm *tm) +{ + struct tm *tmp = localtime(t); + if (tmp == NULL) + return NULL; + memcpy(tm, tmp, sizeof(*tm)); + return tmp; +} diff --git a/src/print.c b/src/print.c index 501236f6..25d893a5 100644 --- a/src/print.c +++ b/src/print.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: print.c,v 1.77 2015/01/02 21:29:39 christos Exp $") +FILE_RCSID("@(#)$File: print.c,v 1.78 2015/01/06 02:04:10 christos Exp $") #endif /* lint */ #include @@ -233,7 +233,7 @@ file_fmttime(uint64_t v, int flags, char *buf) { char *pp; time_t t; - struct tm *tm; + struct tm *tm, tmz; if (flags & FILE_T_WINDOWS) { struct timespec ts; @@ -246,30 +246,13 @@ file_fmttime(uint64_t v, int flags, char *buf) } if (flags & FILE_T_LOCAL) { - pp = ctime_r(&t, buf); + tm = localtime_r(&t, &tmz); } else { -#ifndef HAVE_DAYLIGHT - private int daylight = 0; -#ifdef HAVE_TM_ISDST - private time_t now = (time_t)0; - - if (now == (time_t)0) { - struct tm *tm1; - (void)time(&now); - tm1 = localtime(&now); - if (tm1 == NULL) - goto out; - daylight = tm1->tm_isdst; - } -#endif /* HAVE_TM_ISDST */ -#endif /* HAVE_DAYLIGHT */ - if (daylight) - t += 3600; - tm = gmtime(&t); - if (tm == NULL) - goto out; - pp = asctime_r(tm, buf); + tm = gmtime_r(&t, &tmz); } + if (tm == NULL) + goto out; + pp = asctime_r(tm, buf); if (pp == NULL) goto out; -- 2.40.0