It strikes me is that, since you have to go some way to getting the date in your other thread in order to validate it, the validation and interpretation could be usefully combined. This returns an AppleScript date if the input’s valid, or ‘false’ if it’s not:
property theYesterdayIndicators : {"yes", "ges", "hie", "aye"} -- for yesterday, gestern, hier, ayer
property theTodayIndicators : {"tod", "heu", "auj", "hoy"} -- for today, heute, aujourd'hui, hoy
property theTomorrowIndicators : {"tom", "mor", "dem", "mañ", "man"} -- for tomorrow, morgen, demain, mañana, manana
property theDayIndicators : {"d", "t", "j"} -- for day/d'a, Tag, jour
property theWeekIndicators : {"w", "s"} -- for week/Woche, semain/semana
-- Construct a regex to parse non-date input in the languages provided for in the script properties.
on getPlugin()
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set validateDayWeekIndicator to (theDayIndicators as text) & theWeekIndicators
set AppleScript's text item delimiters to "|"
set validateYesTodTomIndicator to {theYesterdayIndicators, theTodayIndicators, theTomorrowIndicators} as text
set AppleScript's text item delimiters to astid
return "[" & validateDayWeekIndicator & "]|(" & validateYesTodTomIndicator & ")[[:alpha:]'\\'']*"
end getPlugin
-- Is the passed date input valid in this script's terms? If so, return the date as an AppleScript date object. If not, return the boolean 'false'.
on validateDateInput(dateInput)
-- A string of the short-date separators to be recognised. (Edit to taste.)
-- These will be used in a regex class, so the hyphen must be first or last.
set allowedSeparators to "-/."
-- A list of short-date part regexes, in day, month, year order.
set datePartRegices to {"(0?[1-9]|[12][0-9]|3[01])", "(0?[1-9]|1[0-2])", "([1-9][0-9])?[0-9]{2}"}
-- Get the local short-date string for 1st February 4003, strip out everything except the "1", the "2", and the "3", and turn these into a 3-digit integer. Use the digits to index the short-date part regexes and to arrange them into the equivalent order in a full short-date regex.
set order to (do shell script ("<<<" & quoted form of short date string of («data isot343030332D30322D3031» as date) & " sed -E 's/[^123]//g'")) as integer
set separatorClass to "[" & allowedSeparators & "]"
tell datePartRegices to set shortDateRegex to item (order div 100) & separatorClass & item (order mod 100 div 10) & separatorClass & item (order mod 10)
set today to (current date)
set today's time to 0
if ((do shell script ("<<<" & quoted form of dateInput & " sed -E '/^" & shortDateRegex & "$/ !s/.*/false/; /false/ !s/.+/true/ ;'")) as boolean) then
-- If dateInput is a valid date string in any of the allowed formats, test to see if it represents a valid Gregorian or Proleptic Gregorian date.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to characters of allowedSeparators -- Requires Snow Leopard or later.
tell dateInput's text items
set {item (order div 100), item (order mod 100 div 10), item (order mod 10)} to {beginning as integer, item 2 as integer, end as integer}
set {d, m, y} to it
-- If the "year" has only two digits, count it as being in the current century.
if (y < 100) then set y to (today's year) div 100 * 100 + y
end tell
set AppleScript's text item delimiters to astid
-- If the date's valid, revalue the 'today' date object to it and set 'ValidDueDate' to that.
set ValidDueDate to ((d < 29) or (m is in {1, 3, 5, 7, 8, 10, 12}) or ((d < 31) and (m > 2)) or ((d is 29) and (y mod 4 is 0) and (y mod 400 is not in {100, 200, 300})))
if (ValidDueDate) then tell today to set {day, year, its month, day, ValidDueDate} to {1, y, m, d, it}
else
-- Otherwise test for any of the alternative valid inputs.
set ValidDueDate to (do shell script ("<<<" & quoted form of dateInput & " sed -E 'y/ABCDEFGHIJKLMNÑOPQRSTUVWXYZ/abcdefghijklmnñopqrstuvwxyz/ ; /^([-+]?[1-9][0-9]*" & getPlugin() & ")$/ !s/.*/false/; /false/ !s/.+/true/ ;'")) as boolean
if (ValidDueDate) then
-- If this is a valid alternative, test to see if if contains any digit characters.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to characters of "123456789" -- Requires Snow Leopard or later.
set inputContainsDigit to ((count dateInput each text item) > 1)
set AppleScript's text item delimiters to astid
if (inputContainsDigit) then
-- If it contains digits, add the relevant number of days or weeks to 'today' and set 'ValidDueDate' to the result.
set ValidDueDate to today + (text 1 thru -2 of dateInput) * (((last character of dateInput is in theWeekIndicators) as integer) * 6 + 1) * days
else
-- Otherwise it's a word of which only the first three letters have to be present or correct. Test the first three letters against the indicators in this script's properties and add or subtract the appropriate number of days.
tell text 1 thru 3 of dateInput to set ValidDueDate to today + (((it is in theTomorrowIndicators) as integer) - ((it is in theYesterdayIndicators) as integer)) * days
end if
end if
end if
return ValidDueDate -- Either an AppleScript date object or the boolean 'false'.
end validateDateInput
validateDateInput("mañana")