Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2016-02-16
21:59 • Closed ticket [c9eb6b0ac0]: ConvertLocalToUTCUsingC fails the first time if TZ i... artifact: 7358efa0f7 user: gahr
2015-12-11
10:18
Fix [c9eb6b0ac01bb8ef96a616c71426a3db4a279bec|c9eb6b0ac0]: ConvertLocalToUTCUsingC fails the first t... check-in: 38e14f6cc3 user: jan.nijtmans tags: trunk
10:13
Fix [c9eb6b0ac01bb8ef96a616c71426a3db4a279bec|c9eb6b0ac0]: ConvertLocalToUTCUsingC fails the first t... check-in: 0201d51279 user: jan.nijtmans tags: core-8-5-branch
2015-12-10
12:10 • New ticket [c9eb6b0ac0] ConvertLocalToUTCUsingC fails the first time if TZ is no... artifact: 267e170a2e user: anonymous

ConvertLocalToUTCUsingC fails the first time if TZ is not set

Closedc9eb6b0ac01bb8ef96a616c71426a3db4a279becCreated 11 years agoUpdated 11 years ago
Descriptionwiki2015-12-10 12:10:34
This is with fossil trunk on FreeBSD 10.2-RELEASE-p7 amd64.

If TZ is not set in the environment, the ConvertLocalToUTCUsingC will fail the first time it is called. To avoid this, make sure TzsetIfNecessary() always calls tzset the first time.

Index: generic/tclClock.c
==================================================================
--- generic/tclClock.c
+++ generic/tclClock.c
@@ -2005,25 +2005,28 @@
 static void
 TzsetIfNecessary(void)
 {
     static char *tzWas = NULL;	/* Previous value of TZ, protected by
 				 * clockMutex. */
+    static char first = 1;	/* First time? */
     const char *tzIsNow;	/* Current value of TZ */
 
     Tcl_MutexLock(&clockMutex);
     tzIsNow = getenv("TZ");
-    if (tzIsNow != NULL && (tzWas == NULL || strcmp(tzIsNow, tzWas) != 0)) {
+    int mod = 	((size_t)tzWas ^ (size_t)tzIsNow) || 
+		((size_t)tzWas & (size_t)tzIsNow && strcmp(tzIsNow, tzWas) != 0);
+    if (first || mod) {
+	first = 0;
 	tzset();
 	if (tzWas != NULL) {
 	    ckfree(tzWas);
+	    tzWas = NULL;
 	}
-	tzWas = ckalloc(strlen(tzIsNow) + 1);
-	strcpy(tzWas, tzIsNow);
-    } else if (tzIsNow == NULL && tzWas != NULL) {
-	tzset();
-	ckfree(tzWas);
-	tzWas = NULL;
+	if (tzIsNow != NULL) {
+	    tzWas = ckalloc(strlen(tzIsNow) + 1);
+	    strcpy(tzWas, tzIsNow);
+	}
     }
     Tcl_MutexUnlock(&clockMutex);
 }
 
 /*

Using this test driver

package require clock::iso8601

proc doit {} {
    try {
        ::tcl::clock::ConvertLocalToUTC {
            tzName :localtime
            century 19 
            yearOfCentury 94
            month 11
            dayOfMonth 5
            era CE
            year 1994
            julianDay 2449662
            secondOfDay 0
            localSeconds 783993600} {} 2299161
    } on error msg {
        return "$msg\n[info errorstack]"
    }
}

puts [doit]
puts [doit]

I get this with a non-patched version of Tcl:

time value too large/small to represent
INNER {invokeStk1 ::tcl::clock::ConvertLocalToUTC {
            tzName :localtime
            century 19 
            yearOfCentury 94
            month 11
            dayOfMonth 5
            era CE
            year 1994
            julianDay 2449662
            secondOfDay 0
            localSeconds 783993600} {} 2299161} CALL doit
tzName :localtime century 19 yearOfCentury 94 month 11 dayOfMonth 5 era CE year 1994 julianDay 2449662 secondOfDay 0 localSeconds 783993600 seconds 783993600

And this with a patched version:

tzName :localtime century 19 yearOfCentury 94 month 11 dayOfMonth 5 era CE year 1994 julianDay 2449662 secondOfDay 0 localSeconds 783993600 seconds 783993600
tzName :localtime century 19 yearOfCentury 94 month 11 dayOfMonth 5 era CE year 1994 julianDay 2449662 secondOfDay 0 localSeconds 783993600 seconds 783993600

Referenced by2
gahrwiki2016-02-16 21:59:09

Fixed by checkin [38e14f6cc3]

Log in (anonymous is fine) to add a comment.