summaryrefslogtreecommitdiffstats
path: root/cython/plist_util.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2025-05-13 18:34:32 +0200
committerGravatar Nikias Bassen2025-05-13 18:34:32 +0200
commitcb76e4da84c61609c13e84c922f14a27b8348a36 (patch)
tree6f0057efd11780ec38da6e8b686726f51bd10f30 /cython/plist_util.c
parentd7fe479707af57aeedf7e41c08e7fb698cd2e2a3 (diff)
downloadlibplist-cb76e4da84c61609c13e84c922f14a27b8348a36.tar.gz
libplist-cb76e4da84c61609c13e84c922f14a27b8348a36.tar.bz2
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.
Diffstat (limited to 'cython/plist_util.c')
-rw-r--r--cython/plist_util.c31
1 files changed, 14 insertions, 17 deletions
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 @@
3#include <time.h> 3#include <time.h>
4#include <datetime.h> 4#include <datetime.h>
5 5
6void datetime_to_ints(PyObject* obj, int32_t* sec, int32_t* usec) { 6int64_t datetime_to_timestamp(PyObject* obj) {
7 PyDateTime_IMPORT; 7 PyDateTime_IMPORT;
8 if (!PyDateTime_Check(obj)) { 8 if (!PyDateTime_Check(obj)) {
9 PyErr_SetString(PyExc_ValueError,"Expected a datetime"); 9 PyErr_SetString(PyExc_ValueError,"Expected a datetime");
10 sec = NULL; 10 return 0;
11 usec = NULL;
12 return;
13 } 11 }
14 struct tm t; 12 struct tm t;
15 memset(&t, 0, sizeof(t)); 13 memset(&t, 0, sizeof(t));
@@ -19,22 +17,21 @@ void datetime_to_ints(PyObject* obj, int32_t* sec, int32_t* usec) {
19 t.tm_mday = PyDateTime_GET_DAY(obj); 17 t.tm_mday = PyDateTime_GET_DAY(obj);
20 t.tm_mon = PyDateTime_GET_MONTH(obj)-1; 18 t.tm_mon = PyDateTime_GET_MONTH(obj)-1;
21 t.tm_year = PyDateTime_GET_YEAR(obj)-1900; 19 t.tm_year = PyDateTime_GET_YEAR(obj)-1900;
22 *sec = (int32_t)mktime(&t); 20 return mktime(&t);
23 *usec = PyDateTime_DATE_GET_MICROSECOND(obj);
24} 21}
25PyObject* ints_to_datetime(int32_t sec, int32_t usec) { 22PyObject* timestamp_to_datetime(int64_t sec) {
26 time_t sec_tt = sec; 23 time_t sec_tt = sec;
27 struct tm* t = gmtime(&sec_tt); 24 struct tm* t = gmtime(&sec_tt);
28 if(t){ 25 if(t){
29 PyDateTime_IMPORT; 26 PyDateTime_IMPORT;
30 return PyDateTime_FromDateAndTime(t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, usec); 27 return PyDateTime_FromDateAndTime(t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, 0);
31 } 28 }
32 return NULL; 29 return NULL;
33} 30}
34int check_datetime(PyObject* ob) { 31int check_datetime(PyObject* ob) {
35 if(ob){ 32 if(ob){
36 PyDateTime_IMPORT; 33 PyDateTime_IMPORT;
37 return PyDateTime_Check(ob); 34 return PyDateTime_Check(ob);
38 } 35 }
39 return 0; 36 return 0;
40} 37}