Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
75f94c21
Commit
75f94c21
authored
Jun 21, 2010
by
Alexander Belopolsky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #9005: Prevent utctimetuple() from producing year 0 or year 10,000.
parent
c56b094b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
31 additions
and
36 deletions
+31
-36
Doc/library/datetime.rst
Doc/library/datetime.rst
+4
-4
Lib/test/test_datetime.py
Lib/test/test_datetime.py
+19
-23
Misc/NEWS
Misc/NEWS
+5
-0
Modules/datetimemodule.c
Modules/datetimemodule.c
+3
-9
No files found.
Doc/library/datetime.rst
View file @
75f94c21
...
@@ -974,10 +974,10 @@ Instance methods:
...
@@ -974,10 +974,10 @@ Instance methods:
``d.dst()`` returns. DST is never in effect for a UTC time.
``d.dst()`` returns. DST is never in effect for a UTC time.
If *d* is aware, *d* is normalized to UTC time, by subtracting
If *d* is aware, *d* is normalized to UTC time, by subtracting
``d.utcoffset()``, and a :class:`time.struct_time` for the
normalized time is
``d.utcoffset()``, and a :class:`time.struct_time` for the
returned. :attr:`tm_isdst` is forced to 0. Note that the result's
normalized time is returned. :attr:`tm_isdst` is forced to 0. Note
:attr:`tm_year` member may be :const:`MINYEAR`\ -1 or :const:`MAXYEAR`\ +1, if
that an :exc:`OverflowError` may be raised if *d*.year was
*d*.year was
``MINYEAR`` or ``MAXYEAR`` and UTC adjustment spills over a year
``MINYEAR`` or ``MAXYEAR`` and UTC adjustment spills over a year
boundary.
boundary.
...
...
Lib/test/test_datetime.py
View file @
75f94c21
...
@@ -2997,8 +2997,6 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase, unittest.TestCase):
...
@@ -2997,8 +2997,6 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase, unittest.TestCase):
def
utcoffset
(
self
,
dt
):
def
utcoffset
(
self
,
dt
):
return
self
.
uofs
return
self
.
uofs
# Ensure tm_isdst is 0 regardless of what dst() says: DST is never
# in effect for a UTC time.
for
dstvalue
in
-
33
,
33
,
0
,
None
:
for
dstvalue
in
-
33
,
33
,
0
,
None
:
d
=
cls
(
1
,
2
,
3
,
10
,
20
,
30
,
40
,
tzinfo
=
UOFS
(
-
53
,
dstvalue
))
d
=
cls
(
1
,
2
,
3
,
10
,
20
,
30
,
40
,
tzinfo
=
UOFS
(
-
53
,
dstvalue
))
t
=
d
.
utctimetuple
()
t
=
d
.
utctimetuple
()
...
@@ -3011,34 +3009,32 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase, unittest.TestCase):
...
@@ -3011,34 +3009,32 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase, unittest.TestCase):
self
.
assertEqual
(
d
.
weekday
(),
t
.
tm_wday
)
self
.
assertEqual
(
d
.
weekday
(),
t
.
tm_wday
)
self
.
assertEqual
(
d
.
toordinal
()
-
date
(
1
,
1
,
1
).
toordinal
()
+
1
,
self
.
assertEqual
(
d
.
toordinal
()
-
date
(
1
,
1
,
1
).
toordinal
()
+
1
,
t
.
tm_yday
)
t
.
tm_yday
)
# Ensure tm_isdst is 0 regardless of what dst() says: DST
# is never in effect for a UTC time.
self
.
assertEqual
(
0
,
t
.
tm_isdst
)
self
.
assertEqual
(
0
,
t
.
tm_isdst
)
# At the edges, UTC adjustment can normalize into years out-of-range
# Check that utctimetuple() is the same as
# for a datetime object. Ensure that a correct timetuple is
# astimezone(utc).timetuple()
# created anyway.
d
=
cls
(
2010
,
11
,
13
,
14
,
15
,
16
,
171819
)
for
tz
in
[
timezone
.
min
,
timezone
.
utc
,
timezone
.
max
]:
dtz
=
d
.
replace
(
tzinfo
=
tz
)
self
.
assertEqual
(
dtz
.
utctimetuple
()[:
-
1
],
dtz
.
astimezone
(
timezone
.
utc
).
timetuple
()[:
-
1
])
# At the edges, UTC adjustment can produce years out-of-range
# for a datetime object. Ensure that an OverflowError is
# raised.
tiny
=
cls
(
MINYEAR
,
1
,
1
,
0
,
0
,
37
,
tzinfo
=
UOFS
(
1439
))
tiny
=
cls
(
MINYEAR
,
1
,
1
,
0
,
0
,
37
,
tzinfo
=
UOFS
(
1439
))
# That goes back 1 minute less than a full day.
# That goes back 1 minute less than a full day.
t
=
tiny
.
utctimetuple
()
self
.
assertRaises
(
OverflowError
,
tiny
.
utctimetuple
)
self
.
assertEqual
(
t
.
tm_year
,
MINYEAR
-
1
)
self
.
assertEqual
(
t
.
tm_mon
,
12
)
self
.
assertEqual
(
t
.
tm_mday
,
31
)
self
.
assertEqual
(
t
.
tm_hour
,
0
)
self
.
assertEqual
(
t
.
tm_min
,
1
)
self
.
assertEqual
(
t
.
tm_sec
,
37
)
self
.
assertEqual
(
t
.
tm_yday
,
366
)
# "year 0" is a leap year
self
.
assertEqual
(
t
.
tm_isdst
,
0
)
huge
=
cls
(
MAXYEAR
,
12
,
31
,
23
,
59
,
37
,
999999
,
tzinfo
=
UOFS
(
-
1439
))
huge
=
cls
(
MAXYEAR
,
12
,
31
,
23
,
59
,
37
,
999999
,
tzinfo
=
UOFS
(
-
1439
))
# That goes forward 1 minute less than a full day.
# That goes forward 1 minute less than a full day.
t
=
huge
.
utctimetuple
()
self
.
assertRaises
(
OverflowError
,
huge
.
utctimetuple
)
self
.
assertEqual
(
t
.
tm_year
,
MAXYEAR
+
1
)
# More overflow cases
self
.
assertEqual
(
t
.
tm_mon
,
1
)
tiny
=
cls
.
min
.
replace
(
tzinfo
=
timezone
(
MINUTE
))
self
.
assertEqual
(
t
.
tm_mday
,
1
)
self
.
assertRaises
(
OverflowError
,
tiny
.
utctimetuple
)
self
.
assertEqual
(
t
.
tm_hour
,
23
)
huge
=
cls
.
max
.
replace
(
tzinfo
=
timezone
(
-
MINUTE
))
self
.
assertEqual
(
t
.
tm_min
,
58
)
self
.
assertRaises
(
OverflowError
,
huge
.
utctimetuple
)
self
.
assertEqual
(
t
.
tm_sec
,
37
)
self
.
assertEqual
(
t
.
tm_yday
,
1
)
self
.
assertEqual
(
t
.
tm_isdst
,
0
)
def
test_tzinfo_isoformat
(
self
):
def
test_tzinfo_isoformat
(
self
):
zero
=
FixedOffset
(
0
,
"+00:00"
)
zero
=
FixedOffset
(
0
,
"+00:00"
)
...
...
Misc/NEWS
View file @
75f94c21
...
@@ -1330,6 +1330,11 @@ Library
...
@@ -1330,6 +1330,11 @@ Library
Extension Modules
Extension Modules
-----------------
-----------------
- Issue #9005: Prevent utctimetuple() from producing year 0 or year
10,000. Prior to this change, timezone adjustment in utctimetuple()
could produce tm_year value of 0 or 10,000. Now an OverflowError is
raised in these edge cases.
- Issue #6641: The ``datetime.strptime`` method now supports the
- Issue #6641: The ``datetime.strptime`` method now supports the
``%z`` directive. When the ``%z`` directive is present in the
``%z`` directive. When the ``%z`` directive is present in the
format string, an aware ``datetime`` object is returned with
format string, an aware ``datetime`` object is returned with
...
...
Modules/datetimemodule.c
View file @
75f94c21
...
@@ -4932,15 +4932,9 @@ datetime_utctimetuple(PyDateTime_DateTime *self)
...
@@ -4932,15 +4932,9 @@ datetime_utctimetuple(PyDateTime_DateTime *self)
mm
-=
offset
;
mm
-=
offset
;
stat
=
normalize_datetime
(
&
y
,
&
m
,
&
d
,
&
hh
,
&
mm
,
&
ss
,
&
us
);
stat
=
normalize_datetime
(
&
y
,
&
m
,
&
d
,
&
hh
,
&
mm
,
&
ss
,
&
us
);
if
(
stat
<
0
)
{
/* OverflowError may be raised in the edge cases. */
/* At the edges, it's possible we overflowed
if
(
stat
<
0
)
* beyond MINYEAR or MAXYEAR.
return
NULL
;
*/
if
(
PyErr_ExceptionMatches
(
PyExc_OverflowError
))
PyErr_Clear
();
else
return
NULL
;
}
}
}
return
build_struct_time
(
y
,
m
,
d
,
hh
,
mm
,
ss
,
0
);
return
build_struct_time
(
y
,
m
,
d
,
hh
,
mm
,
ss
,
0
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment