guile-user
[Top][All Lists]
Advanced

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

Re: timezone offsets


From: Aaron VanDevender
Subject: Re: timezone offsets
Date: Thu, 22 Jun 2006 11:28:04 -0500

On Thu, 2006-06-22 at 10:57 -0500, Aaron VanDevender wrote:
> I notice that if I run:
> 
> $ guile -c '(display (strftime "%c %z\n" (localtime (current-time))))'
> Thu Jun 22 10:51:21 2006 +0500

On second thought, I think this patch is better since in keeps the
scheme and C representations of the tm structure consistent, and it also
removes the offset calculation redundancy.

Index: stime.c
===================================================================
RCS file: /sources/guile/guile/guile-core/libguile/stime.c,v
retrieving revision 1.108
diff -u -r1.108 stime.c
--- stime.c     17 Apr 2006 00:05:41 -0000      1.108
+++ stime.c     22 Jun 2006 16:26:36 -0000
@@ -355,6 +355,24 @@
     }
 }
 
+/* Calculate timezone offset in seconds east of UTC */
+static int
+getzoff (struct tm *lt, struct tm *utc)
+{
+  int zoff;
+  zoff = (lt->tm_hour - utc->tm_hour) * 3600 + (lt->tm_min - utc->tm_min) * 60
+    + lt->tm_sec - utc->tm_sec;
+  if (utc->tm_year < lt->tm_year)
+    zoff += 24 * 60 * 60;
+  else if (utc->tm_year > lt->tm_year)
+    zoff -= 24 * 60 * 60;
+  else if (utc->tm_yday < lt->tm_yday)
+    zoff += 24 * 60 * 60;
+  else if (utc->tm_yday > lt->tm_yday)
+    zoff -= 24 * 60 * 60;
+  return zoff;
+}
+
 SCM_DEFINE (scm_localtime, "localtime", 1, 1, 0,
             (SCM time, SCM zone),
            "Return an object representing the broken down components of\n"
@@ -367,7 +385,6 @@
   timet itime;
   struct tm *ltptr, lt, *utc;
   SCM result;
-  int zoff;
   char *zname = 0;
   char **oldenv;
   int err;
@@ -416,19 +433,7 @@
   if (utc == NULL || ltptr == NULL)
     SCM_SYSERROR;
 
-  /* calculate timezone offset in seconds west of UTC.  */
-  zoff = (utc->tm_hour - lt.tm_hour) * 3600 + (utc->tm_min - lt.tm_min) * 60
-    + utc->tm_sec - lt.tm_sec;
-  if (utc->tm_year < lt.tm_year)
-    zoff -= 24 * 60 * 60;
-  else if (utc->tm_year > lt.tm_year)
-    zoff += 24 * 60 * 60;
-  else if (utc->tm_yday < lt.tm_yday)
-    zoff -= 24 * 60 * 60;
-  else if (utc->tm_yday > lt.tm_yday)
-    zoff += 24 * 60 * 60;
-
-  result = filltime (&lt, zoff, zname);
+  result = filltime (&lt, getzoff(&lt, utc), zname);
   SCM_CRITICAL_SECTION_END;
   if (zname)
     free (zname);
@@ -520,7 +525,6 @@
   timet itime;
   struct tm lt, *utc;
   SCM result;
-  int zoff;
   char *zname = 0;
   char **oldenv;
   int err;
@@ -559,7 +563,6 @@
       strcpy (zname, ptr);
     }
 
-  /* get timezone offset in seconds west of UTC.  */
   /* POSIX says gmtime sets errno, but C99 doesn't say that.
      Give a sensible default value in case gmtime doesn't set it.  */
   errno = EINVAL;
@@ -573,19 +576,8 @@
   if (utc == NULL || itime == -1)
     SCM_SYSERROR;
 
-  zoff = (utc->tm_hour - lt.tm_hour) * 3600 + (utc->tm_min - lt.tm_min) * 60
-    + utc->tm_sec - lt.tm_sec;
-  if (utc->tm_year < lt.tm_year)
-    zoff -= 24 * 60 * 60;
-  else if (utc->tm_year > lt.tm_year)
-    zoff += 24 * 60 * 60;
-  else if (utc->tm_yday < lt.tm_yday)
-    zoff -= 24 * 60 * 60;
-  else if (utc->tm_yday > lt.tm_yday)
-    zoff += 24 * 60 * 60;
-
   result = scm_cons (scm_from_long (itime),
-                    filltime (&lt, zoff, zname));
+                    filltime (&lt, getzoff(&lt, utc), zname));
   if (zname)
     free (zname);
 





reply via email to

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