@@ -275,6 +275,10 @@ static PyStructSequence_Field struct_time_type_fields[] = {
275275 {"tm_wday" , "day of week, range [0, 6], Monday is 0" },
276276 {"tm_yday" , "day of year, range [1, 366]" },
277277 {"tm_isdst" , "1 if summer time is in effect, 0 if not, and -1 if unknown" },
278+ #ifdef HAVE_STRUCT_TM_TM_ZONE
279+ {"tm_zone" , "abbreviation of timezone name" },
280+ {"tm_gmtoff" , "offset from UTC in seconds" },
281+ #endif /* HAVE_STRUCT_TM_TM_ZONE */
278282 {0 }
279283};
280284
@@ -294,6 +298,7 @@ static PyStructSequence_Desc struct_time_type_desc = {
294298static int initialized ;
295299static PyTypeObject StructTimeType ;
296300
301+
297302static PyObject *
298303tmtotuple (struct tm * p )
299304{
@@ -312,6 +317,11 @@ tmtotuple(struct tm *p)
312317 SET (6 , (p -> tm_wday + 6 ) % 7 ); /* Want Monday == 0 */
313318 SET (7 , p -> tm_yday + 1 ); /* Want January, 1 == 1 */
314319 SET (8 , p -> tm_isdst );
320+ #ifdef HAVE_STRUCT_TM_TM_ZONE
321+ PyStructSequence_SET_ITEM (v , 9 ,
322+ PyUnicode_DecodeLocale (p -> tm_zone , "surrogateescape" ));
323+ SET (10 , p -> tm_gmtoff );
324+ #endif /* HAVE_STRUCT_TM_TM_ZONE */
315325#undef SET
316326 if (PyErr_Occurred ()) {
317327 Py_XDECREF (v );
@@ -371,7 +381,10 @@ PyDoc_STRVAR(gmtime_doc,
371381 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
372382\n\
373383Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
374- GMT). When 'seconds' is not passed in, convert the current time instead." );
384+ GMT). When 'seconds' is not passed in, convert the current time instead.\n\
385+ \n\
386+ If the platform supports the tm_gmtoff and tm_zone, they are available as\n\
387+ attributes only." );
375388
376389static int
377390pylocaltime (time_t * timep , struct tm * result )
@@ -438,6 +451,17 @@ gettmarg(PyObject *args, struct tm *p)
438451 p -> tm_mon -- ;
439452 p -> tm_wday = (p -> tm_wday + 1 ) % 7 ;
440453 p -> tm_yday -- ;
454+ #ifdef HAVE_STRUCT_TM_TM_ZONE
455+ if (Py_TYPE (args ) == & StructTimeType ) {
456+ PyObject * item ;
457+ item = PyTuple_GET_ITEM (args , 9 );
458+ p -> tm_zone = item == Py_None ? NULL : _PyUnicode_AsString (item );
459+ item = PyTuple_GET_ITEM (args , 10 );
460+ p -> tm_gmtoff = item == Py_None ? 0 : PyLong_AsLong (item );
461+ if (PyErr_Occurred ())
462+ return 0 ;
463+ }
464+ #endif /* HAVE_STRUCT_TM_TM_ZONE */
441465 return 1 ;
442466}
443467
@@ -778,7 +802,10 @@ time_mktime(PyObject *self, PyObject *tup)
778802PyDoc_STRVAR (mktime_doc ,
779803"mktime(tuple) -> floating point number\n\
780804\n\
781- Convert a time tuple in local time to seconds since the Epoch." );
805+ Convert a time tuple in local time to seconds since the Epoch.\n\
806+ Note that mktime(gmtime(0)) will not generally return zero for most\n\
807+ time zones; instead the returned value will either be equal to that\n\
808+ of the timezone or altzone attributes on the time module." );
782809#endif /* HAVE_MKTIME */
783810
784811#ifdef HAVE_WORKING_TZSET
@@ -1443,6 +1470,11 @@ PyInit_time(void)
14431470#endif
14441471 }
14451472 Py_INCREF (& StructTimeType );
1473+ #ifdef HAVE_STRUCT_TM_TM_ZONE
1474+ PyModule_AddIntConstant (m , "_STRUCT_TM_ITEMS" , 11 );
1475+ #else
1476+ PyModule_AddIntConstant (m , "_STRUCT_TM_ITEMS" , 9 );
1477+ #endif
14461478 PyModule_AddObject (m , "struct_time" , (PyObject * ) & StructTimeType );
14471479 initialized = 1 ;
14481480 return m ;
0 commit comments