Open gso opened 12 years ago
It could possibly be done without a separate Year class by altering the Month class to allow an ordinal greater than the number of days in the month, i.e., day 100 in a month period of 12.
A second get_date (yearmonth.py) function could be coded to accept a day value with '-1' -ve value syntax but also to accept a 'day' value greater than the number of days in the month (I'm not sure what you would call the function):
def get_date(self, day):
return date(self.year, self.month, day)
This could be changed to add the ordinal, e.g., along the lines of 'date(self.year, self.month, 1) + (day - 1)', however this would break the '-1' syntax. The function would need to process +ve and -ve 'day' values separately; to process +ve values it would need to find the number of whole months (and duration in days) in the +ve value, using the remainder to set the 'day' arg. To process -ve values the function would need to know the period, first adding this number of months, and then returning the date object with the -ve 'day' value. (This curiously is how Javascript codes date values by default, it being possible to create a 'new Date(2012, 01, 100)' object.)
Presumably week/day in month could be recoded likewise (if needed).
It would add a method to the code but is still a quite elegant solution otherwise (removing the necessity for a new class, adding the syntax to recur on not just a day of a month, but a day or week/day in the period of the recurrence).
If I can suggest a name for a second function: 'get_period_date', which would retrieve the date of an offset within a period, and would facilitate '-1' (-ve ordinal) syntax.
I've provisionally implemented this by replacing:
def _date_for_yearmonth(self, ym):
with the following method:
def _date_for_period(self, ym):
I've also renamed 'DAY_OF_MONTH' to DAY_OF_PERIOD.
The code has been duly forked and can be found here https://github.com/gso/Recurrence
Could do with a year class capable of recurring i) day num. of year and ii) week/weekday of year (e.g. 12th Monday of year)? (These can't be implemented with month or day classes I don't think.)
Otherwise this is very elegant code! (Which is both a compliment to the coder and Python.)