[Python-checkins] python/dist/src/Lib calendar.py,1.30,1.31
rhettinger@users.sourceforge.net
rhettinger@users.sourceforge.net
Wed, 25 Dec 2002 08:37:21 -0800
Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv24875/Lib
Modified Files:
calendar.py
Log Message:
SF 658405: calendar.py to rely on the datetime module instead of the time
module.
The code is shorter, more readable, faster, and dramatically increases the
range of acceptable dates.
Also, used the floor division operator in leapdays().
Index: calendar.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/calendar.py,v
retrieving revision 1.30
retrieving revision 1.31
diff -C2 -d -r1.30 -r1.31
*** calendar.py 22 Oct 2002 05:15:17 -0000 1.30
--- calendar.py 25 Dec 2002 16:37:19 -0000 1.31
***************
*** 6,13 ****
set the first day of the week (0=Monday, 6=Sunday)."""
! # Revision 2: uses functions from built-in time module
!
! # Import functions and variables from time module
! from time import localtime, mktime, strftime
__all__ = ["error","setfirstweekday","firstweekday","isleap",
--- 6,10 ----
set the first day of the week (0=Monday, 6=Sunday)."""
! import datetime
__all__ = ["error","setfirstweekday","firstweekday","isleap",
***************
*** 36,40 ****
def __getitem__(self, i):
! data = [strftime(self.format, (2001, j, 1, 12, 0, 0, 1, 1, 0))
for j in range(1, 13)]
data.insert(0, "")
--- 33,37 ----
def __getitem__(self, i):
! data = [datetime.date(2001, j, 1).strftime(self.format)
for j in range(1, 13)]
data.insert(0, "")
***************
*** 50,54 ****
def __getitem__(self, i):
# January 1, 2001, was a Monday.
! data = [strftime(self.format, (2001, 1, j+1, 12, 0, 0, j, j+1, 0))
for j in range(7)]
return data[i]
--- 47,51 ----
def __getitem__(self, i):
# January 1, 2001, was a Monday.
! data = [datetime.date(2001, 1, j+1).strftime(self.format)
for j in range(7)]
return data[i]
***************
*** 90,101 ****
y1 -= 1
y2 -= 1
! return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400)
def weekday(year, month, day):
"""Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12),
day (1-31)."""
! secs = mktime((year, month, day, 0, 0, 0, 0, 0, 0))
! tuple = localtime(secs)
! return tuple[6]
def monthrange(year, month):
--- 87,96 ----
y1 -= 1
y2 -= 1
! return (y2//4 - y1//4) - (y2//100 - y1//100) + (y2//400 - y1//400)
def weekday(year, month, day):
"""Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12),
day (1-31)."""
! return datetime.date(year, month, day).weekday()
def monthrange(year, month):
***************
*** 214,228 ****
EPOCH = 1970
def timegm(tuple):
"""Unrelated but handy function to calculate Unix timestamp from GMT."""
year, month, day, hour, minute, second = tuple[:6]
! assert year >= EPOCH
! assert 1 <= month <= 12
! days = 365*(year-EPOCH) + leapdays(EPOCH, year)
! for i in range(1, month):
! days = days + mdays[i]
! if month > 2 and isleap(year):
! days = days + 1
! days = days + day - 1
hours = days*24 + hour
minutes = hours*60 + minute
--- 209,218 ----
EPOCH = 1970
+ _EPOCH_ORD = datetime.date(EPOCH, 1, 1).toordinal()
+
def timegm(tuple):
"""Unrelated but handy function to calculate Unix timestamp from GMT."""
year, month, day, hour, minute, second = tuple[:6]
! days = datetime.date(year, month, day).toordinal() - _EPOCH_ORD
hours = days*24 + hour
minutes = hours*60 + minute