You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
899 B

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