[plt-scheme] how to get GMT time?

From: Paul Graham (pg at ycombinator.com)
Date: Wed May 20 02:19:31 EDT 2009

Thanks, Eli, the TZ environment var looks like exactly
what I was looking for.  I'm ok with all times being GMT.
So if I just set that to be ":GMT", all times will be GMT,
with no worries about daylight savings time, right?

--pg

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")
>
> 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
> savings.
>
> --
>          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
>                  http://www.barzilay.org/                 Maze is Life!
>


Posted on the users mailing list.