Strange date behaviour after loop.

Hello again macscripter! I’m still working on my AI system, but I’ve run into an issue with my date handling system.

My problem is that during the loop, the date I’m using is correct, but once returned by the function, all the dates become the same. I’ve tested this by having textedit output the date and time each time a date is added.

TextEdit output:

Script output:

With a little poking I’ve deduced that it dosen’t save the variable to the output list as a new item, but a link to the date item that changes when the date is changed in the loop. I found this by monitoring the value of the date of the first item of the list after each iteration and plugging it to the text document:

Here is the tricky code, it has many dependancies, but near the end of the 2 nested repeat loops is the important part.:

on calDated(dateOption)
	set CalDB to load script pathfind("Calendar Database")
	tell CalDB to set database to getContent()
	if dateOption is "" then
		set dateOption to current date
	end if
	if database is {} then
		return {}
	end if
	set outInfo to {}
	tell application "TextEdit" to set the text of the front document to ""
	repeat with calItem in database
		repeat 1 times
			if item 1 of calItem is "RP" then
				set daynum to getDayNum(word 1 of item 2 of calItem)
				if daynum is not weekday of (dateOption) as number then
					if daynum is not weekday of ((dateOption) + 1 * days) as number then
						exit repeat
					end if
					set mydate to (dateOption) + 1 * days
				else
					set mydate to (dateOption)
				end if
				set seconds of mydate to 0
				set hours of mydate to (word 2 of item 2 of calItem) as number
				set minutes of mydate to (word 3 of item 2 of calItem) as number
			else if item 1 of calItem is "FD" then
				set mydate to (dateOption)
				set seconds of mydate to 0
				set hours of mydate to 0
				set minutes of mydate to 0
				set day of mydate to (word 1 of item 2 of calItem) as number
				set year of mydate to (word 3 of item 2 of calItem) as number
				if date string of mydate is not date string of dateOption then
					if date string of mydate is not (date string of dateOption) + 1 * days then
						exit repeat
					end if
				end if
			else
				set mydate to (dateOption)
				set seconds of mydate to 0
				set hours of mydate to (word 4 of item 2 of calItem) as number
				set minutes of mydate to (word 5 of item 2 of calItem) as number
				set day of mydate to (word 1 of item 2 of calItem) as number
				set year of mydate to (word 3 of item 2 of calItem) as number
			end if


---------------------------------------------------------IMPORTANT BIT------------------------------------------------------------------
			set outInfo to outInfo & {{(item 1 of calItem as text), (mydate) as date, (item 3 of calItem as text)} as list}
---------------------------------------------------------IMPORTANT BIT------------------------------------------------------------------


			tell application "TextEdit" to set the text of the front document to ((the text of the front document & return & return & item 1 of calItem as text) & return & time string of mydate & " " & date string of mydate & return & item 3 of calItem as text) & return & time string of item 2 of item 1 of outInfo & " " & date string of item 2 of item 1 of outInfo
		end repeat
	end repeat
	return outInfo
end calDated

Thans for any and all help in advance, I found this bug by missing lectures…

Hi.

A date is a mutable object, like a list or record. If you have a variable set to a date and you set another variable to that variable, they’re both set to the same date object. Change the details of the date and the changes are visible whichever variable you use.

Instead of ‘set mydate to dateoption’, use ‘copy dateoption to mydate’. This sets mydate to a duplicate of the original date. You can then adjust either without affecting the other.

Thank you very much, that worked a charm! The replacement lines of code look like this:

set outInfo to outInfo & {{(item 1 of calItem as text), (current date), (item 3 of calItem as text)} as list}
copy mydate to item 2 of the last item of outInfo

Output and text samples: