| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e5ccf3d commit 3e7a3cb
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -184,6 +184,14 @@ PyAPI_FUNC(int) _PyTime_GetMonotonicClockWithInfo( | |||
| 184 | 184 | Return 0 on success, raise an exception and return -1 on error. */ | |
| 185 | 185 | PyAPI_FUNC(int) _PyTime_Init(void); | |
| 186 | 186 | ||
| 187 | + /* Converts a timestamp to the Gregorian time, using the local time zone. | ||
| 188 | + Return 0 on success, raise an exception and return -1 on error. */ | ||
| 189 | + PyAPI_FUNC(int) _PyTime_localtime(time_t t, struct tm *tm); | ||
| 190 | + | ||
| 191 | + /* Converts a timestamp to the Gregorian time, assuming UTC. | ||
| 192 | + Return 0 on success, raise an exception and return -1 on error. */ | ||
| 193 | + PyAPI_FUNC(int) _PyTime_gmtime(time_t t, struct tm *tm); | ||
| 194 | + | ||
| 187 | 195 | #ifdef __cplusplus | |
| 188 | 196 | } | |
| 189 | 197 | #endif | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1340,6 +1340,7 @@ Michael Schneider | |||
| 1340 | 1340 | Peter Schneider-Kamp | |
| 1341 | 1341 | Arvin Schnell | |
| 1342 | 1342 | Nofar Schnider | |
| 1343 | + Ed Schouten | ||
| 1343 | 1344 | Scott Schram | |
| 1344 | 1345 | Robin Schreiber | |
| 1345 | 1346 | Chad J. Schroeder | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,18 +9,6 @@ | |||
| 9 | 9 | ||
| 10 | 10 | #ifdef MS_WINDOWS | |
| 11 | 11 | # include <winsock2.h> /* struct timeval */ | |
| 12 | - static struct tm *localtime_r(const time_t *timep, struct tm *result) | ||
| 13 | - { | ||
| 14 | - if (localtime_s(result, timep) == 0) | ||
| 15 | - return result; | ||
| 16 | - return NULL; | ||
| 17 | - } | ||
| 18 | - static struct tm *gmtime_r(const time_t *timep, struct tm *result) | ||
| 19 | - { | ||
| 20 | - if (gmtime_s(result, timep) == 0) | ||
| 21 | - return result; | ||
| 22 | - return NULL; | ||
| 23 | - } | ||
| 24 | 12 | #endif | |
| 25 | 13 | ||
| 26 | 14 | /* Differentiate between building the core module and building extension | |
@@ -2529,15 +2517,8 @@ date_local_from_object(PyObject *cls, PyObject *obj) | |||
| 2529 | 2517 | if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_FLOOR) == -1) | |
| 2530 | 2518 | return NULL; | |
| 2531 | 2519 | ||
| 2532 | - if (localtime_r(&t, &tm) == NULL) { | ||
| 2533 | - /* unconvertible time */ | ||
| 2534 | - #ifdef EINVAL | ||
| 2535 | - if (errno == 0) | ||
| 2536 | - errno = EINVAL; | ||
| 2537 | - #endif | ||
| 2538 | - PyErr_SetFromErrno(PyExc_OSError); | ||
| 2520 | + if (_PyTime_localtime(t, &tm) != 0) | ||
| 2539 | 2521 | return NULL; | |
| 2540 | - } | ||
| 2541 | 2522 | ||
| 2542 | 2523 | return PyObject_CallFunction(cls, "iii", | |
| 2543 | 2524 | tm.tm_year + 1900, | |
@@ -4199,8 +4180,9 @@ datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) | |||
| 4199 | 4180 | return self; | |
| 4200 | 4181 | } | |
| 4201 | 4182 | ||
| 4202 | - /* TM_FUNC is the shared type of localtime_r() and gmtime_r(). */ | ||
| 4203 | - typedef struct tm *(*TM_FUNC)(const time_t *timer, struct tm*); | ||
| 4183 | + /* TM_FUNC is the shared type of _PyTime_localtime() and | ||
| 4184 | + * _PyTime_gmtime(). */ | ||
| 4185 | + typedef int (*TM_FUNC)(time_t timer, struct tm*); | ||
| 4204 | 4186 | ||
| 4205 | 4187 | /* As of version 2015f max fold in IANA database is | |
| 4206 | 4188 | * 23 hours at 1969-09-30 13:00:00 in Kwajalein. */ | |
@@ -4229,10 +4211,8 @@ local(long long u) | |||
| 4229 | 4211 | return -1; | |
| 4230 | 4212 | } | |
| 4231 | 4213 | /* XXX: add bounds checking */ | |
| 4232 | - if (localtime_r(&t, &local_time) == NULL) { | ||
| 4233 | - PyErr_SetFromErrno(PyExc_OSError); | ||
| 4214 | + if (_PyTime_localtime(t, &local_time) != 0) | ||
| 4234 | 4215 | return -1; | |
| 4235 | - } | ||
| 4236 | 4216 | return utc_to_seconds(local_time.tm_year + 1900, | |
| 4237 | 4217 | local_time.tm_mon + 1, | |
| 4238 | 4218 | local_time.tm_mday, | |
@@ -4252,13 +4232,8 @@ datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us, | |||
| 4252 | 4232 | struct tm tm; | |
| 4253 | 4233 | int year, month, day, hour, minute, second, fold = 0; | |
| 4254 | 4234 | ||
| 4255 | - if (f(&timet, &tm) == NULL) { | ||
| 4256 | - #ifdef EINVAL | ||
| 4257 | - if (errno == 0) | ||
| 4258 | - errno = EINVAL; | ||
| 4259 | - #endif | ||
| 4260 | - return PyErr_SetFromErrno(PyExc_OSError); | ||
| 4261 | - } | ||
| 4235 | + if (f(timet, &tm) != 0) | ||
| 4236 | + return NULL; | ||
| 4262 | 4237 | ||
| 4263 | 4238 | year = tm.tm_year + 1900; | |
| 4264 | 4239 | month = tm.tm_mon + 1; | |
@@ -4273,7 +4248,7 @@ datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us, | |||
| 4273 | 4248 | */ | |
| 4274 | 4249 | second = Py_MIN(59, tm.tm_sec); | |
| 4275 | 4250 | ||
| 4276 | - if (tzinfo == Py_None && f == localtime_r) { | ||
| 4251 | + if (tzinfo == Py_None && f == _PyTime_localtime) { | ||
| 4277 | 4252 | long long probe_seconds, result_seconds, transition; | |
| 4278 | 4253 | ||
| 4279 | 4254 | result_seconds = utc_to_seconds(year, month, day, | |
@@ -4361,7 +4336,8 @@ datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz) | |||
| 4361 | 4336 | return NULL; | |
| 4362 | 4337 | ||
| 4363 | 4338 | self = datetime_best_possible((PyObject *)type, | |
| 4364 | - tz == Py_None ? localtime_r : gmtime_r, | ||
| 4339 | + tz == Py_None ? _PyTime_localtime : | ||
| 4340 | + _PyTime_gmtime, | ||
| 4365 | 4341 | tz); | |
| 4366 | 4342 | if (self != NULL && tz != Py_None) { | |
| 4367 | 4343 | /* Convert UTC to tzinfo's zone. */ | |
@@ -4376,7 +4352,7 @@ datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz) | |||
| 4376 | 4352 | static PyObject * | |
| 4377 | 4353 | datetime_utcnow(PyObject *cls, PyObject *dummy) | |
| 4378 | 4354 | { | |
| 4379 | - return datetime_best_possible(cls, gmtime_r, Py_None); | ||
| 4355 | + return datetime_best_possible(cls, _PyTime_gmtime, Py_None); | ||
| 4380 | 4356 | } | |
| 4381 | 4357 | ||
| 4382 | 4358 | /* Return new local datetime from timestamp (Python timestamp -- a double). */ | |
@@ -4395,7 +4371,8 @@ datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) | |||
| 4395 | 4371 | return NULL; | |
| 4396 | 4372 | ||
| 4397 | 4373 | self = datetime_from_timestamp(cls, | |
| 4398 | - tzinfo == Py_None ? localtime_r : gmtime_r, | ||
| 4374 | + tzinfo == Py_None ? _PyTime_localtime : | ||
| 4375 | + _PyTime_gmtime, | ||
| 4399 | 4376 | timestamp, | |
| 4400 | 4377 | tzinfo); | |
| 4401 | 4378 | if (self != NULL && tzinfo != Py_None) { | |
@@ -4413,7 +4390,7 @@ datetime_utcfromtimestamp(PyObject *cls, PyObject *args) | |||
| 4413 | 4390 | PyObject *result = NULL; | |
| 4414 | 4391 | ||
| 4415 | 4392 | if (PyArg_ParseTuple(args, "O:utcfromtimestamp", ×tamp)) | |
| 4416 | - result = datetime_from_timestamp(cls, gmtime_r, timestamp, | ||
| 4393 | + result = datetime_from_timestamp(cls, _PyTime_gmtime, timestamp, | ||
| 4417 | 4394 | Py_None); | |
| 4418 | 4395 | return result; | |
| 4419 | 4396 | } | |
@@ -5040,14 +5017,8 @@ local_timezone_from_timestamp(time_t timestamp) | |||
| 5040 | 5017 | PyObject *nameo = NULL; | |
| 5041 | 5018 | const char *zone = NULL; | |
| 5042 | 5019 | ||
| 5043 | - if (localtime_r(×tamp, &local_time_tm) == NULL) { | ||
| 5044 | - #ifdef EINVAL | ||
| 5045 | - if (errno == 0) | ||
| 5046 | - errno = EINVAL; | ||
| 5047 | - #endif | ||
| 5048 | - PyErr_SetFromErrno(PyExc_OSError); | ||
| 5020 | + if (_PyTime_localtime(timestamp, &local_time_tm) != 0) | ||
| 5049 | 5021 | return NULL; | |
| 5050 | - } | ||
| 5051 | 5022 | #ifdef HAVE_STRUCT_TM_TM_ZONE | |
| 5052 | 5023 | zone = local_time_tm.tm_zone; | |
| 5053 | 5024 | delta = new_delta(0, local_time_tm.tm_gmtoff, 0, 1); | |
@@ -5067,14 +5038,8 @@ local_timezone_from_timestamp(time_t timestamp) | |||
| 5067 | 5038 | if (local_time == NULL) { | |
| 5068 | 5039 | return NULL; | |
| 5069 | 5040 | } | |
| 5070 | - if (gmtime_r(×tamp, &utc_time_tm) == NULL) { | ||
| 5071 | - #ifdef EINVAL | ||
| 5072 | - if (errno == 0) | ||
| 5073 | - errno = EINVAL; | ||
| 5074 | - #endif | ||
| 5075 | - PyErr_SetFromErrno(PyExc_OSError); | ||
| 5041 | + if (_PyTime_gmtime(timestamp, &utc_time_tm) != 0) | ||
| 5076 | 5042 | return NULL; | |
| 5077 | - } | ||
| 5078 | 5043 | utc_time = new_datetime(utc_time_tm.tm_year + 1900, | |
| 5079 | 5044 | utc_time_tm.tm_mon + 1, | |
| 5080 | 5045 | utc_time_tm.tm_mday, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -343,21 +343,14 @@ static PyObject * | |||
| 343 | 343 | time_gmtime(PyObject *self, PyObject *args) | |
| 344 | 344 | { | |
| 345 | 345 | time_t when; | |
| 346 | - struct tm buf, *local; | ||
| 346 | + struct tm buf; | ||
| 347 | 347 | ||
| 348 | 348 | if (!parse_time_t_args(args, "|O:gmtime", &when)) | |
| 349 | 349 | return NULL; | |
| 350 | 350 | ||
| 351 | 351 | errno = 0; | |
| 352 | - local = gmtime(&when); | ||
| 353 | - if (local == NULL) { | ||
| 354 | - #ifdef EINVAL | ||
| 355 | - if (errno == 0) | ||
| 356 | - errno = EINVAL; | ||
| 357 | - #endif | ||
| 358 | - return PyErr_SetFromErrno(PyExc_OSError); | ||
| 359 | - } | ||
| 360 | - buf = *local; | ||
| 352 | + if (_PyTime_gmtime(when, &buf) != 0) | ||
| 353 | + return NULL; | ||
| 361 | 354 | #ifdef HAVE_STRUCT_TM_TM_ZONE | |
| 362 | 355 | return tmtotuple(&buf); | |
| 363 | 356 | #else | |
@@ -388,26 +381,6 @@ GMT). When 'seconds' is not passed in, convert the current time instead.\n\ | |||
| 388 | 381 | If the platform supports the tm_gmtoff and tm_zone, they are available as\n\ | |
| 389 | 382 | attributes only."); | |
| 390 | 383 | ||
| 391 | - static int | ||
| 392 | - pylocaltime(time_t *timep, struct tm *result) | ||
| 393 | - { | ||
| 394 | - struct tm *local; | ||
| 395 | - | ||
| 396 | - assert (timep != NULL); | ||
| 397 | - local = localtime(timep); | ||
| 398 | - if (local == NULL) { | ||
| 399 | - /* unconvertible time */ | ||
| 400 | - #ifdef EINVAL | ||
| 401 | - if (errno == 0) | ||
| 402 | - errno = EINVAL; | ||
| 403 | - #endif | ||
| 404 | - PyErr_SetFromErrno(PyExc_OSError); | ||
| 405 | - return -1; | ||
| 406 | - } | ||
| 407 | - *result = *local; | ||
| 408 | - return 0; | ||
| 409 | - } | ||
| 410 | - | ||
| 411 | 384 | static PyObject * | |
| 412 | 385 | time_localtime(PyObject *self, PyObject *args) | |
| 413 | 386 | { | |
@@ -416,7 +389,7 @@ time_localtime(PyObject *self, PyObject *args) | |||
| 416 | 389 | ||
| 417 | 390 | if (!parse_time_t_args(args, "|O:localtime", &when)) | |
| 418 | 391 | return NULL; | |
| 419 | - if (pylocaltime(&when, &buf) == -1) | ||
| 392 | + if (_PyTime_localtime(when, &buf) != 0) | ||
| 420 | 393 | return NULL; | |
| 421 | 394 | #ifdef HAVE_STRUCT_TM_TM_ZONE | |
| 422 | 395 | return tmtotuple(&buf); | |
@@ -611,7 +584,7 @@ time_strftime(PyObject *self, PyObject *args) | |||
| 611 | 584 | ||
| 612 | 585 | if (tup == NULL) { | |
| 613 | 586 | time_t tt = time(NULL); | |
| 614 | - if (pylocaltime(&tt, &buf) == -1) | ||
| 587 | + if (_PyTime_localtime(tt, &buf) != 0) | ||
| 615 | 588 | return NULL; | |
| 616 | 589 | } | |
| 617 | 590 | else if (!gettmarg(tup, &buf) || !checktm(&buf)) | |
@@ -796,7 +769,7 @@ time_asctime(PyObject *self, PyObject *args) | |||
| 796 | 769 | return NULL; | |
| 797 | 770 | if (tup == NULL) { | |
| 798 | 771 | time_t tt = time(NULL); | |
| 799 | - if (pylocaltime(&tt, &buf) == -1) | ||
| 772 | + if (_PyTime_localtime(tt, &buf) != 0) | ||
| 800 | 773 | return NULL; | |
| 801 | 774 | ||
| 802 | 775 | } else if (!gettmarg(tup, &buf) || !checktm(&buf)) | |
@@ -818,7 +791,7 @@ time_ctime(PyObject *self, PyObject *args) | |||
| 818 | 791 | struct tm buf; | |
| 819 | 792 | if (!parse_time_t_args(args, "|O:ctime", &tt)) | |
| 820 | 793 | return NULL; | |
| 821 | - if (pylocaltime(&tt, &buf) == -1) | ||
| 794 | + if (_PyTime_localtime(tt, &buf) != 0) | ||
| 822 | 795 | return NULL; | |
| 823 | 796 | return _asctime(&buf); | |
| 824 | 797 | } | |
@@ -1239,18 +1212,18 @@ PyInit_timezone(PyObject *m) { | |||
| 1239 | 1212 | { | |
| 1240 | 1213 | #define YEAR ((time_t)((365 * 24 + 6) * 3600)) | |
| 1241 | 1214 | time_t t; | |
| 1242 | - struct tm *p; | ||
| 1215 | + struct tm p; | ||
| 1243 | 1216 | long janzone, julyzone; | |
| 1244 | 1217 | char janname[10], julyname[10]; | |
| 1245 | 1218 | t = (time((time_t *)0) / YEAR) * YEAR; | |
| 1246 | - p = localtime(&t); | ||
| 1247 | - get_zone(janname, 9, p); | ||
| 1248 | - janzone = -get_gmtoff(t, p); | ||
| 1219 | + _PyTime_localtime(t, &p); | ||
| 1220 | + get_zone(janname, 9, &p); | ||
| 1221 | + janzone = -get_gmtoff(t, &p); | ||
| 1249 | 1222 | janname[9] = '\0'; | |
| 1250 | 1223 | t += YEAR/2; | |
| 1251 | - p = localtime(&t); | ||
| 1252 | - get_zone(julyname, 9, p); | ||
| 1253 | - julyzone = -get_gmtoff(t, p); | ||
| 1224 | + _PyTime_localtime(t, &p); | ||
| 1225 | + get_zone(julyname, 9, &p); | ||
| 1226 | + julyzone = -get_gmtoff(t, &p); | ||
| 1254 | 1227 | julyname[9] = '\0'; | |
| 1255 | 1228 | ||
| 1256 | 1229 | if( janzone < julyzone ) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -766,3 +766,55 @@ _PyTime_Init(void) | |||
| 766 | 766 | ||
| 767 | 767 | return 0; | |
| 768 | 768 | } | |
| 769 | + | ||
| 770 | + int | ||
| 771 | + _PyTime_localtime(time_t t, struct tm *tm) | ||
| 772 | + { | ||
| 773 | + #ifdef MS_WINDOWS | ||
| 774 | + int error; | ||
| 775 | + | ||
| 776 | + error = localtime_s(tm, &t); | ||
| 777 | + if (error != 0) { | ||
| 778 | + errno = error; | ||
| 779 | + PyErr_SetFromErrno(PyExc_OSError); | ||
| 780 | + return -1; | ||
| 781 | + } | ||
| 782 | + return 0; | ||
| 783 | + #else /* !MS_WINDOWS */ | ||
| 784 | + if (localtime_r(&t, tm) == NULL) { | ||
| 785 | + #ifdef EINVAL | ||
| 786 | + if (errno == 0) | ||
| 787 | + errno = EINVAL; | ||
| 788 | + #endif | ||
| 789 | + PyErr_SetFromErrno(PyExc_OSError); | ||
| 790 | + return -1; | ||
| 791 | + } | ||
| 792 | + return 0; | ||
| 793 | + #endif /* MS_WINDOWS */ | ||
| 794 | + } | ||
| 795 | + | ||
| 796 | + int | ||
| 797 | + _PyTime_gmtime(time_t t, struct tm *tm) | ||
| 798 | + { | ||
| 799 | + #ifdef MS_WINDOWS | ||
| 800 | + int error; | ||
| 801 | + | ||
| 802 | + error = gmtime_s(tm, &t); | ||
| 803 | + if (error != 0) { | ||
| 804 | + errno = error; | ||
| 805 | + PyErr_SetFromErrno(PyExc_OSError); | ||
| 806 | + return -1; | ||
| 807 | + } | ||
| 808 | + return 0; | ||
| 809 | + #else /* !MS_WINDOWS */ | ||
| 810 | + if (gmtime_r(&t, tm) == NULL) { | ||
| 811 | + #ifdef EINVAL | ||
| 812 | + if (errno == 0) | ||
| 813 | + errno = EINVAL; | ||
| 814 | + #endif | ||
| 815 | + PyErr_SetFromErrno(PyExc_OSError); | ||
| 816 | + return -1; | ||
| 817 | + } | ||
| 818 | + return 0; | ||
| 819 | + #endif /* MS_WINDOWS */ | ||
| 820 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments