UTC Date to Local Date/Time AppleScript

20 Jan 2012 - 11:16

date_from_utc

date_from_utc” is the inverse handler of “date_to_utc”: It takes an AppleScript date or a list of dates assumed to be in Universal Time Co-ordinated (Greenwich Mean Time) and returns them as AppleScript dates in the local time zone of the Mac.

Zur deutschen Version

The handler uses System Events and the way, it converts AppleScript data to and from property lists. The date objects are turned into ISO text expressions and a property list is set up based on them. System Events converts it to AppleScript date objects. This applies a time zone conversion according to the Olson time zone settings of your Mac.

on date_from_utc(list_arg)
	-- change the value of a date or a list of dates
	-- given as Universal Time Co-ordinated time to
	-- local time
	-- Argument can be a date or a list of dates
	-- Result for a date is a date,
	-- result for a list is a list
	set type_is_list to true
	if class of list_arg is not list then
		set list_arg to {list_arg}
		set type_is_list to false
	end if
	set string_lines to {}
	repeat with J from 1 to count list_arg
		set curr to item J of list_arg
		-- check for proper type. Everything must be date
		if class of curr is not date then
			error "Non-date value in date_from_utc." number 1700
		end if
		-- extract properties of date and prepare for proper
		-- text length
		set v_list to ¬
			{(year of curr) + 10000, ¬
				(month of curr) + 100, ¬
				(day of curr) + 100, ¬
				(hours of curr) + 100, ¬
				(minutes of curr) + 100, ¬
				(seconds of curr) + 100}
		-- convert to text, second to last character
		repeat with JI from 1 to count v_list
			set (item JI of v_list) to text 2 thru -1 of ((item JI of v_list) as text)
		end repeat
		set {y, m, d, h, min, s} to v_list
		-- build up line for property list
		copy ("<date>" & y & "-" & m & "-" & d & "T" & h & ":" & min & ":" & s & "Z</date>") to end of string_lines
	end repeat
	-- combine all lines created for property list
	set old_delims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to return
	set value_string to string_lines as text
	set AppleScript's text item delimiters to old_delims
	
	-- create property list
	set plist_string to "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<array>
" & value_string & "
</array>
</plist>
"
	-- convert to list of dates
	tell application "System Events"
		set plist to (make new property list item with properties {text:plist_string})
		set r to value of plist
	end tell
	
	-- use first element if calling argument was no list.
	if not type_is_list then
		set r to item 1 of r
	end if
	return r
end date_from_utc

Examples (I used Australian format settings and the time zone “Australia/Melbourne” for this test):

date_from_utc({date "Monday, 2 January 2012 1:00:00 PM", date "Wednesday, 16 May 2012 1:00:00 PM", date "Friday, 2 January 1970 1:00:00 PM"})

returns

{date "Tuesday, 3 January 2012 12:00:00 AM", date "Wednesday, 16 May 2012 11:00:00 PM", date "Friday, 2 January 1970 11:00:00 PM"}

All input dates are 1 am UTC. Note the different results summer and winter and in different years for the summer dates.