[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] choosing right timezone for future date
From: |
Graham Fawcett |
Subject: |
Re: [Chicken-users] choosing right timezone for future date |
Date: |
Tue, 29 Jan 2008 09:30:01 -0500 |
On Jan 28, 2008 5:27 PM, John Cowan <address@hidden> wrote:
> Graham Fawcett scripsit:
> Here's a demo program that does work: [snip]
> However, this doesn't really solve your problem, because it sets
> the offset and tzname to the current values, not to the values in
> effect at the time.
I think I've got it. It's localtime(3) that sets the tz, so converting
to time_t and back invokes the TZ lookup:
#include <string.h>
#include <stdio.h>
#include <time.h>
void get_tz(int y, int m, int d, int hh, int mm, char *buf) {
struct tm tm;
time_t t;
if (hh == -1) { // if no hour, assume noon.
hh = 12; mm = 0;
}
memset(&tm, 0, sizeof(tm)); // just to be safe
tm.tm_year = y - 1900; tm.tm_mon = m; tm.tm_mday = d;
tm.tm_hour = hh; tm.tm_min = mm;
t = mktime(&tm);
localtime_r(&t, &tm); // localtime sets the tz.
strftime(buf, sizeof(buf), "%Z", &tm);
}
int main() {
char buf[200];
get_tz(2008, 1, 1, 0, 0, buf); // January 1
printf("%s\n", buf);
get_tz(2008, 8, 1, 0, 0, buf); // August 1
printf("%s\n", buf);
}
$ ./a.out
EST
EDT
$ TZ=Europe/London ./a.out
GMT
BST
Thanks John & Kon for your help, you set me on the right path.
Graham