Alexander Belopolsky | d95a586 | 2010-06-16 22:38:15 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "_time.h" |
| 3 | |
| 4 | /* Exposed in timefuncs.h. */ |
| 5 | time_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 Belopolsky | 3fe3e12 | 2010-06-18 16:22:00 +0000 | [diff] [blame] | 19 | * work around that. |
Alexander Belopolsky | d95a586 | 2010-06-16 22:38:15 +0000 | [diff] [blame] | 20 | */ |
| 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 | } |