@@ -1522,6 +1522,29 @@ PyDoc_STRVAR(get_clock_info_doc,
1522
1522
\n\
1523
1523
Get information of the specified clock." );
1524
1524
1525
+ #if !defined(HAVE_TZNAME ) || defined(__GLIBC__ ) || defined(__CYGWIN__ )
1526
+ static void
1527
+ get_zone (char * zone , int n , struct tm * p )
1528
+ {
1529
+ #ifdef HAVE_STRUCT_TM_TM_ZONE
1530
+ strncpy (zone , p -> tm_zone ? p -> tm_zone : " " , n );
1531
+ #else
1532
+ tzset ();
1533
+ strftime (zone , n , "%Z" , p );
1534
+ #endif
1535
+ }
1536
+
1537
+ static int
1538
+ get_gmtoff (time_t t , struct tm * p )
1539
+ {
1540
+ #ifdef HAVE_STRUCT_TM_TM_ZONE
1541
+ return p -> tm_gmtoff ;
1542
+ #else
1543
+ return timegm (p ) - t ;
1544
+ #endif
1545
+ }
1546
+ #endif /* !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__) */
1547
+
1525
1548
static void
1526
1549
PyInit_timezone (PyObject * m ) {
1527
1550
/* This code moved from PyInit_time wholesale to allow calling it from
@@ -1540,35 +1563,65 @@ PyInit_timezone(PyObject *m) {
1540
1563
1541
1564
And I'm lazy and hate C so nyer.
1542
1565
*/
1566
+ #if defined(HAVE_TZNAME ) && !defined(__GLIBC__ ) && !defined(__CYGWIN__ )
1543
1567
PyObject * otz0 , * otz1 ;
1544
1568
tzset ();
1545
1569
PyModule_AddIntConstant (m , "timezone" , timezone );
1546
1570
#ifdef HAVE_ALTZONE
1547
1571
PyModule_AddIntConstant (m , "altzone" , altzone );
1548
- #elif defined(HAVE_STRUCT_TM_TM_ZONE )
1572
+ #else
1573
+ PyModule_AddIntConstant (m , "altzone" , timezone - 3600 );
1574
+ #endif
1575
+ PyModule_AddIntConstant (m , "daylight" , daylight );
1576
+ otz0 = PyUnicode_DecodeLocale (tzname [0 ], "surrogateescape" );
1577
+ otz1 = PyUnicode_DecodeLocale (tzname [1 ], "surrogateescape" );
1578
+ PyModule_AddObject (m , "tzname" , Py_BuildValue ("(NN)" , otz0 , otz1 ));
1579
+ #else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
1549
1580
{
1550
- static const time_t YEAR = ( 365 * 24 + 6 ) * 3600 ;
1581
+ #define YEAR ((time_t)(( 365 * 24 + 6) * 3600))
1551
1582
time_t t ;
1552
1583
struct tm p ;
1553
1584
long janzone , julyzone ;
1585
+ char janname [10 ], julyname [10 ];
1554
1586
t = (time ((time_t * )0 ) / YEAR ) * YEAR ;
1555
1587
_PyTime_localtime (t , & p );
1556
- janzone = - p .tm_gmtoff ;
1588
+ get_zone (janname , 9 , & p );
1589
+ janzone = - get_gmtoff (t , & p );
1590
+ janname [9 ] = '\0' ;
1557
1591
t += YEAR /2 ;
1558
1592
_PyTime_localtime (t , & p );
1559
- julyzone = - p .tm_gmtoff ;
1560
-
1561
- // DST is reversed in the southern hemisphere.
1562
- PyModule_AddIntConstant (m , "altzone" ,
1563
- (janzone < julyzone ) ? janzone : julyzone );
1593
+ get_zone (julyname , 9 , & p );
1594
+ julyzone = - get_gmtoff (t , & p );
1595
+ julyname [9 ] = '\0' ;
1596
+
1597
+ if ( janzone < julyzone ) {
1598
+ /* DST is reversed in the southern hemisphere */
1599
+ PyModule_AddIntConstant (m , "timezone" , julyzone );
1600
+ PyModule_AddIntConstant (m , "altzone" , janzone );
1601
+ PyModule_AddIntConstant (m , "daylight" ,
1602
+ janzone != julyzone );
1603
+ PyModule_AddObject (m , "tzname" ,
1604
+ Py_BuildValue ("(zz)" ,
1605
+ julyname , janname ));
1606
+ } else {
1607
+ PyModule_AddIntConstant (m , "timezone" , janzone );
1608
+ PyModule_AddIntConstant (m , "altzone" , julyzone );
1609
+ PyModule_AddIntConstant (m , "daylight" ,
1610
+ janzone != julyzone );
1611
+ PyModule_AddObject (m , "tzname" ,
1612
+ Py_BuildValue ("(zz)" ,
1613
+ janname , julyname ));
1614
+ }
1564
1615
}
1565
- #else
1566
- PyModule_AddIntConstant (m , "altzone" , timezone - 3600 );
1567
- #endif
1568
- PyModule_AddIntConstant (m , "daylight" , daylight );
1569
- otz0 = PyUnicode_DecodeLocale (tzname [0 ], "surrogateescape" );
1570
- otz1 = PyUnicode_DecodeLocale (tzname [1 ], "surrogateescape" );
1571
- PyModule_AddObject (m , "tzname" , Py_BuildValue ("(NN)" , otz0 , otz1 ));
1616
+ #ifdef __CYGWIN__
1617
+ tzset ();
1618
+ PyModule_AddIntConstant (m , "timezone" , _timezone );
1619
+ PyModule_AddIntConstant (m , "altzone" , _timezone - 3600 );
1620
+ PyModule_AddIntConstant (m , "daylight" , _daylight );
1621
+ PyModule_AddObject (m , "tzname" ,
1622
+ Py_BuildValue ("(zz)" , _tzname [0 ], _tzname [1 ]));
1623
+ #endif /* __CYGWIN__ */
1624
+ #endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
1572
1625
}
1573
1626
1574
1627
0 commit comments