blob: 10cc8e12a6ab0360de97528b6ef8043132ca9164 [file] [log] [blame]
Alexander Belopolskyd95a5862010-06-16 22:38:15 +00001#include "Python.h"
2#include "_time.h"
3
4/* Exposed in timefuncs.h. */
5time_t
6_PyTime_DoubleToTimet(double x)
7{
8 time_t result;
9 double diff;
10
11 result = (time_t)x;
12 /* How much info did we lose? time_t may be an integral or
13 * floating type, and we don't know which. If it's integral,
14 * we don't know whether C truncates, rounds, returns the floor,
15 * etc. If we lost a second or more, the C rounding is
16 * unreasonable, or the input just doesn't fit in a time_t;
17 * call it an error regardless. Note that the original cast to
18 * time_t can cause a C error too, but nothing we can do to
Alexander Belopolsky3fe3e122010-06-18 16:22:00 +000019 * work around that.
Alexander Belopolskyd95a5862010-06-16 22:38:15 +000020 */
21 diff = x - (double)result;
22 if (diff <= -1.0 || diff >= 1.0) {
23 PyErr_SetString(PyExc_ValueError,
24 "timestamp out of range for platform time_t");
25 result = (time_t)-1;
26 }
27 return result;
28}