Three Date Manipulation Functions

This scriptlet is three date manipulation functions.

The first returns a weekday date variable from a string representing either the full or abbreviated version of the weekday.

The second function uses the first function to return a date that is the current time on this coming what-ever-weekday-you-specified (for instance if I ran it at 6 pm on Thurs. the 22nd and specified “tues”, it would return 6 pm on Tues. the 27th.

The third returns the number of seconds in a given time of day. It requires that the first word be a number, but everything else is optional. For instance, make_time_var from “9” would return 32400 (9 am in seconds since midnight). “9:00 pm” would return 75600, as would “9 pm”.

These functions can be used in sequence to set a date variable to, say 9 am this comming Tuesday, like so…

set apptStart to this_coming_weekday(“tues”)
set time of apptStart to make_time_var from “9”

OS version: Any

on get_weekday_var from weekdayStr
	if weekdayStr is "Monday" or weekdayStr is "Mon." or weekdayStr is "mon" then
		set weekdayVar to Monday
	else if weekdayStr is "Tuesday" or weekdayStr is "tues." or weekdayStr is "tues" then
		set weekdayVar to Tuesday
	else if weekdayStr is "Wedneday" or weekdayStr is "wed" or weekdayStr is "wed." then
		set weekdayVar to Wedneday
	else if weekdayStr is "Thursday" or weekdayStr is "Thurs." or weekdayStr is "thurs" then
		set weekdayVar to Thursday
	else if weekdayStr is "Friday" or weekdayStr is "fri." or weekdayStr is "fri" then
		set weekdayVar to Friday
	else if weekdayStr is "Saturday" or weekdayStr is "sat" or weekdayStr is "sat." then
		set weekdayVar to Saturday
	else if weekdayStr is "Sunday" or weekdayStr is "sun" or weekdayStr is "sun." then
		set weekdayVar to Sunday
	else
		set weekdayVar to weekdayStr
	end if
	return weekdayVar
end get_weekday_var

on this_coming_weekday(weekdayVar)
	set currentDate to current date
	if class of weekdayVar is string or class of weekdayVar is Unicode text then
		tell me to set weekdayVar to get_weekday_var from weekdayVar
		set keepNewDate to false
		repeat with i from 1 to 7
			if weekday of currentDate is weekdayVar then
				set keepNewDate to true
				exit repeat
			end if
			set day of currentDate to ((day of currentDate) + 1)
		end repeat
		if keepNewDate then
			return currentDate
		else
			return ""
		end if
	else if class of weekdayVar is class then
		return currentDate
	end if
end this_coming_weekday

on make_time_var from timeStr
	set timeHrs to word 1 of timeStr as number
	set makepm to 0
	set timeMin to 0
	set timeSec to 0
	if ((count of words of timeStr) is greater than 1) then
		repeat with i from 2 to (count of words of timeStr)
			if (word i of timeStr is "am" or word i of timeStr is "pm") then
				if word i of timeStr is "pm" then
					set makepm to 43200
				end if
			else if i is 2 then
				set timeMin to word 2 of timeStr as number
			else if i is 3 then
				set timeSec to word 3 of timeStr as number
			end if
		end repeat
	end if
	set timeTotal to timeHrs * 60 * 60 + timeMin * 60 + timeSec + makepm
	return timeTotal
end make_time_var