The Date class.
| 76 | |
| 77 | |
| 78 | class Date(object): |
| 79 | """The Date class.""" |
| 80 | |
| 81 | Weekdays = ["Monday", |
| 82 | "Tuesday", |
| 83 | "Wednesday", |
| 84 | "Thursday", |
| 85 | "Friday", |
| 86 | "Saturday", |
| 87 | "Sunday"] |
| 88 | |
| 89 | Months = ["January", |
| 90 | "February", |
| 91 | "March", |
| 92 | "April", |
| 93 | "May", |
| 94 | "June", |
| 95 | "July", |
| 96 | "August", |
| 97 | "September", |
| 98 | "October", |
| 99 | "November", |
| 100 | "December"] |
| 101 | |
| 102 | #The slots in a Date object are constrained to allow more efficient operations. |
| 103 | __slots__ = ["year", "month", "day"] |
| 104 | |
| 105 | def __init__(self, tm = None): |
| 106 | """The initializer has an optional argument, time, in the time module format, |
| 107 | wether as in seconds since the epoch (Unix time) wether as a tuple (time tuple). |
| 108 | If it is not provided, then it returns the current date.""" |
| 109 | if tm is None: |
| 110 | t = time.localtime() |
| 111 | else: |
| 112 | if isinstance(tm, int): |
| 113 | t = time.localtime(tm) |
| 114 | else: |
| 115 | t = tm |
| 116 | |
| 117 | self.year, self.month, self.day = t[:3] |
| 118 | |
| 119 | def weekday(self): |
| 120 | """Returns the weekday of the date. |
| 121 | |
| 122 | The format is as in the time module: Monday is 0 and sunday is 6.""" |
| 123 | a = (14 - self.month)//12 |
| 124 | y = self.year - a |
| 125 | m = self.month + 12*a -2 |
| 126 | d = (self.day + y + y//4 - y//100 + y//400 + (31*m//12))%7 |
| 127 | if d: |
| 128 | ret = d - 1 |
| 129 | else: |
| 130 | ret = 6 |
| 131 | return ret |
| 132 | |
| 133 | def __str__(self): |
| 134 | return "%s, %d-%s-%d" % (Date.Weekdays[self.weekday()], |
| 135 | self.day, |
no outgoing calls
no test coverage detected