This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
[Patch] RFE new API - gettimezone
- From: P J P <pj dot pandit at yahoo dot co dot in>
- To: "libc-help at sourceware dot org" <libc-help at sourceware dot org>
- Date: Wed, 2 Apr 2014 02:47:35 +0800 (SGT)
- Subject: [Patch] RFE new API - gettimezone
- Authentication-results: sourceware.org; auth=none
- Reply-to: P J P <pj dot pandit at yahoo dot co dot in>
Hello,
WRT BZ#1077902 [1], please find attached herein, a patch to add the new API - gettimezone(), which returns local time zone information in the form of a POSIX TZ environment variable string. The API reads the '/etc/localtime' file to fetch the said information.
Could someone please help me with the patch review or any other comments that would be helpful in the development of this API.
--
[1] https://bugzilla.redhat.com/show_bug.cgi?id=1077902#c2
Thank you.
---
Regards
-Prasad
http://feedmug.com
diff --git a/time/gettimezone.c b/time/gettimezone.c
new file mode 100644
index 0000000..c16326a
--- /dev/null
+++ b/time/gettimezone.c
@@ -0,0 +1,109 @@
+/* Copyright (C) 1991-2014 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+
+#define TZONEVERSN '2'
+#define TZONEMAGIC "TZif"
+#define LOCALTZONE "/etc/localtime"
+
+
+/* gettimezone: reads local timezone definition from '/etc/localtime'
+ and returns a POSIX TZ environment variable string or NULL in case
+ of an error; See: tzfile(5), tzset(3).
+
+ std offset dst [offset],start[/time],end[/time]
+
+ Ex: TZ="NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0" */
+char *
+gettimezone (const char *tzname)
+{
+ int n = 0;
+ FILE *fp = NULL;
+ char *tz = NULL, s[4];
+
+ errno = 0;
+ if (!tzname)
+ tzname = LOCALTZONE;
+
+ fp = fopen (tzname, "rce");
+ if (!fp)
+ return tz;
+
+ if (fread (s, sizeof (char), 4, fp) != 4)
+ goto err;
+ if (strncmp (TZONEMAGIC, s, 4))
+ goto err;
+
+ if (fread (s, sizeof (char), 1, fp) != 1)
+ goto err;
+ if (TZONEVERSN != *s)
+ goto err;
+
+ if (fseek (fp, -1, SEEK_END) < 0)
+ goto err;
+ if (fread (s, sizeof (char), 1, fp) != 1)
+ goto err;
+ if ('\n' != *s)
+ goto err;
+
+ n = -2;
+ while (!fseek (fp, n, SEEK_END))
+ {
+ if (fread (s, sizeof (char), 1, fp) != 1)
+ goto err;
+ if ('\n' == *s)
+ break;
+
+ n--;
+ }
+ fseek (fp, ++n, SEEK_END);
+
+ n = abs (n + 1);
+ tz = calloc (n, sizeof (char));
+ if (!tz)
+ goto err;
+
+ if (fread (tz, sizeof (char), n, fp) != n)
+ {
+ free (tz);
+ tz = NULL;
+ }
+
+err:
+ fclose (fp);
+ return tz;
+}
+
+
+/* int
+main (int argc, char *argv[])
+{
+ char *tz = NULL;
+
+ tz = gettimezone (NULL);
+ printf ("tz: %s\n", tz);
+ if (tz)
+ free (tz);
+
+ return 0;
+} */