summaryrefslogtreecommitdiffstats
path: root/cython
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
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')
-rw-r--r--cython/plist.pyx48
-rw-r--r--cython/plist_util.c31
-rw-r--r--cython/plist_util.h4
3 files changed, 40 insertions, 43 deletions
diff --git a/cython/plist.pyx b/cython/plist.pyx
index 4d1f8aa..29c1181 100644
--- a/cython/plist.pyx
+++ b/cython/plist.pyx
@@ -33,9 +33,9 @@ cdef extern from *:
33 void plist_get_real_val(plist_t node, double *val) 33 void plist_get_real_val(plist_t node, double *val)
34 void plist_set_real_val(plist_t node, double val) 34 void plist_set_real_val(plist_t node, double val)
35 35
36 plist_t plist_new_date(int32_t sec, int32_t usec) 36 plist_t plist_new_unix_date(int64_t sec)
37 void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec) 37 void plist_get_unix_date_val(plist_t node, int64_t *sec)
38 void plist_set_date_val(plist_t node, int32_t sec, int32_t usec) 38 void plist_set_unix_date_val(plist_t node, int64_t sec)
39 39
40 void plist_get_key_val(plist_t node, char **val) 40 void plist_get_key_val(plist_t node, char **val)
41 void plist_set_key_val(plist_t node, char *val) 41 void plist_set_key_val(plist_t node, char *val)
@@ -488,23 +488,21 @@ cdef String String_factory(plist_t c_node, bint managed=True):
488 instance._c_node = c_node 488 instance._c_node = c_node
489 return instance 489 return instance
490 490
491MAC_EPOCH = 978307200
492
493cdef extern from "plist_util.h": 491cdef extern from "plist_util.h":
494 void datetime_to_ints(object obj, int32_t* sec, int32_t* usec) 492 int64_t datetime_to_timestamp(object obj)
495 object ints_to_datetime(int32_t sec, int32_t usec) 493 object timestamp_to_datetime(int64_t sec)
496 int check_datetime(object obj) 494 int check_datetime(object obj)
497 495
498cdef plist_t create_date_plist(value=None): 496cdef plist_t create_date_plist(value=None):
499 cdef plist_t node = NULL 497 cdef plist_t node = NULL
500 cdef int32_t secs
501 cdef int32_t usecs
502 if value is None: 498 if value is None:
503 node = plist_new_date(0, 0) 499 node = plist_new_unix_date(0)
500 elif isinstance(value, int):
501 node = plist_new_unix_date(value)
502 elif isinstance(value, float):
503 node = plist_new_unix_date(int(value))
504 elif check_datetime(value): 504 elif check_datetime(value):
505 datetime_to_ints(value, &secs, &usecs) 505 node = plist_new_unix_date(datetime_to_timestamp(value))
506 secs -= MAC_EPOCH
507 node = plist_new_date(secs, usecs)
508 return node 506 return node
509 507
510cdef class Date(Node): 508cdef class Date(Node):
@@ -531,19 +529,21 @@ cdef class Date(Node):
531 return d >= other 529 return d >= other
532 530
533 cpdef object get_value(self): 531 cpdef object get_value(self):
534 cdef int32_t secs = 0 532 cdef int64_t secs = 0
535 cdef int32_t usecs = 0 533 plist_get_unix_date_val(self._c_node, &secs)
536 plist_get_date_val(self._c_node, &secs, &usecs) 534 return timestamp_to_datetime(secs)
537 secs += MAC_EPOCH
538 return ints_to_datetime(secs, usecs)
539 535
540 cpdef set_value(self, object value): 536 cpdef set_value(self, object value):
541 cdef int32_t secs 537 cdef int64_t secs = 0
542 cdef int32_t usecs 538 if isinstance(value, int):
543 if not check_datetime(value): 539 secs = value
544 raise ValueError("Expected a datetime") 540 elif isinstance(value, float):
545 datetime_to_ints(value, &secs, &usecs) 541 secs = int(value)
546 plist_set_date_val(self._c_node, secs, usecs) 542 elif check_datetime(value):
543 secs = datetime_to_timestamp(value)
544 else:
545 raise ValueError("Expected int or datetime")
546 plist_set_unix_date_val(self._c_node, secs)
547 547
548cdef Date Date_factory(plist_t c_node, bint managed=True): 548cdef Date Date_factory(plist_t c_node, bint managed=True):
549 cdef Date instance = Date.__new__(Date) 549 cdef Date instance = Date.__new__(Date)
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}
diff --git a/cython/plist_util.h b/cython/plist_util.h
index fbf56b6..e0a84b5 100644
--- a/cython/plist_util.h
+++ b/cython/plist_util.h
@@ -1,5 +1,5 @@
1#include <Python.h> 1#include <Python.h>
2 2
3void datetime_to_ints(PyObject* obj, int32_t* sec, int32_t* usec); 3int64_t datetime_to_timestamp(PyObject* obj);
4PyObject* ints_to_datetime(int32_t sec, int32_t usec); 4PyObject* timestamp_to_datetime(int64_t sec);
5int check_datetime(PyObject* obj); 5int check_datetime(PyObject* obj);