I found on our site 2 ways to convert ISO8601 (ISOT) - strings to dates.
These 2 methods using 1) Plain AppleScript and 2) AsObjC result in a 3 hours difference on my computer (see the results).
The question is: 1) which way is correct and 2) how to fix the wrong way?
I would really appreciate your help:
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
on isotText2Date(isotText) -- plain AppleScript handler
set fRef to (open for access ((path to temporary items as Unicode text) & "isot.txt") with write permission)
try
set eof fRef to 0
write isotText as string to fRef
set theDate to (read fRef from 1 as «class isot») as date
on error
set theDate to false
end try
close access fRef
return theDate
end isotText2Date
on isotText2DateAsObjC(isotText) -- AsObjC handler
set formatter to current application's NSISO8601DateFormatter's new()
set theDate to (formatter's dateFromString:isotText) as date
end isotText2DateAsObjC
set theDate to isotText2Date("2020-05-07T06:05:34Z")
--> date "Thursday, 7 May 2020 - 6:05:34 AM"
set theDate to isotText2DateAsObjC("2020-05-07T06:05:34Z")
--> date "Thursday, 7 May 2020 - 9:05:34 AM"
The ISOT method (which isn’t an official one and therefore not exactly “correct”) doesn’t transpose the UTC time to your local one, whereas the ASObjC method does. You could try adding (time to GMT) to the date object from the ISOT method, but (time to GMT) relates to the date and time when the script’s run, not necessarily to the date being added to.
I checked 2 handlers right now (using back converting method), and it seems to me, correcting the result with (time to GMT) needs only AsObjC handler:
on isotText2Date(isotText) -- plain AppleScript handler
set fRef to (open for access ((path to temporary items as Unicode text) & "isot.txt") with write permission)
try
set eof fRef to 0
write isotText as string to fRef
set theDate to (read fRef from 1 as «class isot») as date
on error
set theDate to false
end try
close access fRef
return theDate
end isotText2Date
set theDate to isotText2Date("2020-05-07T06:05:34Z")
--> date "Thursday, 7 May 2020 - 6:05:34 AM"
-- back converting (to check):
(theDate as «class isot» as string) & "Z"
--> "2020-05-07T06:05:34Z" -------> OK!!!
Without -(time to GMT) following AsObjC gives ISOT string wrong result. So, added -(time to GMT) here:
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
on isotText2DateAsObjC(isotText) -- AsObjC handler
set formatter to current application's NSISO8601DateFormatter's new()
set theDate to (formatter's dateFromString:isotText) as date
return theDate - (time to GMT)
end isotText2DateAsObjC
set theDate to isotText2DateAsObjC("2020-05-07T06:05:34Z")
--> date "Thursday, 7 May 2020 - 6:05:34 AM"
-- back converting (to check):
(theDate as «class isot» as string) & "Z"
--> "2020-05-07T06:05:34Z" -------> OK!!!
It can’t become a full-fledged equivalent because the ASObjC’s adjustment to your time zone is based on whichever of standard or daylight-saving time is in force in your zone at the time of the GMT date. time to GMT returns the difference between your local time and GMT at the moment the script’s run, so it only produces the correct result if the input date and the running of the script occur in the same period of the year.