time, calendar, datetime, etc

Tim Peters tim.one at comcast.net
Sun Aug 3 13:14:05 EDT 2003


[Dan Bishop]
> There were no leap years between 10 BC and AD 4, because after Julius
> Caesar's death, the priests in charge of the calendar mistakenly added
> leap days every 3 years instead of 4, and this had to be corrected.
>
> I wouldn't expect the datetime module to deal with that, though ;-)

Thank you -- some people did <wink>.


> ...
> >>> datetime.date(1970, 8, 22).weekday()
>  5
> >>> datetime.date(6970, 8, 22).weekday()
>  5
>
> The output above is my least favorite feature of the datetime module.
> It took me a while to figure out that those dates are Saturdays.

That's very peculiar!  Because they're not both Saturdays:

>>> datetime.date(1970, 8, 22).weekday()
5
>>> datetime.date(6970, 8, 22).weekday()
2
>>>

Or if you want names instead:

>>> datetime.date(1970, 8, 22).ctime()[:3]
'Sat'
>>> datetime.date(6970, 8, 22).ctime()[:3]
'Wed'
>>>

Did you actually run the example you pasted, or just assume that the second
line would display 5 too, or didn't paste the actual example you ran and
made a typo?

The earlier claim:

> The Gregorian leap year cycle is 400 years and this is coincidentally
> also a whole number of weeks.

was correct, but the 5000 years between 1970 and 6970 isn't a multiple of
400, so the example as given wasn't relevant to that claim.

> Perhaps we could add a weekday class to 2.3.1?
>
> DAY_NAMES = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
>              'Saturday', 'Sunday']
> class weekday(int):
>    def __repr__(self):
>       return DAY_NAMES[self]
> for i, name in enumerate(DAY_NAMES):
>    globals()[name] = weekday(i)

I doubt anything like this will get added, in part because we have the
unfortunate fact that .weekday() and .isoweekday() map day names into little
integers differently.  As shown above, you can easily get English day names
(well, abbreviations) from the .ctime() result.  A .strftime() format works
too, except .strftime() has irritating year-range limitations inherited from
C.

I have a little dateutil.py module I use for my own stuff, containing

MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY = range(7)

(JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
 JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER) = range(1, 13)

at the start.  That's easy, and caters to that (1) I'm unlikely ever to use
.isoweekday(); and, (2) after decades of practice, I'm comfortable with the
English names for these things.  It's much easier to do something similar
for yourself than to try to define a gimmick that covers the union of all
peoples' crazy ideas <wink>.






More information about the Python-list mailing list