@@ -780,6 +780,8 @@ typedef struct
780780 PyObject * name ;
781781} PyDateTime_TimeZone ;
782782
783+ PyObject * PyDateTime_TimeZone_UTC ;
784+
783785/* Create new timezone instance checking offset range. This
784786 function does not check the name argument. Caller must assure
785787 that offset is a timedelta instance and name is either NULL
@@ -3379,6 +3381,24 @@ _timezone_check_argument(PyObject *dt, const char *meth)
33793381 return -1 ;
33803382}
33813383
3384+ static PyObject *
3385+ timezone_repr (PyDateTime_TimeZone * self )
3386+ {
3387+ /* Note that although timezone is not subclassable, it is convenient
3388+ to use Py_TYPE(self)->tp_name here. */
3389+ const char * type_name = Py_TYPE (self )-> tp_name ;
3390+
3391+ if (((PyObject * )self ) == PyDateTime_TimeZone_UTC )
3392+ return PyUnicode_FromFormat ("%s.utc" , type_name );
3393+
3394+ if (self -> name == NULL )
3395+ return PyUnicode_FromFormat ("%s(%R)" , type_name , self -> offset );
3396+
3397+ return PyUnicode_FromFormat ("%s(%R, %R)" , type_name , self -> offset ,
3398+ self -> name );
3399+ }
3400+
3401+
33823402static PyObject *
33833403timezone_str (PyDateTime_TimeZone * self )
33843404{
@@ -3505,7 +3525,7 @@ static PyTypeObject PyDateTime_TimeZoneType = {
35053525 0 , /* tp_getattr */
35063526 0 , /* tp_setattr */
35073527 0 , /* tp_reserved */
3508- 0 , /* tp_repr */
3528+ ( reprfunc ) timezone_repr , /* tp_repr */
35093529 0 , /* tp_as_number */
35103530 0 , /* tp_as_sequence */
35113531 0 , /* tp_as_mapping */
@@ -5260,7 +5280,7 @@ PyInit_datetime(void)
52605280 Py_DECREF (delta );
52615281 if (x == NULL || PyDict_SetItemString (d , "utc" , x ) < 0 )
52625282 return NULL ;
5263- Py_DECREF ( x ) ;
5283+ PyDateTime_TimeZone_UTC = x ;
52645284
52655285 delta = new_delta (-1 , 60 , 0 , 1 ); /* -23:59 */
52665286 if (delta == NULL )
0 commit comments