Skip to content

Commit a049f57

Browse files
pganssleabalkin
authored andcommitted
Test that new_timezone can return the UTC singleton (gh-5318)
1 parent 48e8c82 commit a049f57

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Lib/test/datetimetester.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5489,6 +5489,28 @@ def test_timezones_capi(self):
54895489

54905490
self.assertEqual(dt1.astimezone(timezone.utc), dt_utc)
54915491

5492+
def test_timezones_offset_zero(self):
5493+
utc0, utc1, non_utc = _testcapi.get_timezones_offset_zero()
5494+
5495+
with self.subTest(testname="utc0"):
5496+
self.assertIs(utc0, timezone.utc)
5497+
5498+
with self.subTest(testname="utc1"):
5499+
self.assertIs(utc1, timezone.utc)
5500+
5501+
with self.subTest(testname="non_utc"):
5502+
self.assertIsNot(non_utc, timezone.utc)
5503+
5504+
non_utc_exp = timezone(timedelta(hours=0), "")
5505+
5506+
self.assertEqual(non_utc, non_utc_exp)
5507+
5508+
dt1 = datetime(2000, 2, 4, tzinfo=non_utc)
5509+
dt2 = datetime(2000, 2, 4, tzinfo=non_utc_exp)
5510+
5511+
self.assertEqual(dt1, dt2)
5512+
self.assertEqual(dt1.tzname(), dt2.tzname())
5513+
54925514
def test_check_date(self):
54935515
class DateSubclass(date):
54945516
pass

Modules/_testcapimodule.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,29 @@ make_timezones_capi(PyObject *self, PyObject *args) {
22932293
return rv;
22942294
}
22952295

2296+
static PyObject *
2297+
get_timezones_offset_zero(PyObject *self, PyObject *args) {
2298+
PyObject *offset = PyDelta_FromDSU(0, 0, 0);
2299+
PyObject *name = PyUnicode_FromString("");
2300+
2301+
// These two should return the UTC singleton
2302+
PyObject *utc_singleton_0 = PyTimeZone_FromOffset(offset);
2303+
PyObject *utc_singleton_1 = PyTimeZone_FromOffsetAndName(offset, NULL);
2304+
2305+
// This one will return +00:00 zone, but not the UTC singleton
2306+
PyObject *non_utc_zone = PyTimeZone_FromOffsetAndName(offset, name);
2307+
2308+
Py_DecRef(offset);
2309+
Py_DecRef(name);
2310+
2311+
PyObject *rv = PyTuple_New(3);
2312+
PyTuple_SET_ITEM(rv, 0, utc_singleton_0);
2313+
PyTuple_SET_ITEM(rv, 1, utc_singleton_1);
2314+
PyTuple_SET_ITEM(rv, 2, non_utc_zone);
2315+
2316+
return rv;
2317+
}
2318+
22962319
static PyObject *
22972320
get_timezone_utc_capi(PyObject* self, PyObject *args) {
22982321
int macro = 0;
@@ -4540,6 +4563,7 @@ static PyMethodDef TestMethods[] = {
45404563
{"datetime_check_delta", datetime_check_delta, METH_VARARGS},
45414564
{"datetime_check_tzinfo", datetime_check_tzinfo, METH_VARARGS},
45424565
{"make_timezones_capi", make_timezones_capi, METH_NOARGS},
4566+
{"get_timezones_offset_zero", get_timezones_offset_zero, METH_NOARGS},
45434567
{"get_timezone_utc_capi", get_timezone_utc_capi, METH_VARARGS},
45444568
{"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
45454569
{"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},

0 commit comments

Comments
 (0)