From owner-freebsd-questions@FreeBSD.ORG Wed Mar 17 05:30:44 2010 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 5E5C6106564A for ; Wed, 17 Mar 2010 05:30:44 +0000 (UTC) (envelope-from subscriber+freebsd@markshroyer.com) Received: from frodo.paleogene.net (frodo.paleogene.net [206.125.175.178]) by mx1.freebsd.org (Postfix) with ESMTP id 428F78FC1A for ; Wed, 17 Mar 2010 05:30:43 +0000 (UTC) Received: from auth-client.paleogene.net (auth-client.paleogene.net [206.125.175.178]) (Authenticated sender: hidden) by frodo.paleogene.net (Postfix) with ESMTPSA id 4C44B3F411 ; Wed, 17 Mar 2010 01:30:43 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=markshroyer.com; s=default; t=1268803843; bh=EOFvCGC43HtwHmpMv08skYorFmsapZP8GgYmV/nhYy4=; h=Message-ID:Date:From:MIME-Version:To:CC:Subject:References: In-Reply-To:Content-Type:Content-Transfer-Encoding; b=TxaEylBy87wJRwttEd90U2Mzh4OLz2zJdVMy8vQFpS1est5fBr+GJ1ASuk/7w7vJB nSTHkJ3VM5NVr5bdUoGt9PF1HOrZ9AnxeyM2uEmXBwvnKuVwUM70IFS2S/jbA/yydB 0h0qcIhYjjcjmMUoLrxKAY9obPNWFGfMTZ5G07AI= Message-ID: <4BA068FF.6000305@markshroyer.com> Date: Wed, 17 Mar 2010 01:30:39 -0400 From: Mark Shroyer User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.8) Gecko/20100227 Thunderbird/3.0.3 MIME-Version: 1.0 To: freebsd-questions@freebsd.org References: <7B9397B189EB6E46A5EE7B4C8A4BB7CB3B27D7F5@MBX03.exg5.exghost.com> In-Reply-To: <7B9397B189EB6E46A5EE7B4C8A4BB7CB3B27D7F5@MBX03.exg5.exghost.com> X-Enigmail-Version: 1.0.1 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Cc: Peter Steele Subject: Re: How to make a process detect time zone change? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 17 Mar 2010 05:30:44 -0000 On 3/16/2010 11:23 AM, Peter Steele wrote: > We have a system controlled through a Java GUI and one of the > commands provided in the GUI is to change the date/time, including > the time zone. When the time zone is changed the FreeBSD system > immediately recognizes the change (that is, the date command from the > command line shows the correct time and time zone). However, our > running C apps do not recognize that a time zone change has occurred > unless they are restarted. What's the proper way to inform an active > process that a time zone change has occurred? I've tried tzset() and > tzsetwall() but neither seem to do the trick. The only thing I've > found that works is to restart the process, and that's not really a > solution. I think I have a solution. First I tried the following code: > #include > #include > #include > > int main(int argc, char* argv[]) > { > time_t now; > struct tm localNow; > > for (;;) { > time(&now); > tzsetwall(); > localtime_r(&now, &localNow); > printf("%02d:%02d:%02d %s\n", localNow.tm_hour, > localNow.tm_min, > localNow.tm_sec, > localNow.tm_zone); > sleep(1); > } > > return 0; > } While this was running I set /etc/localtime to a different time zone, and sure enough, my process failed to pick up the new zone until I killed and restarted it. However, when I passed the environment variable TZ=/etc/localtime to the program: > $ env TZ=/etc/localtime ./a.out > 06:11:30 MET > 06:11:31 MET > 06:11:32 MET > 06:11:33 MET > 01:11:34 EDT > 01:11:35 EDT > 01:11:36 EDT > ^C$ So there it is: set TZ=/etc/localtime and use tzsetwall() to update the time zone within the process. In my reading, the tzsetwall(3) documentation does seem to imply tzsetwall() would check /etc/localtime even if TZ isn't set, but apparently this isn't the case; maybe the man page could be clarified on this point? Anyway, I hope this helps... -- Mark Shroyer http://markshroyer.com/contact/