Basic tokens can be implemented with
strptime(), but (in Python at least) unparsed suffix data cause an exception which means we need to know in advance how many tokens to consume. This is the reason why we need both
<date> and a
<year> tokens, for example.
Phrases such as “next Monday” can be ambiguous, but they're very common so a reasonable interpretation is probably still useful. The table below shows the offset in days from the current day for each of the three phrases shown:
|Day||“last Wednesday”||“this Wednesday”||“next Wednesday”|
Note the inconsistency where “this” becomes synonymous with “next” for the current day — this reflects the fact that I don't believe someone would ever say “this Wednesday” if they meant “today”.
The algorithms for each version are thus (assuming
<current weekday> have been converted to zero-based integers where Monday is zero):
<new date> = <old date> - <current weekday> - 7 + <weekday>
if <weekday> == <current weekday>:
<new date> = <old date> + 7
<new date> = <old date> - <current weekday> + <weekday>
<new date> = <old date> - <current weekday> + 7 + <weekday>
A bare day of the week (e.g. just “Tuesday”) is always taken as the day of the current week (Monday-based), as opposed to some date parsing packages which regard this as synonymous with “next Tuesday”. This means it doesn't quite correspond exactly with any of the last/this/next interpretations, but that's probably OK.