«class isot» as date Problem

Hello,

This works on Tiger, I have recently upgraded to Leopard and now it doesn’t work. Is this because Leopard is using Unicode Text for everything and also how can I make it work.

"2008-08-09T22:22:15" as «class isot» as date

Any suggestions would be great
Thank you Dallas

Exactly.

there is no workaround to handle «class isot» directly with a literal string

Alright thanks Stefan. That is very weird that it there is no problem returning the current date as class isot but you can’t convert it back to a date. Very windows like.

Hi Stefan

Can you think of a better way to do it that this? If so let me know.

set d to "2008-09-08T13:40:44"

set thedate to date (((word 2 of (text items 1 through 10 of d as string)) & "/" & (word 3 of (text items 1 through 10 of d as string)) & "/" & (word 1 of (text items 1 through 10 of d as string)) as string) & " " & (text items 12 through end of d))

Dallas

I don’t know whether it’s better, but it works regardless of the international date format settings


set dateString to "2008-08-09T22:22:15"
set {TID, text item delimiters} to {text item delimiters, "T"}
set {_date, _time} to text items of dateString
set theDate to date _time
set text item delimiters to "-"
set {yr, mn, dy} to text items of _date
set text item delimiters to TID
tell theDate to set {its year, its month, its day} to {(yr as integer), (mn as integer), dy as integer}
theDate

That’s good to know, thanks
Dallas

Hi, Stefan.

That’ll give the wrong result if it’s run on a day that doesn’t exist in the target month. theDate’s day should additionally be set below 29 before the month’s set. Alternatively, you can build from a known safe date:

set dateString to "2008-09-09T22:22:15"

tell dateString to set {y, m, d, t} to {text 1 thru 4, text 6 thru 7, text 9 thru 10, (text 12 thru 13) * hours + (text 15 thru 16) * minutes + text 18 thru 19}
tell date "Wednesday 1 January 1000 00:00:00" to set {year, its month, day, time, theDate} to {y, m, d, t, it}
theDate

Thanks Nigel,

of course I assumed only valid dates in the ISOT string :wink:

Hi, Stefan.

I’m sorry. I don’t think I explained myself very well last night. :frowning:

The problem’s with the line set theDate to date _time. The date it creates is the same as the date on which the script’s run; so if the script’s run on the 30th of this month, theDate will be 30th September 2008 with the time from dateString.

Now, if the ISOT string represents a date in February, setting theDate’s month to 2 a few lines later will overflow theDate into March, because its day is 30 and there are only 29 days in February this year.

-- Assuming this script is run at some time during 30th September 2008.

-- Valid ISOT string for a date in February.
set dateString to "2008-02-09T22:22:15"
set {TID, text item delimiters} to {text item delimiters, "T"}
set {_date, _time} to text items of dateString
-- This compiles a date from the given time, filling in the rest of the details from today's date.
set theDate to date _time
--> date "Tuesday 30 September 2008 22:22:15"
set text item delimiters to "-"
set {yr, mn, dy} to text items of _date
set text item delimiters to TID
-- This sets theDate's year, month, and day, in that order. Since theDate's day is still 30 when the month is set to 2, it'll overflow into March.
tell theDate to set {its year, its month, its day} to {(yr as integer), (mn as integer), dy as integer}
theDate
--> date "Monday 9 March 2008 22:22:15"

Another problem with set theDate to date _time ” which I’ve only realised this morning ” is that AppleScript only recognises “22:22:15” as a time because: 1) the numbers are too high to be anything else, and: 2) the time separator is set to “:” in my International preferences. If the script’s run on a machine with a different preference (unlikely, but possible) and the numbers can be interpreted as a date on that machine, that’s how they’ll be interpreted:

-- With the user's time separator preference set to "." instead of ":"

set _time to "10:10:15"
set theDate to date _time
--> date "Saturday 10 October 2015 00.00.00"

Got it :slight_smile:

It occurs to me rather belatedly that «class isot» data are exactly the same as the equivalent string data: only the class is different. Since the File Read/Write commands still use ‘as string’ in the old sense (I believe), this should work on any system:

on isotText2Date(isotText)
	set fRef to (open for access file ((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

isotText2Date("2008-08-09T22:22:15")
--> date "Saturday 9 August 2008 22:22:15"

Thank you Nigel, this is a very clever solution :smiley:
I’ve also twisted my mind to recover this feature for Leopard