@@ -275,6 +275,10 @@ static PyStructSequence_Field struct_time_type_fields[] = {
275
275
{"tm_wday" , "day of week, range [0, 6], Monday is 0" },
276
276
{"tm_yday" , "day of year, range [1, 366]" },
277
277
{"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 */
278
282
{0 }
279
283
};
280
284
@@ -294,6 +298,7 @@ static PyStructSequence_Desc struct_time_type_desc = {
294
298
static int initialized ;
295
299
static PyTypeObject StructTimeType ;
296
300
301
+
297
302
static PyObject *
298
303
tmtotuple (struct tm * p )
299
304
{
@@ -312,6 +317,11 @@ tmtotuple(struct tm *p)
312
317
SET (6 , (p -> tm_wday + 6 ) % 7 ); /* Want Monday == 0 */
313
318
SET (7 , p -> tm_yday + 1 ); /* Want January, 1 == 1 */
314
319
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 */
315
325
#undef SET
316
326
if (PyErr_Occurred ()) {
317
327
Py_XDECREF (v );
@@ -371,7 +381,10 @@ PyDoc_STRVAR(gmtime_doc,
371
381
tm_sec, tm_wday, tm_yday, tm_isdst)\n\
372
382
\n\
373
383
Convert 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." );
375
388
376
389
static int
377
390
pylocaltime (time_t * timep , struct tm * result )
@@ -438,6 +451,17 @@ gettmarg(PyObject *args, struct tm *p)
438
451
p -> tm_mon -- ;
439
452
p -> tm_wday = (p -> tm_wday + 1 ) % 7 ;
440
453
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 */
441
465
return 1 ;
442
466
}
443
467
@@ -778,7 +802,10 @@ time_mktime(PyObject *self, PyObject *tup)
778
802
PyDoc_STRVAR (mktime_doc ,
779
803
"mktime(tuple) -> floating point number\n\
780
804
\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." );
782
809
#endif /* HAVE_MKTIME */
783
810
784
811
#ifdef HAVE_WORKING_TZSET
@@ -1443,6 +1470,11 @@ PyInit_time(void)
1443
1470
#endif
1444
1471
}
1445
1472
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
1446
1478
PyModule_AddObject (m , "struct_time" , (PyObject * ) & StructTimeType );
1447
1479
initialized = 1 ;
1448
1480
return m ;
0 commit comments