Date portability in source code

This is more for interest than anything.

Sometimes it’s necessary to use a fixed date in a script, perhaps as a reference date for a calculation. I always compile such dates directly into the script, writing their source code in my preferred date format. Anyone else in the world who opens the script in a script editor will see the compiled dates in their preferred format ” provided they’re using their own machine, of course.

But if you need to post the script on a forum, or otherwise circulate it in text form, it’s very difficult to use your own date format in the source code without upsetting French scripters. :wink: You’re constrained to use a run-time circumlocution like calling ‘current date’ and changing every single detail of the result, remembering to forestall possible month overflow with certain combinations of dates.

Say you need a known leap day in the past for a reference date. You have to get the current date and immediately set its day to something less than 29 in case today’s day doesn’t occur in the month of one of the dates to which the date object’s set while you’re changing the individual details. Then you set the details:

tell (current date) to set {day, year, its month, day, time, refDate} to {1, 1600, February, 29, 0, it}

Before all AppleScript text became UTF-16 Unicode in Leopard, people sometimes used a hack whereby a date string in the form YYYY-MM-DDThh:mm:ss (where “-”, “T”, and “:” were the actual delimiters) could be coerced to an AppleScript date object and back again via another class known as «class isot»:

set refDate to "1600-02-29T00:00:00" as «class isot» as date
-- Or alternatively in this case: 
set refDate to "1600-02-29" as «class isot» as date
--> date "Tuesday 29 February 1600 00:00:00" (or however this would look on your machine)

refDate as «class isot» as text
--> "1600-02-29T00:00:00"

The data in a «class isot» object are simply the 8-bit characters of the YYYY-MM-DDThh:mm:ss string, but rebranded «class isot» instead of «class TEXT». This may be why coercion was possible between string and «class isot» but not between Unicode text and «class isot».

This method of making dates portable was briefly popular on MacScripter a few years ago, but I stopped using it because it was a hack. Its use ceased altogether when it stopped working with the introduction of AppleScript 2.0 in Leopard.

Actually, on my Leopard and Snow Leopard systems, it does still work in the date → «class isot» → string direction provided I specify ‘string’ and not ‘text’ or ‘Unicode text’:

(current date) as «class isot» as string

More usefully from the date-portability aspect, the string → «class isot» → date direction can be made to work with the help of ‘do shell script’ and its ‘as’ parameter:

set refDate to (do shell script "echo '1600-02-29'" as «class isot») as date

I think this must work because “echo” returns UTF-8 Unicode text, which is identical to ‘string’ with the characters involved.

A hack way to perform a hack! :lol:

Very cool, Nigel.

I missed «class isot» for a long time

Thanks Nigel, I wasn’t aware of the undocumented iso time format. Too bad that it needs a T, but now SQL datetime to AS date conversions is a lot easier now.