Because my partner’s travelling, I’ve developed a sudden interest in knowing the time in Santa Fe, New Mexico, USA, and I wanted to do this with Applescript.
Almost a year ago I wrote Speak the time or date with Applescript, and you’ll want to refer to that for some background information.
The timezone problem
The problem with the script I created to tell me the time and date is that it only works for the timezone the user is in. While I already know the time in Wellington, New Zealand, Santa Fe is about 20 hours behind us. That means when it’s 1 pm Saturday in New Zealand it’s only 5 pm Friday in Santa Fe.
Usually I find it easier to add hours and subtract a day, like this: Santa Fe is 4 hours ahead: 5 pm, and 1 day behind: Friday (as I write).
I don’t need to know the year or month, so I wrote the script to get the day of the week, the hours and minutes.
Here’s the script I eventually came up with:
-- Tells me the date in Santa Fe and Los Angeles. -- Santa Fe is 20 hours behind NZ time; LA is 21 hours behind -- at the moment, anyway. Daylight Savings is likely to change that. -- thanks to -- http://erikslab.com/2007/11/26/date-time-calculations-using-applescript/ -- for how to subtract hours set myDate to current date set santafeDate to myDate - (20 * hours) set laDate to myDate - (21 * hours) set santafeHours to hours of santafeDate set laHours to hours of laDate set myMinutes to minutes of myDate set sfDay to weekday of santafeDate as string say "Now in Santa Fe it is " & santafeHours & ":" & myMinutes & " on " & sfDay say "Now in Los Angeles it is " & laHours & ":" & myMinutes & " on " & sfDay
How to subtract time?
Initially I had no idea how to find the time in another place. After considerable Googling on words like
timezone and trying to subtract numbers from the date in the script I eventually found help at Erik’s Lab: Date-/Time-Calculations using AppleScript. That gave me the very simple 2 lines I needed to subtract 20 hours from my time to get Santa Fe’s time.
My own previous post was helpful
The other surprising thing was that amongst my searches I found my own post from last year (linked above). That was particularly helpful, I’m glad to say, giving me the way to extract hours, months and so on from the Current Date.
The Daylight Savings problem
Daylight Savings is a problem — the script doesn’t work out any timezones. For this little script I’ve simply assigned Santa Fe time to be a fixed 20 hours behind New Zealand.
We are currently in Daylight Savings Time, but will come out of it on 03 April 2011. I have no idea if or when Santa Fe moves into Daylight Savings Time. For me, just now, it doesn’t matter.