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 <stdio.h>
> 
> #include <time.h>
> 
>  
> 
> 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 <stdio.h>
#include <time.h>

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
> [EMAIL PROTECTED]:/usr/obj/usr/src/sys/SMP  i386
> 
>  
> 
>  
> 
> Thank for the help!
> 
>  
> 

-- 
<Insert your favourite quote here.>
Erik Trulsson
[EMAIL PROTECTED]
_______________________________________________
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"

Reply via email to