[plt-scheme] how to get GMT time?

From: YC (yinso.chen at gmail.com)
Date: Wed May 20 02:23:38 EDT 2009

On Tue, May 19, 2009 at 11:04 PM, Eli Barzilay <eli at barzilay.org> wrote:

> 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")
>

SRFI 19 makes things a bit easier if you are okay with using a different
date struct.

(require srfi/19)
(current-date 0) ;; for current time in GMT.

;; And for any second
(define (seconds->date/offset sec (offset 0))
    (julian-day->date (time-utc->julian-day (make-time 'time-utc 0 sec))
offset))


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

Daylight saving depends on the timezone (e.g. America/New_York or
America/Los_Angeles), not offset.  Eli's solution might work for you, or you
might have to parse the timezone database to determine the offset by
timezone and date if you need other than GMT.

Cheers,
yc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090519/8420f547/attachment.html>

Posted on the users mailing list.