bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-gnulib] new module: timegm


From: Simon Josefsson
Subject: [Bug-gnulib] new module: timegm
Date: Sun, 31 Aug 2003 14:14:54 +0200
User-agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.3.50 (gnu/linux)

How about a timegm module?

Tested on many platforms, but does not work on OSF/1, although the bug
is probably somewhere else.  If someone wants to investigate, here's a
way to reproduce the bug.  It should print 4711, but on OSF/1 it
prints 22711 (on a machine in the EDT time zone).  putenv instead of
setenv doesn't help.  Neither does the mktime replacement in gnulib.
It appears as if tzset() doesn't work, or some part of mktime()
doesn't use the TZ information (localtime?).

#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
// #include "config.h"

time_t my_timegm (struct tm *tm) {
  time_t ret;
  char *tz;

  tz = getenv("TZ");
  setenv("TZ", "UTC", 1);
  tzset();
  ret = mktime(tm);
  if (tz)
    setenv("TZ", tz, 1);
  else
    unsetenv("TZ");
  tzset();
  return ret;
}
int
main ()
{
  struct tm tm;
  time_t ct;

  memset (&tm, 0, sizeof (tm));

  tm.tm_year = 70;
  tm.tm_mon = 0;
  tm.tm_mday = 1;
  tm.tm_hour = 1;
  tm.tm_min = 18;
  tm.tm_sec = 31;

  ct = my_timegm (&tm);

  printf("%d\n", ct);
}

2003-08-31  Simon Josefsson  <address@hidden>

        * MODULES.html.sh (func_all_modules): Add timegm.

        * modules/timegm: New file.

2003-08-31  Simon Josefsson  <address@hidden>

        * timegm.h, timegm.c: New files.  Based on timegm man page.

2003-08-31  Simon Josefsson  <address@hidden>

        * timegm.m4: New file.

Index: MODULES.html.sh
===================================================================
RCS file: /cvsroot/gnulib/gnulib/MODULES.html.sh,v
retrieving revision 1.35
diff -u -p -r1.35 MODULES.html.sh
--- MODULES.html.sh     20 Aug 2003 19:45:05 -0000      1.35
+++ MODULES.html.sh     31 Aug 2003 11:46:57 -0000
@@ -1533,6 +1533,7 @@ func_all_modules ()
 
   func_begin_table
   func_module getdate
+  func_module timegm
   func_end_table
 
   element="Input/Output <stdio.h>"
Index: lib/timegm.c
===================================================================
RCS file: lib/timegm.c
diff -N lib/timegm.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/timegm.c        31 Aug 2003 11:46:57 -0000
@@ -0,0 +1,48 @@
+/* Convert calendar time to simple time, inverse of mktime.
+   Copyright (C) 2003 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Get getenv declaration */
+#include <stdlib.h>
+
+/* Specification.  Get tzset and mktime declaration.  */
+#include "timegm.h"
+
+/* Get setenv and unsetenv declaration */
+#include "setenv.h"
+
+/* Convert calendar time to simple time, inverse of mktime. */
+time_t
+timegm (struct tm *tm)
+{
+  time_t ret;
+  char *tz;
+
+  tz = getenv("TZ");
+  setenv("TZ", "", 1);
+  tzset();
+  ret = mktime(tm);
+  if (tz)
+    setenv("TZ", tz, 1);
+  else
+    unsetenv("TZ");
+  tzset();
+  return ret;
+}
Index: lib/timegm.h
===================================================================
RCS file: lib/timegm.h
diff -N lib/timegm.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/timegm.h        31 Aug 2003 11:46:57 -0000
@@ -0,0 +1,31 @@
+/* Convert calendar time to simple time, inverse of mktime.
+   Copyright (C) 2003 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+#ifdef HAVE_TIMEGM
+
+/* Get timegm declaration.  */
+#include <time.h>
+
+#else
+
+/* Get time_t and struct tm */
+#include <time.h>
+
+/* Convert calendar time to simple time, inverse of mktime. */
+extern time_t timegm (struct tm *tm);
+
+#endif
Index: m4/timegm.m4
===================================================================
RCS file: m4/timegm.m4
diff -N m4/timegm.m4
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ m4/timegm.m4        31 Aug 2003 11:46:57 -0000
@@ -0,0 +1,20 @@
+# timegm.m4 serial 1
+dnl Copyright (C) 2003 Free Software Foundation, Inc.
+dnl This file is free software, distributed under the terms of the GNU
+dnl General Public License.  As a special exception to the GNU General
+dnl Public License, this file may be distributed as part of a program
+dnl that contains a configuration script generated by Autoconf, under
+dnl the same distribution terms as the rest of that program.
+
+AC_DEFUN([gl_FUNC_TIMEGM],
+[
+  AC_REPLACE_FUNCS(timegm)
+  if test $ac_cv_func_timegm = no; then
+    gl_PREREQ_TIMEGM
+  fi
+])
+
+# Prerequisites of lib/timegm.c.
+AC_DEFUN([gl_PREREQ_TIMEGM], [
+  :
+])
Index: modules/timegm
===================================================================
RCS file: modules/timegm
diff -N modules/timegm
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ modules/timegm      31 Aug 2003 11:46:57 -0000
@@ -0,0 +1,22 @@
+Description:
+Convert calendar time to simple time, inverse of mktime.
+
+Files:
+lib/timegm.h
+lib/timegm.c
+m4/timegm.m4
+
+Depends-on:
+setenv
+
+configure.ac:
+gl_FUNC_TIMEGM
+
+Makefile.am:
+lib_SOURCES += timegm.h
+
+Include:
+"timegm.h"
+
+Maintainer:
+Simon Josefsson





reply via email to

[Prev in Thread] Current Thread [Next in Thread]