For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > February 2007 > Re: timezone programming









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: timezone programming
Larry W. Virden

2007-02-21, 7:10 pm

On Feb 8, 3:13 pm, vanos...@cox.net wrote:
> You will have to do the individual mappings as you said. However,
> once you have
> that its easy. For each area, set the TZ enviornment variable to the
> right
> timezone and then using something like strftime() to print out the
> current
> time in the format you choose.


Okay, i put together a prototype, just to figure out how to use the
various functions to try to generate times and dates for various
timezones. Here's the prototype's code. The problem is that the
tzset() call and putenv do not seem to be working:


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

int main(int argc, char **argv)
{
struct tm tval;
time_t clock;
int i, size;
char timestamp[4096];

tval.tm_sec =59;
tval.tm_min =59; /* 01:59:59 am */
tval.tm_hour=01;

tval.tm_mday=11;
tval.tm_mon =02; /* 03/11/2007 */
tval.tm_year=107;

clock = mktime(&tval);
if (clock == (time_t) -1) {
perror("tsttz");
return 1;
}

if (argc < 2) {
fprintf(stderr, "USAGE: %s TZ_value\n", argv[0]);
return 2;
}
for (i=1; i <= argc-1; i++) {
putenv(argv[i]);
tzset();
size = strftime(timestamp, sizeof(timestamp),
"%T %A, %B %d, %Y %Z", &tval);
if (size < 0) {
perror("tsttz");
fprintf(stderr, "%s: failed in strftime\n",
argv[0]);
}
fprintf(stdout, "%s\n", timestamp);
}

return 0;
}

I compile the above, then run it as:

tsttz :US/Pacific

and what I get out is
01:59:59 Sunday, March 11, 2007 EST

which isn't what I was expecting - I was expecting PST ...

Can someone point out (hopefully gently) what I'm missing here.

My intent is to evolve this so that I start out with a time in one of
the timezones, then show what that time maps to in multiple
timezones...

Casper H.S. Dik

2007-02-21, 7:10 pm

"Larry W. Virden" <lvirden@gmail.com> writes:

>I compile the above, then run it as:


>tsttz :US/Pacific


You're missing the fact that you need to do:

tsttz TZ=:US/Pacific

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com