From owner-freebsd-questions@FreeBSD.ORG Mon Mar 24 09:12:08 2008 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 B1265106564A for ; Mon, 24 Mar 2008 09:12:08 +0000 (UTC) (envelope-from erikt@midgard.homeip.net) Received: from ch-smtp02.sth.basefarm.net (ch-smtp02.sth.basefarm.net [80.76.149.213]) by mx1.freebsd.org (Postfix) with ESMTP id 4D8D18FC18 for ; Mon, 24 Mar 2008 09:12:08 +0000 (UTC) (envelope-from erikt@midgard.homeip.net) Received: from c83-253-25-183.bredband.comhem.se ([83.253.25.183]:58541 helo=falcon.midgard.homeip.net) by ch-smtp02.sth.basefarm.net with esmtp (Exim 4.68) (envelope-from ) id 1Jdiit-0002UE-70 for freebsd-questions@freebsd.org; Mon, 24 Mar 2008 10:12:07 +0100 Received: (qmail 47231 invoked from network); 24 Mar 2008 10:12:03 +0100 Received: from owl.midgard.homeip.net (10.1.5.7) by falcon.midgard.homeip.net with ESMTP; 24 Mar 2008 10:12:03 +0100 Received: (qmail 13306 invoked by uid 1001); 24 Mar 2008 10:12:03 +0100 Date: Mon, 24 Mar 2008 10:12:03 +0100 From: Erik Trulsson To: fred Message-ID: <20080324091203.GA12725@owl.midgard.homeip.net> Mail-Followup-To: fred , freebsd-questions@freebsd.org References: <007d01c88d21$4440be30$ccc23a90$@com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <007d01c88d21$4440be30$ccc23a90$@com> User-Agent: Mutt/1.5.17 (2007-11-01) X-Originating-IP: 83.253.25.183 X-Scan-Result: No virus found in message 1Jdiit-0002UE-70. X-Scan-Signature: ch-smtp02.sth.basefarm.net 1Jdiit-0002UE-70 397c4007f80ce3f7af8e768d5bf7371b Cc: freebsd-questions@freebsd.org Subject: Re: Timezone problem 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: Mon, 24 Mar 2008 09:12:08 -0000 On Sun, Mar 23, 2008 at 04:05:39PM -0400, fred wrote: > Hello everyone, > > > > First of all, sorry for the terrible English I will do my best, also I don't > have much programming knowledge only some PHP. > > > > I am having issues with a software that I run on my FreeBSD server > (6.2-RELEASE). Here is a simple demonstration of the problem: > > > > This code: > > > > // CODE START > > #include > > #include > > > > int main() { > > extern long timezone1; > > > > tzset(); > > > > printf("timezone is %d\n", timezone); > > printf("tzname[0] is %s\n", tzname[0]); > > printf("tzname[1] is %s\n", tzname[1]); > > return 0; > > } > > // CODE END > > > > > > Give this result: > > > > timezone is 134513672 > > tzname[0] is EST > > tzname[1] is EDT > > > > > > The value of "timezone" should be "14400" which is the difference between my > timezone (EDT) and UTC in seconds. What makes you think that that should be the value of 'timezone'? It should not be. You have not declared any variable with that name, nor does there exist any variable with that name in the standard library. What does exist is a function timezone() (See the timezone(3) manpage for information on that function. It is not very useful.) Now, in C a function name all by itself is equivalent to a pointer to that function. The value '134513672' you get is simply the value of that pointer. If you had compiled your programs with all warnings enabled (use -Wall) then the compiler would have complained that the argument to printf does not match the format. ("%d" makes printf expect an integer, but you pass it a pointer.) Also, I am not sure that tzset(3) is guaranteed to initialize the tzdata[] array, nor is tzset(3) all that portable (nor is usage of the tzdata[] array very portable for that matter.) A better (as in: working) version of your program would be the following: #include #include int main() { struct tm *lt; time_t t; t = time(NULL); lt = localtime(&t); printf("My timezone is %s\n", lt->tm_zone); printf("timezone offset is %ld seconds\n", lt->tm_gmtoff); return 0; } It is still not fully portable (the 'tm_zone' and 'tm_gmtoff' fields are non-standard extenstions to 'struct tm'), but it makes use only of documented features of FreeBSD. A standard compliant solution would be to use localtime(3) in conjunction with strftime(3), using the "%z" and "%Z" formats to strftime. (The "%z" format is part of C99, but not of C89, so it will not be supported by many older compilers.) > This problem only appeared when we went > from EST to EDT (daylight saving time) on march 9th. Anyone knows why I am > getting "134513672" ? > > > > Here is some more information about my system: > > > > # date > > Sat Mar 22 15:24:42 EDT 2008 > > # date -u > > Sat Mar 22 19:24:45 UTC 2008 > > # gcc -v > > Using built-in specs. > > Configured with: FreeBSD/i386 system compiler Thread model: posix gcc > version 3.4.6 [FreeBSD] 20060305 > > # uname -a > > FreeBSD 6.2-RELEASE FreeBSD 6.2-RELEASE #0: Fri Jan 12 11:05:30 UTC 2007 > root@dessler.cse.buffalo.edu:/usr/obj/usr/src/sys/SMP i386 > > > > > > Thank for the help! > > > -- Erik Trulsson ertr1013@student.uu.se