hours from Greenwich?

For the Compleat Fan
Locked
Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

hours from Greenwich?

Post by 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?

Code: Select all

(define (local-time-offset)
     (- (/ (time-of-day) (* 60 60 1000)) (nth 3 (now))))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

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:

Code: Select all

(define (now-local)
    (now (- (/ (time-of-day) (* 60 60 1000)) (nth 3 (now)))) )

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

My first attempt to determine the difference between my local time and Greenwich time was naive, at best.

Code: Select all

(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.

Code: Select all

(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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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

Locked