Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e11b510

Browse files
committed
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().
1 parent 80475bb commit e11b510

3 files changed

Lines changed: 18 additions & 22 deletions

File tree

Doc/lib/libcalendar.tex

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ \section{\module{calendar} ---
1515
week to Sunday (6) or to any other weekday. Parameters that specify
1616
dates are given as integers.
1717

18-
Most of these functions rely on the platform provided \function{mktime()}.
19-
Therefore, valid argument values may vary from system to system.
20-
On Unix, valid years are typically between \code{1970} and \code{2037},
21-
but may be work between \code{1902} and \code{2037}.
18+
Most of these functions rely on the \module{datetime} module which
19+
uses an idealized calendar, the current Gregorian calendar indefinitely
20+
extended in both directions. This matches the definition of the
21+
"proleptic Gregorian" calendar in Dershowitz and Reingold's book
22+
"Calendrical Calculations", where it's the base calendar for all
23+
computations.
2224

2325
\begin{funcdesc}{setfirstweekday}{weekday}
2426
Sets the weekday (\code{0} is Monday, \code{6} is Sunday) to start

Lib/calendar.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
Sunday as the last (the European convention). Use setfirstweekday() to
66
set the first day of the week (0=Monday, 6=Sunday)."""
77

8-
# Revision 2: uses functions from built-in time module
9-
10-
# Import functions and variables from time module
11-
from time import localtime, mktime, strftime
8+
import datetime
129

1310
__all__ = ["error","setfirstweekday","firstweekday","isleap",
1411
"leapdays","weekday","monthrange","monthcalendar",
@@ -35,7 +32,7 @@ def __init__(self, format):
3532
self.format = format
3633

3734
def __getitem__(self, i):
38-
data = [strftime(self.format, (2001, j, 1, 12, 0, 0, 1, 1, 0))
35+
data = [datetime.date(2001, j, 1).strftime(self.format)
3936
for j in range(1, 13)]
4037
data.insert(0, "")
4138
return data[i]
@@ -49,7 +46,7 @@ def __init__(self, format):
4946

5047
def __getitem__(self, i):
5148
# January 1, 2001, was a Monday.
52-
data = [strftime(self.format, (2001, 1, j+1, 12, 0, 0, j, j+1, 0))
49+
data = [datetime.date(2001, 1, j+1).strftime(self.format)
5350
for j in range(7)]
5451
return data[i]
5552

@@ -89,14 +86,12 @@ def leapdays(y1, y2):
8986
Assume y1 <= y2."""
9087
y1 -= 1
9188
y2 -= 1
92-
return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400)
89+
return (y2//4 - y1//4) - (y2//100 - y1//100) + (y2//400 - y1//400)
9390

9491
def weekday(year, month, day):
9592
"""Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12),
9693
day (1-31)."""
97-
secs = mktime((year, month, day, 0, 0, 0, 0, 0, 0))
98-
tuple = localtime(secs)
99-
return tuple[6]
94+
return datetime.date(year, month, day).weekday()
10095

10196
def monthrange(year, month):
10297
"""Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for
@@ -213,17 +208,12 @@ def calendar(year, w=0, l=0, c=_spacing):
213208
return s[:-l] + '\n'
214209

215210
EPOCH = 1970
211+
_EPOCH_ORD = datetime.date(EPOCH, 1, 1).toordinal()
212+
216213
def timegm(tuple):
217214
"""Unrelated but handy function to calculate Unix timestamp from GMT."""
218215
year, month, day, hour, minute, second = tuple[:6]
219-
assert year >= EPOCH
220-
assert 1 <= month <= 12
221-
days = 365*(year-EPOCH) + leapdays(EPOCH, year)
222-
for i in range(1, month):
223-
days = days + mdays[i]
224-
if month > 2 and isleap(year):
225-
days = days + 1
226-
days = days + day - 1
216+
days = datetime.date(year, month, day).toordinal() - _EPOCH_ORD
227217
hours = days*24 + hour
228218
minutes = hours*60 + minute
229219
seconds = minutes*60 + second

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,10 @@ Extension modules
443443
Library
444444
-------
445445

446+
- calendar.py now depends on the new datetime module rather than
447+
the time module. As a result, the range of allowable dates
448+
has been increased.
449+
446450
- pdb has a new 'j(ump)' command to select the next line to be
447451
executed.
448452

0 commit comments

Comments
 (0)