summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>2010-10-14 17:03:51 (GMT)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>2010-10-14 17:03:51 (GMT)
commit1bcbaab15a38ce23cfa4e5ddfee507b75b7cc0b4 (patch)
tree92b95c8558e6a10a2545a7f7c1517e8d84466f66 /Modules
parent5bc4fa7a4947a39cbfdd3dd2ea1019bfa0f93085 (diff)
downloadcpython-1bcbaab15a38ce23cfa4e5ddfee507b75b7cc0b4.zip
cpython-1bcbaab15a38ce23cfa4e5ddfee507b75b7cc0b4.tar.gz
cpython-1bcbaab15a38ce23cfa4e5ddfee507b75b7cc0b4.tar.bz2
Issue 9183: Intern UTC timezone.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_datetimemodule.c45
1 files changed, 31 insertions, 14 deletions
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 8c47351..9a04549 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -767,14 +767,15 @@ typedef struct
PyObject *name;
} PyDateTime_TimeZone;
-PyObject *PyDateTime_TimeZone_UTC;
+/* The interned UTC timezone instance */
+static 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
or a unicode object. */
static PyObject *
-new_timezone(PyObject *offset, PyObject *name)
+create_timezone(PyObject *offset, PyObject *name)
{
PyDateTime_TimeZone *self;
PyTypeObject *type = &PyDateTime_TimeZoneType;
@@ -783,6 +784,30 @@ new_timezone(PyObject *offset, PyObject *name)
assert(PyDelta_Check(offset));
assert(name == NULL || PyUnicode_Check(name));
+ self = (PyDateTime_TimeZone *)(type->tp_alloc(type, 0));
+ if (self == NULL) {
+ return NULL;
+ }
+ Py_INCREF(offset);
+ self->offset = offset;
+ Py_XINCREF(name);
+ self->name = name;
+ return (PyObject *)self;
+}
+
+static int delta_bool(PyDateTime_Delta *self);
+
+static PyObject *
+new_timezone(PyObject *offset, PyObject *name)
+{
+ assert(offset != NULL);
+ assert(PyDelta_Check(offset));
+ assert(name == NULL || PyUnicode_Check(name));
+
+ if (name == NULL && delta_bool((PyDateTime_Delta *)offset) == 0) {
+ Py_INCREF(PyDateTime_TimeZone_UTC);
+ return PyDateTime_TimeZone_UTC;
+ }
if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) {
PyErr_Format(PyExc_ValueError, "offset must be a timedelta"
" representing a whole number of minutes");
@@ -796,15 +821,7 @@ new_timezone(PyObject *offset, PyObject *name)
return NULL;
}
- self = (PyDateTime_TimeZone *)(type->tp_alloc(type, 0));
- if (self == NULL) {
- return NULL;
- }
- Py_INCREF(offset);
- self->offset = offset;
- Py_XINCREF(name);
- self->name = name;
- return (PyObject *)self;
+ return create_timezone(offset, name);
}
/* ---------------------------------------------------------------------------
@@ -5156,7 +5173,7 @@ PyInit__datetime(void)
delta = new_delta(0, 0, 0, 0);
if (delta == NULL)
return NULL;
- x = new_timezone(delta, NULL);
+ x = create_timezone(delta, NULL);
Py_DECREF(delta);
if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0)
return NULL;
@@ -5165,7 +5182,7 @@ PyInit__datetime(void)
delta = new_delta(-1, 60, 0, 1); /* -23:59 */
if (delta == NULL)
return NULL;
- x = new_timezone(delta, NULL);
+ x = create_timezone(delta, NULL);
Py_DECREF(delta);
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
return NULL;
@@ -5174,7 +5191,7 @@ PyInit__datetime(void)
delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */
if (delta == NULL)
return NULL;
- x = new_timezone(delta, NULL);
+ x = create_timezone(delta, NULL);
Py_DECREF(delta);
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
return NULL;