diff options
Diffstat (limited to 'cython/plist_util.c')
| -rw-r--r-- | cython/plist_util.c | 48 |
1 files changed, 22 insertions, 26 deletions
diff --git a/cython/plist_util.c b/cython/plist_util.c index 70c5be3..27084fa 100644 --- a/cython/plist_util.c +++ b/cython/plist_util.c @@ -3,39 +3,35 @@ #include <time.h> #include <datetime.h> -void datetime_to_ints(PyObject* obj, int32_t* sec, int32_t* usec) { +int64_t datetime_to_timestamp(PyObject* obj) { PyDateTime_IMPORT; if (!PyDateTime_Check(obj)) { - PyErr_SetString(PyExc_ValueError,"Expected a datetime"); - sec = NULL; - usec = NULL; - return; + PyErr_SetString(PyExc_ValueError,"Expected a datetime"); + return 0; } - struct tm t = { - PyDateTime_DATE_GET_SECOND(obj), - PyDateTime_DATE_GET_MINUTE(obj), - PyDateTime_DATE_GET_HOUR(obj), - PyDateTime_GET_DAY(obj), - PyDateTime_GET_MONTH(obj)-1, - PyDateTime_GET_YEAR(obj)-1900, - 0,0,0 - }; - *sec = (int32_t)mktime(&t); - *usec = PyDateTime_DATE_GET_MICROSECOND(obj); + struct tm t; + memset(&t, 0, sizeof(t)); + t.tm_sec = PyDateTime_DATE_GET_SECOND(obj); + t.tm_min = PyDateTime_DATE_GET_MINUTE(obj); + t.tm_hour = PyDateTime_DATE_GET_HOUR(obj); + t.tm_mday = PyDateTime_GET_DAY(obj); + t.tm_mon = PyDateTime_GET_MONTH(obj)-1; + t.tm_year = PyDateTime_GET_YEAR(obj)-1900; + return mktime(&t); } -PyObject* ints_to_datetime(int32_t sec, int32_t usec) { - time_t sec_tt = sec; +PyObject* timestamp_to_datetime(int64_t sec) { + time_t sec_tt = sec; struct tm* t = gmtime(&sec_tt); if(t){ - PyDateTime_IMPORT; - return PyDateTime_FromDateAndTime(t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, usec); + PyDateTime_IMPORT; + return PyDateTime_FromDateAndTime(t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, 0); } - return NULL; + return NULL; } int check_datetime(PyObject* ob) { - if(ob){ - PyDateTime_IMPORT; - return PyDateTime_Check(ob); - } - return 0; + if(ob){ + PyDateTime_IMPORT; + return PyDateTime_Check(ob); + } + return 0; } |
