diff options
author | Alexander Belopolsky <alexander.belopolsky@gmail.com> | 2016-09-28 21:31:35 (GMT) |
---|---|---|
committer | Alexander Belopolsky <alexander.belopolsky@gmail.com> | 2016-09-28 21:31:35 (GMT) |
commit | 3e7a3cb903258321a17265a1c01976f88e7b72fc (patch) | |
tree | e848e2281c28dfccf6a277876c60b040579fec2b /Python | |
parent | e5ccf3d699162e896b2bf7560d42d77a82405c5e (diff) | |
download | cpython-3e7a3cb903258321a17265a1c01976f88e7b72fc.zip cpython-3e7a3cb903258321a17265a1c01976f88e7b72fc.tar.gz cpython-3e7a3cb903258321a17265a1c01976f88e7b72fc.tar.bz2 |
Issue #28148: Stop using localtime() and gmtime() in the time module.
Introduced platform independent _PyTime_localtime API that is similar
to POSIX localtime_r, but available on all platforms. Patch by Ed
Schouten.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pytime.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/Python/pytime.c b/Python/pytime.c index 02ef8ee..3015a6b 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -766,3 +766,55 @@ _PyTime_Init(void) return 0; } + +int +_PyTime_localtime(time_t t, struct tm *tm) +{ +#ifdef MS_WINDOWS + int error; + + error = localtime_s(tm, &t); + if (error != 0) { + errno = error; + PyErr_SetFromErrno(PyExc_OSError); + return -1; + } + return 0; +#else /* !MS_WINDOWS */ + if (localtime_r(&t, tm) == NULL) { +#ifdef EINVAL + if (errno == 0) + errno = EINVAL; +#endif + PyErr_SetFromErrno(PyExc_OSError); + return -1; + } + return 0; +#endif /* MS_WINDOWS */ +} + +int +_PyTime_gmtime(time_t t, struct tm *tm) +{ +#ifdef MS_WINDOWS + int error; + + error = gmtime_s(tm, &t); + if (error != 0) { + errno = error; + PyErr_SetFromErrno(PyExc_OSError); + return -1; + } + return 0; +#else /* !MS_WINDOWS */ + if (gmtime_r(&t, tm) == NULL) { +#ifdef EINVAL + if (errno == 0) + errno = EINVAL; +#endif + PyErr_SetFromErrno(PyExc_OSError); + return -1; + } + return 0; +#endif /* MS_WINDOWS */ +} |