Weekday as integer

Sunday is #1, and it seems to be hard-coded - at least, I could not find a way to change it.

Is this correct?

As far as I know that is correct in both Leopard and Snow Leopard.

It’s quite easy to write a handler which gets the weekday number wirh some other week start:

on weekdayNum(w, wkstrt)
	return (w - wkstrt + 7) mod 7 + 1
end weekdayNum

set wkstrt to Monday

weekdayNum(Sunday, wkstrt) --> 7

Or you could hard-wire it for, say, Monday week starts:

on isoWeekdayNum(w)
	return (w + 5) mod 7 + 1
end isoWeekdayNum

isoWeekdayNum(Sunday)

As it happens I wanted the 1st saturday of next year (or any year, for that matter).

-- Find 1st saturday of next year (or pass in any year)
-- change the property to get any weekday
-- sunday is #1, and this seems to be hard-coded
-- so this *should* be safe in any locale
property dayNumber : 7 -- saturday

set theDate to (current date)
tell theDate to set {it's year, it's month, it's day} to {(it's year) + 1, 1, 1} -- jan 1 of 2011

set wkday to theDate's weekday as integer
tell theDate to set it's day to (it's day) + dayNumber - wkday
theDate -->date "zaterdag 1 januari 2011 14:00" - a coincidence

Will be part of a script to produce a year’s worth of iCal events; I don’t want to use recurring events, they are a pain in the … to move to a new Mac.

Hi.

The result of your day calculation is sometimes 0, which will cause an error when you try to set theDate’s day to it. You need to ensure that the result isn’t negative and that 0 is changed to 7.

tell theDate to set its day to ((its day) + dayNumber - wkday + 6) mod 7 + 1

In fact, you already know from when you created theDate that its day is 1, so you could optimise the line to:

tell theDate to set its day to (dayNumber - wkday + 7) mod 7 + 1

It appears that weekday-to-integer coercions are automatic if you use the weekdays in arithmetic, so it’s feasible to use the weekday rather than the integer as the property value.

-- Find 1st saturday of next year (or pass in any year)
-- change the property to get any weekday
property wkday : Saturday

tell (current date)
	set {theDate, its year, its month, its day} to {it, (its year) + 1, 1, 1} -- jan 1 of 2011
	set its day to (wkday - (its weekday) + 7) mod 7 + 1
end tell
theDate -->date "zaterdag 1 januari 2011 14:00" - a coincidence

Before the introduction of weekday-to-integer coercions (in Tiger, I think), you had to do something like this:

-- Find 1st saturday of next year (or pass in any year)
-- change the property to get any weekday
property wkday : Saturday

-- Get a reference date in the past with the same weekday as the one required.
set refDate to (date "Saturday 1 January 1600 00:00:00") + (offset of (text 1 thru 2 of (wkday as string)) in "SaSuMoTuWeThFr") div 2 * days

-- Get the last day which could possibly be the first wkday of the required year.
tell (current date) to set {theDate, its year, its month, its day} to {it, (its year) + 1, 1, 7} -- jan 7 of 2011
-- Round down to an exact number of weeks after the reference date.
set theDate to theDate - (theDate - refDate) mod weeks
theDate -->date "zaterdag 1 januari 2011 14:00" - a coincidence

Thank you…:confused:
Think I’m in for some more one-step-at-a-time scripting (I have this habit of wanting to understand every line I use).

It was actually quite a bit worse:

-- some properties of a date
set itsDay to 1 -- the date
set wkday to 7 -- saturday

repeat with dayNumber from 1 to 7 -- desired weekday
	log itsDay + dayNumber - wkday
end repeat
-- result:
(*-5*)
(*-4*)
(*-3*)
(*-2*)
(*-1*)
(*0*)
(*1*)

So it only works once a week. I happened to write it on that one day…
You were just being kind, weren’t you? :smiley: