Finding current Olson tz

In some cases, I need to find the current Olson time zone settings of the system. The only solution I found is something like

set f to (POSIX file "/etc/localtime") as text
set od to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set {continent, location} to text items -2 thru -1 of f
set AppleScript's text item delimiters to od
continent & "/" & location

This properly returns something like “America/New_York” but looks pretty much like a hack. Has anybody a better (i.e. cleaner) idea?

Jürgen

Hi,

another way is


text 12 thru -1 of (do shell script "systemsetup -gettimezone")

Good idea, Stefan. “systemsetup” escaped me.

Jürgen

Here’s another method, which is about 10 times faster on my 10.6.8 machine:

tell application "System Events"
	set TZone to (get value of property list item "TimeZoneName" of property list item "com.apple.preferences.timezone.selected_city" of property list file ((POSIX file "/" as text) & "Library:Preferences:.GlobalPreferences.plist"))
end tell

I wouldn’t call accessing /etc/localtime a hack. It’s Unix, should be quite stable - more so than OS X, I’d say.
Or were you referring to the POSIX file bit?

I’d think exploiting the ‘POSIX file’ behaviour was more of a hack than following the link, which can be done more respectably like this:

do shell script "readlink '/etc/localtime' | sed -E 's|^.+/zoneinfo/||'"

Or for those of you for whom sed remains a mystery:

set LT to do shell script "readlink '/etc/localtime'"
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/zoneinfo/"
set timeZone to text item 2 of LT
set AppleScript's text item delimiters to tid