Subject: Re: 25.0.50; encode-time not working as expected

From: Paul Eggert <[email-masked]>
Date: Wed, 25 Feb 2015 09:41:45 -0800

Thanks for the bug report.  My guess is that there's an incompatibility with 
FreeBSD 10.1 amd64 mktime.  I can't reproduce the problem on FreeBSD 9.1 x86.

Please try the attached patch, just for debugging, and then run the following 
one-line shell command:

src/emacs -Q -batch -eval '(progn (setenv "TZ" "Asia/Kolkata") (print 
(encode-time 44 42 6 15 2 2015 0 nil 0)))'

What output do you get?  Here's what I get on Fedora 21 x86-64, which seems correct:

oldtz=Asia/Kolkata tz=XXX-0:00:00 oldTZ=Asia/Kolkata TZ=XXX-0:00:00 2015-02-15 
06:42:44 -1 -> 2015-02-15 06:42:44 0 = 1423982564

Assuming you get different output, can you debug Emacs with GDB to send us more 
details about what's going wrong?  If not, can you give me access to a FreeBSD 
10.1 amd64 machine like yours?
diff --git a/src/editfns.c b/src/editfns.c
index dbcb316..fca95d5 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -1423,10 +1423,28 @@ mktime_z (timezone_t tz, struct tm *tm)
       oldtz = strcpy (oldtzcopy, oldtz);
     }
   block_input ();
+  char const *oldTZ = getenv ("TZ");
+  if (oldTZ)
+    oldTZ = xstrdup (oldTZ);
   set_time_zone_rule (tz);
+  char const *TZ = getenv ("TZ");
+  fprintf (stderr,
+	   ("oldtz=%s tz=%s oldTZ=%s TZ=%s "
+	    "%.4d-%.2d-%.2d %.2d:%.2d:%.2d %d -> "),
+	   oldtz ? oldtz : "(null)",
+	   tz ? tz : "(null)",
+	   oldTZ ? oldTZ : "(null)",
+	   TZ ? TZ : "(null)",
+	   tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
+	   tm->tm_hour, tm->tm_min, tm->tm_sec,
+	   tm->tm_isdst);
   time_t t = mktime (tm);
   set_time_zone_rule (oldtz);
   unblock_input ();
+  fprintf (stderr, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d %d = %ld",
+	   tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
+	   tm->tm_hour, tm->tm_min, tm->tm_sec,
+	   tm->tm_isdst, (long) t);
   SAFE_FREE ();
   return t;
 }