/Demo/classes/Dates.py

http://unladen-swallow.googlecode.com/ · Python · 227 lines · 142 code · 39 blank · 46 comment · 39 complexity · 3ecc0f3d855417786d6454a81af40219 MD5 · raw file

  1. # Class Date supplies date objects that support date arithmetic.
  2. #
  3. # Date(month,day,year) returns a Date object. An instance prints as,
  4. # e.g., 'Mon 16 Aug 1993'.
  5. #
  6. # Addition, subtraction, comparison operators, min, max, and sorting
  7. # all work as expected for date objects: int+date or date+int returns
  8. # the date `int' days from `date'; date+date raises an exception;
  9. # date-int returns the date `int' days before `date'; date2-date1 returns
  10. # an integer, the number of days from date1 to date2; int-date raises an
  11. # exception; date1 < date2 is true iff date1 occurs before date2 (&
  12. # similarly for other comparisons); min(date1,date2) is the earlier of
  13. # the two dates and max(date1,date2) the later; and date objects can be
  14. # used as dictionary keys.
  15. #
  16. # Date objects support one visible method, date.weekday(). This returns
  17. # the day of the week the date falls on, as a string.
  18. #
  19. # Date objects also have 4 read-only data attributes:
  20. # .month in 1..12
  21. # .day in 1..31
  22. # .year int or long int
  23. # .ord the ordinal of the date relative to an arbitrary staring point
  24. #
  25. # The Dates module also supplies function today(), which returns the
  26. # current date as a date object.
  27. #
  28. # Those entranced by calendar trivia will be disappointed, as no attempt
  29. # has been made to accommodate the Julian (etc) system. On the other
  30. # hand, at least this package knows that 2000 is a leap year but 2100
  31. # isn't, and works fine for years with a hundred decimal digits <wink>.
  32. # Tim Peters tim@ksr.com
  33. # not speaking for Kendall Square Research Corp
  34. # Adapted to Python 1.1 (where some hacks to overcome coercion are unnecessary)
  35. # by Guido van Rossum
  36. # Note that as of Python 2.3, a datetime module is included in the stardard
  37. # library.
  38. # vi:set tabsize=8:
  39. _MONTH_NAMES = [ 'January', 'February', 'March', 'April', 'May',
  40. 'June', 'July', 'August', 'September', 'October',
  41. 'November', 'December' ]
  42. _DAY_NAMES = [ 'Friday', 'Saturday', 'Sunday', 'Monday',
  43. 'Tuesday', 'Wednesday', 'Thursday' ]
  44. _DAYS_IN_MONTH = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
  45. _DAYS_BEFORE_MONTH = []
  46. dbm = 0
  47. for dim in _DAYS_IN_MONTH:
  48. _DAYS_BEFORE_MONTH.append(dbm)
  49. dbm = dbm + dim
  50. del dbm, dim
  51. _INT_TYPES = type(1), type(1L)
  52. def _is_leap(year): # 1 if leap year, else 0
  53. if year % 4 != 0: return 0
  54. if year % 400 == 0: return 1
  55. return year % 100 != 0
  56. def _days_in_year(year): # number of days in year
  57. return 365 + _is_leap(year)
  58. def _days_before_year(year): # number of days before year
  59. return year*365L + (year+3)//4 - (year+99)//100 + (year+399)//400
  60. def _days_in_month(month, year): # number of days in month of year
  61. if month == 2 and _is_leap(year): return 29
  62. return _DAYS_IN_MONTH[month-1]
  63. def _days_before_month(month, year): # number of days in year before month
  64. return _DAYS_BEFORE_MONTH[month-1] + (month > 2 and _is_leap(year))
  65. def _date2num(date): # compute ordinal of date.month,day,year
  66. return _days_before_year(date.year) + \
  67. _days_before_month(date.month, date.year) + \
  68. date.day
  69. _DI400Y = _days_before_year(400) # number of days in 400 years
  70. def _num2date(n): # return date with ordinal n
  71. if type(n) not in _INT_TYPES:
  72. raise TypeError, 'argument must be integer: %r' % type(n)
  73. ans = Date(1,1,1) # arguments irrelevant; just getting a Date obj
  74. del ans.ord, ans.month, ans.day, ans.year # un-initialize it
  75. ans.ord = n
  76. n400 = (n-1)//_DI400Y # # of 400-year blocks preceding
  77. year, n = 400 * n400, n - _DI400Y * n400
  78. more = n // 365
  79. dby = _days_before_year(more)
  80. if dby >= n:
  81. more = more - 1
  82. dby = dby - _days_in_year(more)
  83. year, n = year + more, int(n - dby)
  84. try: year = int(year) # chop to int, if it fits
  85. except (ValueError, OverflowError): pass
  86. month = min(n//29 + 1, 12)
  87. dbm = _days_before_month(month, year)
  88. if dbm >= n:
  89. month = month - 1
  90. dbm = dbm - _days_in_month(month, year)
  91. ans.month, ans.day, ans.year = month, n-dbm, year
  92. return ans
  93. def _num2day(n): # return weekday name of day with ordinal n
  94. return _DAY_NAMES[ int(n % 7) ]
  95. class Date:
  96. def __init__(self, month, day, year):
  97. if not 1 <= month <= 12:
  98. raise ValueError, 'month must be in 1..12: %r' % (month,)
  99. dim = _days_in_month(month, year)
  100. if not 1 <= day <= dim:
  101. raise ValueError, 'day must be in 1..%r: %r' % (dim, day)
  102. self.month, self.day, self.year = month, day, year
  103. self.ord = _date2num(self)
  104. # don't allow setting existing attributes
  105. def __setattr__(self, name, value):
  106. if self.__dict__.has_key(name):
  107. raise AttributeError, 'read-only attribute ' + name
  108. self.__dict__[name] = value
  109. def __cmp__(self, other):
  110. return cmp(self.ord, other.ord)
  111. # define a hash function so dates can be used as dictionary keys
  112. def __hash__(self):
  113. return hash(self.ord)
  114. # print as, e.g., Mon 16 Aug 1993
  115. def __repr__(self):
  116. return '%.3s %2d %.3s %r' % (
  117. self.weekday(),
  118. self.day,
  119. _MONTH_NAMES[self.month-1],
  120. self.year)
  121. # Python 1.1 coerces neither int+date nor date+int
  122. def __add__(self, n):
  123. if type(n) not in _INT_TYPES:
  124. raise TypeError, 'can\'t add %r to date' % type(n)
  125. return _num2date(self.ord + n)
  126. __radd__ = __add__ # handle int+date
  127. # Python 1.1 coerces neither date-int nor date-date
  128. def __sub__(self, other):
  129. if type(other) in _INT_TYPES: # date-int
  130. return _num2date(self.ord - other)
  131. else:
  132. return self.ord - other.ord # date-date
  133. # complain about int-date
  134. def __rsub__(self, other):
  135. raise TypeError, 'Can\'t subtract date from integer'
  136. def weekday(self):
  137. return _num2day(self.ord)
  138. def today():
  139. import time
  140. local = time.localtime(time.time())
  141. return Date(local[1], local[2], local[0])
  142. class DateTestError(Exception):
  143. pass
  144. def test(firstyear, lastyear):
  145. a = Date(9,30,1913)
  146. b = Date(9,30,1914)
  147. if repr(a) != 'Tue 30 Sep 1913':
  148. raise DateTestError, '__repr__ failure'
  149. if (not a < b) or a == b or a > b or b != b:
  150. raise DateTestError, '__cmp__ failure'
  151. if a+365 != b or 365+a != b:
  152. raise DateTestError, '__add__ failure'
  153. if b-a != 365 or b-365 != a:
  154. raise DateTestError, '__sub__ failure'
  155. try:
  156. x = 1 - a
  157. raise DateTestError, 'int-date should have failed'
  158. except TypeError:
  159. pass
  160. try:
  161. x = a + b
  162. raise DateTestError, 'date+date should have failed'
  163. except TypeError:
  164. pass
  165. if a.weekday() != 'Tuesday':
  166. raise DateTestError, 'weekday() failure'
  167. if max(a,b) is not b or min(a,b) is not a:
  168. raise DateTestError, 'min/max failure'
  169. d = {a-1:b, b:a+1}
  170. if d[b-366] != b or d[a+(b-a)] != Date(10,1,1913):
  171. raise DateTestError, 'dictionary failure'
  172. # verify date<->number conversions for first and last days for
  173. # all years in firstyear .. lastyear
  174. lord = _days_before_year(firstyear)
  175. y = firstyear
  176. while y <= lastyear:
  177. ford = lord + 1
  178. lord = ford + _days_in_year(y) - 1
  179. fd, ld = Date(1,1,y), Date(12,31,y)
  180. if (fd.ord,ld.ord) != (ford,lord):
  181. raise DateTestError, ('date->num failed', y)
  182. fd, ld = _num2date(ford), _num2date(lord)
  183. if (1,1,y,12,31,y) != \
  184. (fd.month,fd.day,fd.year,ld.month,ld.day,ld.year):
  185. raise DateTestError, ('num->date failed', y)
  186. y = y + 1
  187. if __name__ == '__main__':
  188. test(1850, 2150)