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
a11d8c03
Commit
a11d8c03
authored
Jul 06, 2010
by
Alexander Belopolsky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #9000: datetime.timezone objects now have eval-friendly repr.
parent
44afa5e6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
2 deletions
+33
-2
Lib/test/test_datetime.py
Lib/test/test_datetime.py
+9
-0
Misc/NEWS
Misc/NEWS
+2
-0
Modules/datetimemodule.c
Modules/datetimemodule.c
+22
-2
No files found.
Lib/test/test_datetime.py
View file @
a11d8c03
...
...
@@ -153,6 +153,15 @@ class TestTimeZone(unittest.TestCase):
timezone
.
min
,
timezone
.
max
]:
self
.
assertEqual
(
str
(
tz
),
tz
.
tzname
(
None
))
def
test_repr
(
self
):
import
datetime
for
tz
in
[
self
.
ACDT
,
self
.
EST
,
timezone
.
utc
,
timezone
.
min
,
timezone
.
max
]:
# test round-trip
tzrep
=
repr
(
tz
)
self
.
assertEqual
(
tz
,
eval
(
tzrep
))
def
test_class_members
(
self
):
limit
=
timedelta
(
hours
=
23
,
minutes
=
59
)
self
.
assertEqual
(
timezone
.
utc
.
utcoffset
(
None
),
ZERO
)
...
...
Misc/NEWS
View file @
a11d8c03
...
...
@@ -1394,6 +1394,8 @@ Library
Extension Modules
-----------------
- Issue #9000: datetime.timezone objects now have eval-friendly repr.
- In the math module, correctly lookup __trunc__, __ceil__, and __floor__ as
special methods.
...
...
Modules/datetimemodule.c
View file @
a11d8c03
...
...
@@ -780,6 +780,8 @@ typedef struct
PyObject
*
name
;
}
PyDateTime_TimeZone
;
PyObject
*
PyDateTime_TimeZone_UTC
;
/* Create new timezone instance checking offset range. This
function does not check the name argument. Caller must assure
that offset is a timedelta instance and name is either NULL
...
...
@@ -3379,6 +3381,24 @@ _timezone_check_argument(PyObject *dt, const char *meth)
return
-
1
;
}
static
PyObject
*
timezone_repr
(
PyDateTime_TimeZone
*
self
)
{
/* Note that although timezone is not subclassable, it is convenient
to use Py_TYPE(self)->tp_name here. */
const
char
*
type_name
=
Py_TYPE
(
self
)
->
tp_name
;
if
(((
PyObject
*
)
self
)
==
PyDateTime_TimeZone_UTC
)
return
PyUnicode_FromFormat
(
"%s.utc"
,
type_name
);
if
(
self
->
name
==
NULL
)
return
PyUnicode_FromFormat
(
"%s(%R)"
,
type_name
,
self
->
offset
);
return
PyUnicode_FromFormat
(
"%s(%R, %R)"
,
type_name
,
self
->
offset
,
self
->
name
);
}
static
PyObject
*
timezone_str
(
PyDateTime_TimeZone
*
self
)
{
...
...
@@ -3505,7 +3525,7 @@ static PyTypeObject PyDateTime_TimeZoneType = {
0
,
/* tp_getattr */
0
,
/* tp_setattr */
0
,
/* tp_reserved */
0
,
/* tp_repr */
(
reprfunc
)
timezone_repr
,
/* tp_repr */
0
,
/* tp_as_number */
0
,
/* tp_as_sequence */
0
,
/* tp_as_mapping */
...
...
@@ -5260,7 +5280,7 @@ PyInit_datetime(void)
Py_DECREF
(
delta
);
if
(
x
==
NULL
||
PyDict_SetItemString
(
d
,
"utc"
,
x
)
<
0
)
return
NULL
;
Py
_DECREF
(
x
)
;
Py
DateTime_TimeZone_UTC
=
x
;
delta
=
new_delta
(
-
1
,
60
,
0
,
1
);
/* -23:59 */
if
(
delta
==
NULL
)
...
...
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