Re: [Chicken-users] choosing right timezone for future date

2008-01-29 Thread Graham Fawcett
On Jan 28, 2008 5:27 PM, John Cowan <[EMAIL PROTECTED]> 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 
#include 
#include 

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


___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] choosing right timezone for future date

2008-01-28 Thread Kon Lovett


On Jan 28, 2008, at 2:27 PM, John Cowan wrote:


Graham Fawcett scripsit:


My TZ is set properly, and using strace I see that the correct
zoneinfo file is being accessed. "date +%Z" works properly. Any  
ideas?






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.


The TZ database contains historical DST rules. The usual Unix  
suspects provide a compiled form (Windows doesn't use the TZ database),


SLIB has code to read the binary form (tzfile.scm) and use the info  
(timecore.scm & timezone.scm).


(I have been meaning to steal the algorithms for the srfi-19 egg.)



I admit I don't know how to make progress here.  Let me think on it.

--
John Cowan  [EMAIL PROTECTED]   http://www.ccil.org/~cowan
O beautiful for patriot's dream that sees beyond the years
Thine alabaster cities gleam undimmed by human tears!
America! America!  God mend thine every flaw,
Confirm thy soul in self-control, thy liberty in law!
-- one of the verses not usually taught in U.S. schools


___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


Best Wishes,
Kon




___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] choosing right timezone for future date

2008-01-28 Thread John Cowan
Graham Fawcett scripsit:

> My TZ is set properly, and using strace I see that the correct
> zoneinfo file is being accessed. "date +%Z" works properly. Any ideas?

The problem with your test program is that tm is uninitialized, which
strptime does not alter (since there is no zone information in the input)
and which causes strftime to bomb.  Using memset(&tm, 0, sizeof(tm))
causes %z to be + (wrong) and %Z to be "EST" (correct).  So that's
not a feasible approach.

Here's a demo program that does work:

#include 
#include 
#include 

int main() {
  struct tm * tm;
  char buf[1000];
  time_t t;

  buf[0] = 0;
  t = time(NULL);
  tm = localtime(&t);
  strptime("2008-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", tm);
  strftime(buf, sizeof(buf), "%d %b %Y %H:%M %z %Z", tm);
  puts(buf);
  return 0;
}

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 admit I don't know how to make progress here.  Let me think on it.

-- 
John Cowan  [EMAIL PROTECTED]   http://www.ccil.org/~cowan
O beautiful for patriot's dream that sees beyond the years
Thine alabaster cities gleam undimmed by human tears!
America! America!  God mend thine every flaw,
Confirm thy soul in self-control, thy liberty in law!
-- one of the verses not usually taught in U.S. schools


___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] choosing right timezone for future date

2008-01-28 Thread Graham Fawcett
On Jan 28, 2008 3:45 PM, Graham Fawcett <[EMAIL PROTECTED]> wrote:
> > strftime(3), which should be in unit posix but isn't, is your friend;
> > it's the library routine underlying date(1).

John, I'm seeing an odd behaviour with strftime on my machine; perhaps
you've seen it before and might be able to spot the problem.

The manpage for strptime gives the following example program, to which
I added "%Z %z" in the strftime line:

#include 
#include 

int main() {
  struct tm tm;
  char buf[255];

  strptime("2008-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm);
  strftime(buf, sizeof(buf), "%d %b %Y %H:%M %Z %z", &tm);
  puts(buf);
  return 0;
}

but the output I get is:
$ ./a.out
12 Nov 2008 18:31 \360\204

My TZ is set properly, and using strace I see that the correct
zoneinfo file is being accessed. "date +%Z" works properly. Any ideas?

Thanks,
Graham


___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] choosing right timezone for future date

2008-01-28 Thread Graham Fawcett
On Jan 28, 2008 2:12 PM, John Cowan <[EMAIL PROTECTED]> wrote:
> Graham Fawcett scripsit:
>
> > This may not be Chicken-specific, but is there an existing library
> > that, given a date (year,month,day) and given the current locale, will
> > tell me the timezone in which the date falls? I
>
> The locale as such is not relevant to time zones, which are stored in
> the TZ environment variable.  ("en_US" is just one locale, but it
> spans eight time zones.)

D'oh, right.

> > I can't find anything in SRFI-19 to help me here. Using date(1), I can
> > do 'date -d "mmdd" +%z' but that seems silly...
>
> strftime(3), which should be in unit posix but isn't, is your friend;
> it's the library routine underlying date(1).

Thanks, John.

Graham


___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] choosing right timezone for future date

2008-01-28 Thread John Cowan
Graham Fawcett scripsit:

> This may not be Chicken-specific, but is there an existing library
> that, given a date (year,month,day) and given the current locale, will
> tell me the timezone in which the date falls? I'm in the EST zone, but
> during warmer months, dates fall within the EDT zone. I'd like to be
> able to create a date and have the library provide the correct
> timezone. (Unix-specific answers are OK.)

The locale as such is not relevant to time zones, which are stored in
the TZ environment variable.  ("en_US" is just one locale, but it
spans eight time zones.)

> I can't find anything in SRFI-19 to help me here. Using date(1), I can
> do 'date -d "mmdd" +%z' but that seems silly...

strftime(3), which should be in unit posix but isn't, is your friend;
it's the library routine underlying date(1).

-- 
Winter:  MIT,   John Cowan
Keio, INRIA,[EMAIL PROTECTED]
Issue lots of Drafts.   http://www.ccil.org/~cowan
So much more to understand!
Might simplicity return?(A "tanka", or extended haiku)


___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users