raise TypeError("fromutc() argument must be a datetime instance"
" or None")
- _maxoffset = timedelta(hours=23, minutes=59)
+ _maxoffset = timedelta(hours=24, microseconds=-1)
_minoffset = -_maxoffset
@staticmethod
return f'UTC{sign}{hours:02d}:{minutes:02d}'
timezone.utc = timezone._create(timedelta(0))
-timezone.min = timezone._create(timezone._minoffset)
-timezone.max = timezone._create(timezone._maxoffset)
+# bpo-37642: These attributes are rounded to the nearest minute for backwards
+# compatibility, even though the constructor will accept a wider range of
+# values. This may change in the future.
+timezone.min = timezone._create(-timedelta(hours=23, minutes=59))
+timezone.max = timezone._create(timedelta(hours=23, minutes=59))
_EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc)
# Some time zone algebra. For a datetime x, let
tz_copy = copy.deepcopy(tz)
self.assertIs(tz_copy, tz)
+ def test_offset_boundaries(self):
+ # Test timedeltas close to the boundaries
+ time_deltas = [
+ timedelta(hours=23, minutes=59),
+ timedelta(hours=23, minutes=59, seconds=59),
+ timedelta(hours=23, minutes=59, seconds=59, microseconds=999999),
+ ]
+ time_deltas.extend([-delta for delta in time_deltas])
+
+ for delta in time_deltas:
+ with self.subTest(test_type='good', delta=delta):
+ timezone(delta)
+
+ # Test timedeltas on and outside the boundaries
+ bad_time_deltas = [
+ timedelta(hours=24),
+ timedelta(hours=24, microseconds=1),
+ ]
+ bad_time_deltas.extend([-delta for delta in bad_time_deltas])
+
+ for delta in bad_time_deltas:
+ with self.subTest(test_type='bad', delta=delta):
+ with self.assertRaises(ValueError):
+ timezone(delta)
+
#############################################################################
# Base class for testing a particular aspect of timedelta, time, date and
Robert Leenders
Tim Hopper
Dan Lidral-Porter
+Ngalim Siregar
--- /dev/null
+Allowed the pure Python implementation of :class:`datetime.timezone` to represent
+sub-minute offsets close to minimum and maximum boundaries, specifically in the
+ranges (23:59, 24:00) and (-23:59, 24:00). Patch by Ngalim Siregar
Py_INCREF(PyDateTime_TimeZone_UTC);
return PyDateTime_TimeZone_UTC;
}
- if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) ||
+ if ((GET_TD_DAYS(offset) == -1 &&
+ GET_TD_SECONDS(offset) == 0 &&
+ GET_TD_MICROSECONDS(offset) < 1) ||
GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) {
PyErr_Format(PyExc_ValueError, "offset must be a timedelta"
" strictly between -timedelta(hours=24) and"
if (offset == Py_None || offset == NULL)
return offset;
if (PyDelta_Check(offset)) {
- if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) ||
+ if ((GET_TD_DAYS(offset) == -1 &&
+ GET_TD_SECONDS(offset) == 0 &&
+ GET_TD_MICROSECONDS(offset) < 1) ||
GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) {
Py_DECREF(offset);
PyErr_Format(PyExc_ValueError, "offset must be a timedelta"
PyDateTime_TimeZone_UTC = x;
CAPI.TimeZone_UTC = PyDateTime_TimeZone_UTC;
+ /* bpo-37642: These attributes are rounded to the nearest minute for backwards
+ * compatibility, even though the constructor will accept a wider range of
+ * values. This may change in the future.*/
delta = new_delta(-1, 60, 0, 1); /* -23:59 */
if (delta == NULL)
return NULL;