hours from Greenwich?

Started by Sammo, December 30, 2003, 11:43:30 AM

Previous topic - Next topic

Sammo

Is this a good way to calculate the number of hours my time zone differs from Greenwich?  Is there a more direct way of asking a Windows-based system?
(define (local-time-offset)
     (- (/ (time-of-day) (* 60 60 1000)) (nth 3 (now))))

Lutz

#1
I am not very famliar anymore with the Win32 API, perhaps something is available for 'import'.



Your function has the advantage, that it is platform independent, because 'time-of-day' always will always refer to the local timezone and 'now' will give always universal time (GMT).



Lutz

Sammo

#2
Hi Lutz,



Thanks for the answer.  I like this function, too, exactly because it doesn't depend on the OS (and is therefore portable), and doesn't depend on the availability or functionality of Win32 DLLs.



I guess I was asking if there was a more direct way of asking newLISP for the local time zone offset.  That is, was I overlooking a built-in function?  I guess I wasn't, and I'm happy with this -- especially after bundling it with 'now' in the following 'now-local' function:
(define (now-local)
    (now (- (/ (time-of-day) (* 60 60 1000)) (nth 3 (now)))) )

Sammo

#3
My first attempt to determine the difference between my local time and Greenwich time was naive, at best.
(define (local-timezone-offset)
    (- (/ (time-of-day) (* 60 60 1000)) (nth 3 (now))))
The idea was simplistic and fails between 5:00 PM and midnight in Loveland Colorado because Greenwich has advanced to the next day and the Greenwich clock reads 17 hours less than instead of 7 hours greater than my local clock.  The following function attempts to remedy this defect by computing the clock time (hour only) for all world time zones and finding my local hour in that spectrum.
(define (local-timezone-offset , myhour timezones)
    (setq myhour (/ (time-of-day) (* 60 60 1000)))
    (setq timezones (map (fn (x) (nth 3 (now x))) (sequence -11 12)))
    (+ -11 (find myhour timezones)) )
Seems to work.

Lutz

#4
The next version 7.4.4 will have an additional field in (now ...) giving 'minutes west of GMT'. This number is available on Win32 using GetTimeZoneInformation() and comes with gettimeofday() on all other OSs using GCC.



Lutz