[plt-scheme] how to get GMT time?

From: Eli Barzilay (eli at barzilay.org)
Date: Wed May 20 02:04:41 EDT 2009

On May 19, Paul Graham wrote:
> I'd appreciate it if anyone can tell me if this is the standard way
> to get a GMT time from Mzscheme:
> (define (gmt-date sec)
>   (let ((offset (date-time-zone-offset (seconds->date sec))))
>     (seconds->date (- sec offset))))
> It seems ugly to have to call seconds->date twice.

I think that this is the only way to do this now, but the resulting
`date' is going to be broken with respect to its time zone offset...
But -- the `seconds->date' function uses the system to get the time,
so you can hack around this by setting your desired TZ environment
variable, for example:

  (define (current-gmt-date tz)
    (putenv "TZ" ":GMT")
    (seconds->date (current-seconds)))

or more generally

  (define (current-date/tz tz)
    (putenv "TZ" tz)
    (seconds->date (current-seconds)))
  (current-date/tz ":US/Eastern")

This is fine if you need only GMT dates, but for anything else you'll
need to prevent one thread from stepping over another's setting.

It'll probably be nice to have an optional argument for
`seconds->date' to specify the timezone, or perhaps a parameter.

> Also, is the offset always going to get me back to GMT, or do I have
> to consider daylight savings time?

Well, right now I'm getting -14400, so it does include daylight

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.