iso week number in Eggplant

is there a quick way to get the iso week number in Eggplant ?

There seems to always be something new that people want to do with dates! :slight_smile:

No, the ISO Week number isn’t something that’s built into SenseTalk (yet!), but in searching to see what this was, I found an algorithm to calculate it posted by Rick McCarty. I implemented his approach in this weekDate function:

to handle weekDate of aDate
	if aDate is empty then set aDate to the date else set aDate to date(aDate)
	set y to year(aDate)
	set m to month(aDate)
	set d to day(aDate)
	set leapYear to (y mod 4 = 0 and y mod 100 <> 0) or y mod 400 = 0
	set leapPrev to ((y-1) mod 4 = 0 and (y-1) mod 100 <> 0) or (y-1) mod 400 = 0
	set DayOfYearNumber to dayOfYear(aDate)
	set Jan1Weekday to dayOfWeek(y&"-01-01")
	if Jan1Weekday is zero then set Jan1Weekday to 7
	set Weekday to dayOfWeek(aDate)
	if Weekday is zero then set Weekday to 7
	if DayOfYearNumber <= (8 - Jan1Weekday) and Jan1Weekday > 4 then
		set YearNumber to y - 1
		if Jan1Weekday = 5 or (Jan1Weekday = 6 and leapPrev) then
			set WeekNumber to 53
			set WeekNumber to 52
		end if
		set YearNumber to y
		set i to (if leapYear then 366 else 365)
		if (i - DayOfYearNumber) < (4 - Weekday) then
			add 1 to YearNumber
			set WeekNumber to 1
		end if
	end if
	if YearNumber is Y then
		set J to DayOfYearNumber + (7 - Weekday) + (Jan1Weekday - 1)
		set WeekNumber to J div 7
		if Jan1Weekday > 4 then subtract 1 from WeekNumber
	end if
	return (YearNumber, WeekNumber, Weekday)
end weekDate

This function returns a list containing the year number, the week number, and the day of the week for any date that is passed to the function. If you don’t pass a date it assumes the current date.

I’ve also uploaded a script file containing a slightly modified version of this handler and a couple of additional handlers to return just the week number and to return the date in a standard ISO 8601 Week Day format. The script includes some test code that produces this output:

2008-09-16 is 2008-W38-2
2005-01-01 is 2004-W53-6
2005-01-02 is 2004-W53-7
2005-01-03 is 2005-W01-1
2005-12-31 is 2005-W52-6
2007-01-01 is 2007-W01-1
2007-12-30 is 2007-W52-7
2007-12-31 is 2008-W01-1
2008-01-01 is 2008-W01-2
2008-12-28 is 2008-W52-7
2008-12-29 is 2009-W01-1
2008-12-31 is 2009-W01-3
2009-01-01 is 2009-W01-4
2009-12-31 is 2009-W53-4
2010-01-01 is 2009-W53-5
2010-01-02 is 2009-W53-6
2010-01-03 is 2009-W53-7
2010-01-04 is 2010-W01-1

I don’t guarantee that this will produce the correct results in all cases, but it seems to be working as expected. Good luck!

Thank you Doug,
I just had to modifiy something in order to avoid an error message.
I replaced all the “to hanble handleName of aParameter” with “to handle handleName(aParameter)” and it works perfectly (up to now).

Thanks again.

Oops! Allowing the “of” syntax in “to handle” may be a recent addition that hasn’t made it into Eggplant yet… I should have been more careful about which version I tested that in. I’m glad you figured out how to make it work!