+++ /dev/null
-# Class Date supplies date objects that support date arithmetic.\r
-#\r
-# Date(month,day,year) returns a Date object. An instance prints as,\r
-# e.g., 'Mon 16 Aug 1993'.\r
-#\r
-# Addition, subtraction, comparison operators, min, max, and sorting\r
-# all work as expected for date objects: int+date or date+int returns\r
-# the date `int' days from `date'; date+date raises an exception;\r
-# date-int returns the date `int' days before `date'; date2-date1 returns\r
-# an integer, the number of days from date1 to date2; int-date raises an\r
-# exception; date1 < date2 is true iff date1 occurs before date2 (&\r
-# similarly for other comparisons); min(date1,date2) is the earlier of\r
-# the two dates and max(date1,date2) the later; and date objects can be\r
-# used as dictionary keys.\r
-#\r
-# Date objects support one visible method, date.weekday(). This returns\r
-# the day of the week the date falls on, as a string.\r
-#\r
-# Date objects also have 4 read-only data attributes:\r
-# .month in 1..12\r
-# .day in 1..31\r
-# .year int or long int\r
-# .ord the ordinal of the date relative to an arbitrary staring point\r
-#\r
-# The Dates module also supplies function today(), which returns the\r
-# current date as a date object.\r
-#\r
-# Those entranced by calendar trivia will be disappointed, as no attempt\r
-# has been made to accommodate the Julian (etc) system. On the other\r
-# hand, at least this package knows that 2000 is a leap year but 2100\r
-# isn't, and works fine for years with a hundred decimal digits <wink>.\r
-\r
-# Tim Peters tim@ksr.com\r
-# not speaking for Kendall Square Research Corp\r
-\r
-# Adapted to Python 1.1 (where some hacks to overcome coercion are unnecessary)\r
-# by Guido van Rossum\r
-\r
-# Note that as of Python 2.3, a datetime module is included in the stardard\r
-# library.\r
-\r
-# vi:set tabsize=8:\r
-\r
-_MONTH_NAMES = [ 'January', 'February', 'March', 'April', 'May',\r
- 'June', 'July', 'August', 'September', 'October',\r
- 'November', 'December' ]\r
-\r
-_DAY_NAMES = [ 'Friday', 'Saturday', 'Sunday', 'Monday',\r
- 'Tuesday', 'Wednesday', 'Thursday' ]\r
-\r
-_DAYS_IN_MONTH = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]\r
-\r
-_DAYS_BEFORE_MONTH = []\r
-dbm = 0\r
-for dim in _DAYS_IN_MONTH:\r
- _DAYS_BEFORE_MONTH.append(dbm)\r
- dbm = dbm + dim\r
-del dbm, dim\r
-\r
-_INT_TYPES = type(1), type(1L)\r
-\r
-def _is_leap(year): # 1 if leap year, else 0\r
- if year % 4 != 0: return 0\r
- if year % 400 == 0: return 1\r
- return year % 100 != 0\r
-\r
-def _days_in_year(year): # number of days in year\r
- return 365 + _is_leap(year)\r
-\r
-def _days_before_year(year): # number of days before year\r
- return year*365L + (year+3)//4 - (year+99)//100 + (year+399)//400\r
-\r
-def _days_in_month(month, year): # number of days in month of year\r
- if month == 2 and _is_leap(year): return 29\r
- return _DAYS_IN_MONTH[month-1]\r
-\r
-def _days_before_month(month, year): # number of days in year before month\r
- return _DAYS_BEFORE_MONTH[month-1] + (month > 2 and _is_leap(year))\r
-\r
-def _date2num(date): # compute ordinal of date.month,day,year\r
- return _days_before_year(date.year) + \\r
- _days_before_month(date.month, date.year) + \\r
- date.day\r
-\r
-_DI400Y = _days_before_year(400) # number of days in 400 years\r
-\r
-def _num2date(n): # return date with ordinal n\r
- if type(n) not in _INT_TYPES:\r
- raise TypeError, 'argument must be integer: %r' % type(n)\r
-\r
- ans = Date(1,1,1) # arguments irrelevant; just getting a Date obj\r
- del ans.ord, ans.month, ans.day, ans.year # un-initialize it\r
- ans.ord = n\r
-\r
- n400 = (n-1)//_DI400Y # # of 400-year blocks preceding\r
- year, n = 400 * n400, n - _DI400Y * n400\r
- more = n // 365\r
- dby = _days_before_year(more)\r
- if dby >= n:\r
- more = more - 1\r
- dby = dby - _days_in_year(more)\r
- year, n = year + more, int(n - dby)\r
-\r
- try: year = int(year) # chop to int, if it fits\r
- except (ValueError, OverflowError): pass\r
-\r
- month = min(n//29 + 1, 12)\r
- dbm = _days_before_month(month, year)\r
- if dbm >= n:\r
- month = month - 1\r
- dbm = dbm - _days_in_month(month, year)\r
-\r
- ans.month, ans.day, ans.year = month, n-dbm, year\r
- return ans\r
-\r
-def _num2day(n): # return weekday name of day with ordinal n\r
- return _DAY_NAMES[ int(n % 7) ]\r
-\r
-\r
-class Date:\r
- def __init__(self, month, day, year):\r
- if not 1 <= month <= 12:\r
- raise ValueError, 'month must be in 1..12: %r' % (month,)\r
- dim = _days_in_month(month, year)\r
- if not 1 <= day <= dim:\r
- raise ValueError, 'day must be in 1..%r: %r' % (dim, day)\r
- self.month, self.day, self.year = month, day, year\r
- self.ord = _date2num(self)\r
-\r
- # don't allow setting existing attributes\r
- def __setattr__(self, name, value):\r
- if self.__dict__.has_key(name):\r
- raise AttributeError, 'read-only attribute ' + name\r
- self.__dict__[name] = value\r
-\r
- def __cmp__(self, other):\r
- return cmp(self.ord, other.ord)\r
-\r
- # define a hash function so dates can be used as dictionary keys\r
- def __hash__(self):\r
- return hash(self.ord)\r
-\r
- # print as, e.g., Mon 16 Aug 1993\r
- def __repr__(self):\r
- return '%.3s %2d %.3s %r' % (\r
- self.weekday(),\r
- self.day,\r
- _MONTH_NAMES[self.month-1],\r
- self.year)\r
-\r
- # Python 1.1 coerces neither int+date nor date+int\r
- def __add__(self, n):\r
- if type(n) not in _INT_TYPES:\r
- raise TypeError, 'can\'t add %r to date' % type(n)\r
- return _num2date(self.ord + n)\r
- __radd__ = __add__ # handle int+date\r
-\r
- # Python 1.1 coerces neither date-int nor date-date\r
- def __sub__(self, other):\r
- if type(other) in _INT_TYPES: # date-int\r
- return _num2date(self.ord - other)\r
- else:\r
- return self.ord - other.ord # date-date\r
-\r
- # complain about int-date\r
- def __rsub__(self, other):\r
- raise TypeError, 'Can\'t subtract date from integer'\r
-\r
- def weekday(self):\r
- return _num2day(self.ord)\r
-\r
-def today():\r
- import time\r
- local = time.localtime(time.time())\r
- return Date(local[1], local[2], local[0])\r
-\r
-class DateTestError(Exception):\r
- pass\r
-\r
-def test(firstyear, lastyear):\r
- a = Date(9,30,1913)\r
- b = Date(9,30,1914)\r
- if repr(a) != 'Tue 30 Sep 1913':\r
- raise DateTestError, '__repr__ failure'\r
- if (not a < b) or a == b or a > b or b != b:\r
- raise DateTestError, '__cmp__ failure'\r
- if a+365 != b or 365+a != b:\r
- raise DateTestError, '__add__ failure'\r
- if b-a != 365 or b-365 != a:\r
- raise DateTestError, '__sub__ failure'\r
- try:\r
- x = 1 - a\r
- raise DateTestError, 'int-date should have failed'\r
- except TypeError:\r
- pass\r
- try:\r
- x = a + b\r
- raise DateTestError, 'date+date should have failed'\r
- except TypeError:\r
- pass\r
- if a.weekday() != 'Tuesday':\r
- raise DateTestError, 'weekday() failure'\r
- if max(a,b) is not b or min(a,b) is not a:\r
- raise DateTestError, 'min/max failure'\r
- d = {a-1:b, b:a+1}\r
- if d[b-366] != b or d[a+(b-a)] != Date(10,1,1913):\r
- raise DateTestError, 'dictionary failure'\r
-\r
- # verify date<->number conversions for first and last days for\r
- # all years in firstyear .. lastyear\r
-\r
- lord = _days_before_year(firstyear)\r
- y = firstyear\r
- while y <= lastyear:\r
- ford = lord + 1\r
- lord = ford + _days_in_year(y) - 1\r
- fd, ld = Date(1,1,y), Date(12,31,y)\r
- if (fd.ord,ld.ord) != (ford,lord):\r
- raise DateTestError, ('date->num failed', y)\r
- fd, ld = _num2date(ford), _num2date(lord)\r
- if (1,1,y,12,31,y) != \\r
- (fd.month,fd.day,fd.year,ld.month,ld.day,ld.year):\r
- raise DateTestError, ('num->date failed', y)\r
- y = y + 1\r
-\r
-if __name__ == '__main__':\r
- test(1850, 2150)\r