coerce vcal date format into AS format

I am trying to coerce a vcal (.ics) file date to AS format and I am stuck before I start.

The relevant .ics code reads
DTSTART:20101015T070000Z
DTEND:20101015T080000Z

Which produces an event in iCal with these relevant properties
start date:date “Wednesday, October 15, 2010 08:00:00”, end date:date “Wednesday, October 15, 2010 09:00:00”,
Why do they appear to be 1 hr later?
What use is the T and Z

Here’s one solution. There’s probably a shell command that’s easier to use but I don’t know it. Note: the “Z” means that your date is from the time zone Greenwich Mean Time (GMT).

-- remove DTSTART : from the string
set dtStart to "DTSTART:20101015T070000Z"
set text item delimiters to ":"
set dtStart to text item 2 of dtStart
set text item delimiters to ""

-- get a date with using the DTSTART values
set startDate to current date
set year of startDate to (text 1 thru 4 of dtStart) as number
set month of startDate to (text 5 thru 6 of dtStart) as number
set day of startDate to (text 7 thru 8 of dtStart) as number
set hours of startDate to (text 10 thru 11 of dtStart) as number
set minutes of startDate to (text 12 thru 13 of dtStart) as number
set seconds of startDate to (text 14 thru 15 of dtStart) as number

-- convert to local time
set timeDiff to time to GMT
set localStartDate to startDate + timeDiff

Thanks You Regulus.

As a massive bodger of AS I do truly appreciate the help I get through this forum

Don’t forget that when changing an unforeknown date (such as the date on which the script will be run) to another date, you should set the day below 29 first to avoid any possible overflow effects.

Also, ‘time to GMT’ only gives a result for the date on the script’s run, which may not be the date to which you want to add the result. I’ve attempted date-specific time transposition in this thread. Using the isotToDate() and GMTtoTZ() handlers there, you’d need this code:

set asGMTDate to isotToDate("20101015T070000Z")
set asLondonDate to GMTtoTZ(asGMTDate, "Europe/London") --> date "Wednesday 15 October 2008 08:00:00"

Edit: url supplied for the above link!

Hi Nigel, good point. I guess if the current day was 30 and you set the month to Feb then there could be a problem.

Note you didn’t give us a link. Also, I looked through some “date” threads before posting my solution. Most of them use class isot. The scripts with that in them didn’t run on my system running the latest version of 10.6. Not sure why but the script couldn’t resolve isot. Since I couldn’t see your link I can’t tell if your solutions use that class or not. If so then they no longer seem to be working.

Duh! Sorry. I went to a birthday party 230 miles away last night and was up till 04:00 this morning. I’d just driven back when I checked this forum. Link now rectified.

PS.

«class isot» is one of those things you’re not supposed to be able to use in AppleScript. It’s still possible in Leopard and Snow Leopard to coerce between «class isot» and date, but not between «class isot» and string. That’s because the old, 8-bit ‘string’ class no longer exists and the unofficial text coercion has never worked with Unicode text.

«class isot» data are in fact exactly the same as the equivalent string, so you could use the File Read/Write commands to do the string coercion:

set isotText to "20101015T070000Z"

set fRef to (open for access file ((path to temporary items from local domain as text) & "isot.dat") 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
end try
close access fRef

theDate --> date "Friday 15 October 2010 07:00:00"

But parsing the ISOT text as you did is probably a better idea. :slight_smile: