From cb76e4da84c61609c13e84c922f14a27b8348a36 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Tue, 13 May 2025 18:34:32 +0200 Subject: Add plist_new_unix_date, plist_get_unix_date_val, plist_set_unix_date_val functions These functions work with int64_t values representing a UNIX timestamp instead of using the 'MAC epoch'. They should be used instead of plist_new_date, plist_get_date_val, and plist_set_date_val, which are now marked deprecated and might be removed in a future version of libplist. --- cython/plist_util.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'cython/plist_util.c') diff --git a/cython/plist_util.c b/cython/plist_util.c index 4f922e5..27084fa 100644 --- a/cython/plist_util.c +++ b/cython/plist_util.c @@ -3,13 +3,11 @@ #include #include -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; memset(&t, 0, sizeof(t)); @@ -19,22 +17,21 @@ void datetime_to_ints(PyObject* obj, int32_t* sec, int32_t* usec) { t.tm_mday = PyDateTime_GET_DAY(obj); t.tm_mon = PyDateTime_GET_MONTH(obj)-1; t.tm_year = PyDateTime_GET_YEAR(obj)-1900; - *sec = (int32_t)mktime(&t); - *usec = PyDateTime_DATE_GET_MICROSECOND(obj); + 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; } -- cgit v1.1-32-gdbae