]> granicus.if.org Git - file/commitdiff
- stop doing time gymnastics for daylight savings.
authorChristos Zoulas <christos@zoulas.com>
Fri, 9 Jan 2015 19:28:32 +0000 (19:28 +0000)
committerChristos Zoulas <christos@zoulas.com>
Fri, 9 Jan 2015 19:28:32 +0000 (19:28 +0000)
- use the _r functions for struct tm retrieval

src/file.h
src/gmtime_r.c [new file with mode: 0644]
src/localtime_r.c [new file with mode: 0644]
src/print.c

index 5fc8d698691d0b6dd79b3617d4a8869630b272f5..4eda387c6989569b6ff675ae41c222c7ee7f4054 100644 (file)
@@ -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 (file)
index 0000000..2f4d022
--- /dev/null
@@ -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 <time.h>
+#include <string.h>
+
+/* 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 (file)
index 0000000..a8eaaba
--- /dev/null
@@ -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 <time.h>
+#include <string.h>
+
+/* 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;
+}
index 501236f6192d6a0bb6472063ebd10ce49c9c8a7d..25d893a5c245a4de3d9aca48c2b0b9b98f209ce4 100644 (file)
@@ -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 <string.h>
@@ -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;