delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2010/10/29/17:44:24

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-1.0 required=5.0 tests=AWL,BAYES_00,SPF_NEUTRAL,T_RP_MATCHES_RCVD
X-Spam-Check-By: sourceware.org
Message-ID: <4CCB4025.6070405@cornell.edu>
Date: Fri, 29 Oct 2010 17:44:05 -0400
From: Ken Brown <kbrown AT cornell DOT edu>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.11) Gecko/20101013 Thunderbird/3.1.5
MIME-Version: 1.0
To: cygwin <cygwin AT cygwin DOT com>
Subject: localtime and TZ
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

While trying to debug a timezone problem in the Cygwin build of emacs, I've come across a difference between Cygwin and Linux in the behavior of localtime with respect to TZ.  Suppose I set TZ, call localtime, unset TZ, and call localtime again.  On Cygwin, the second call to localtime re-uses the previous value of TZ.  On Linux, localtime reverts to giving local information, just as if TZ had never been set.  Here's a Simple Test Case:

#include <time.h>
#include <stdio.h>

extern char **environ;

void
unset_TZ (void)
{
  char **from, **to;
  for (to = from = environ; (*to = *from); from++)
    if (! (to[0][0] == 'T' && to[0][1] == 'Z' && to[0][2] == '='))
      to++;
}

int
main (void)
{
  time_t now = time ((time_t *) 0);
  printf ("TZ is initially unset; hour = %d\n", localtime (&now)->tm_hour);
  putenv ("TZ=GMT0");
  printf ("TZ=GMT0; hour = %d\n", localtime (&now)->tm_hour);
  unset_TZ ();
  printf ("TZ unset; hour = %d\n", localtime (&now)->tm_hour);
  putenv ("TZ=PST8");
  printf ("TZ=PST8; hour = %d\n", localtime (&now)->tm_hour);
  unset_TZ ();
  printf ("TZ unset again; hour = %d\n", localtime (&now)->tm_hour);
}

I run the program with TZ unset.  The output on Cygwin is

TZ is initially unset; hour = 17
TZ=GMT0; hour = 21
TZ unset; hour = 21
TZ=PST8; hour = 13
TZ unset again; hour = 13

The output on Linux is

TZ is initially unset; hour = 17
TZ=GMT0; hour = 21
TZ unset; hour = 17
TZ=PST8; hour = 13
TZ unset again; hour = 17

Is this a bug in Cygwin, or is the difference with Linux by design?

Ken

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019