Suchen / SearchNavigation |
AppleScript-Daten in UTC umwandeln8 Jan 2012 - 15:08 Dies ist ein einfacher Handler zum Wandeln eines AppleScript Datums in Universal Time Coordinated, auch Mittlere Greenwich-Zeit genannt. (Der umgekehrte Handler liegt unter UTC-Datum zu Ortszeit und -datum AppleScript) AppleScript kennt die Anweisung Dies scheitert für beliebige Daten. Befinden Sie sich gerade im Jahresabschnitt mit Standardzeit und versuchen, ein Datum mit Sommerzeit zu konvertieren, ist das Ergebnis falsch. Der folgende Handler benutzt die Konvertierung zu Property Lists durch System Events. Das wandelt jedes Datum zu UTC basierend auf der Olson-Zeitzone Ihres Mac. Diese Zeitzonen kennen die Regeln für Zeitänderungen seit 1970. Auch Daten for Jahrzehnten werden deshalb korrekt konvertiert. Da der Aufruf von System Events Zeit kostet, kann der Handler auch eine Liste von Daten im Aufruf bearbeiten. Das Ergebnis ist ein Datum, wenn das Argument ein Datum war, aber eine Liste von Daten, wenn das Argument eine Liste von Daten war.
on date_to_utc(list_arg)
-- change the value of a date or a list of dates to
-- the Universal Time Coordinated time zone
-- Argument can be a date or a list of dates
-- Result for a date is a date,
-- result for a list is a list
-- make sure, argument is list. Store original state
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
-- serialize arguments to plist using system events
tell application "System Events"
set plist to (make new property list item with properties {value:list_arg})
set strg to text of plist
end tell
-- create some arbitrary date for manipulation
copy item 1 of list_arg to date_obj
-- raise error if first element of list is not convertable to date
try
set date_obj to date_obj as date
on error the_error number errnum
error the_error & " In handler date_to_utc." number errnum
end try
set {time of date_obj, day of date_obj, month of date_obj} to {0, 1, 1}
-- find date entries in plist
set old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"", ""}
set temp_list to every text item of strg
set r_list to text items 2 thru -2 of strg
set datestrings to {}
repeat with c from 2 to count temp_list by 2
copy item c of temp_list to end of datestrings
end repeat
-- Here, we have a list of strings, representing
-- dates in ISO notation for Zulu time zone
-- converting the strings
set r to {}
set AppleScript's text item delimiters to {"-", "T", ":", "Z"}
repeat with J from 1 to (count datestrings)
set curr to item J of datestrings
log "curr: " & curr
set tl to every text item of curr
-- result has 7 elements, the last one is empty
log tl
set curr to items 1 thru 6 of curr
-- here we have a list of year, month, day, hour, minute and second as strings
copy date_obj to ld
-- next will set date_ob and implicitelly change strings to integers
set {year of ld, month of ld, day of ld, hours of ld, minutes of ld, seconds of ld} to tl
copy ld to end of r
end repeat
if (count r) ? (count list_arg) then
-- this should occur only if some of the entries in the plist were
-- of other types
error "Some values are no dates in handler date_to_utc." number 1700
end if
-- if original argument was single, make return value single agein
if not type_is_list then
set r to item 1 of r
end if
set AppleScript's text item delimiters to old_delims
return r
end date_to_utc
Beispiel: Auf meiner Maschine, eingestellt sind deutsche Formate und die Zeitzone Europe/Berlin, liefert
date_to_utc({current date, date ("15.5.2010 12:00:00"), date ("15.5.1975 12:00:00")})
das Ergebnis
{date "Sonntag, 8. Januar 2012 14:55:54", date "Samstag, 15. Mai 2010 10:00:00", date "Donnerstag, 15. Mai 1975 11:00:00"}
Beachten Sie, das im Mai 2012 und im Mai 1975 die Umwandlung verschieden ist. Wir hatten keine Sommerzeit in den Siebzigern.
|
AppleScript Datumsobjekte und -berechnungen |