Saturday, December 3, 2022

#1 2022-10-01 12:22:32 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2589

ISOT - problem with 3 hours difference in results of 2 handlers

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:

Applescript:


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"

Last edited by KniazidisR (2022-10-01 12:29:42 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 15.6.1
Ram: 4 GB

Offline

 

#2 2022-10-01 12:59:58 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5588

Re: ISOT - problem with 3 hours difference in results of 2 handlers

Hi KniazidisR.

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.


NG

Offline

 

#3 2022-10-01 02:34:27 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2589

Re: ISOT - problem with 3 hours difference in results of 2 handlers

Nigel Garvey wrote:

Hi KniazidisR.

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.


Thanks, this explanation is what I need.

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:

Applescript:


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:

Applescript:


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!!!

Last edited by KniazidisR (2022-10-01 03:03:31 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 15.6.1
Ram: 4 GB

Offline

 

#4 2022-10-01 03:02:51 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5588

Re: ISOT - problem with 3 hours difference in results of 2 handlers

KniazidisR wrote:

in order for it to become a full-fledged equivalent of the AsObjC


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.


NG

Offline

 

#5 2022-10-01 03:07:13 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2589

Re: ISOT - problem with 3 hours difference in results of 2 handlers

I edited my post #3.

Last edited by KniazidisR (2022-10-01 03:08:07 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 15.6.1
Ram: 4 GB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)