diff options
Diffstat (limited to 'cython/plist.pyx')
| -rw-r--r-- | cython/plist.pyx | 48 |
1 files changed, 24 insertions, 24 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 | ||
| 491 | MAC_EPOCH = 978307200 | ||
| 492 | |||
| 493 | cdef extern from "plist_util.h": | 491 | cdef 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 | ||
| 498 | cdef plist_t create_date_plist(value=None): | 496 | cdef 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 | ||
| 510 | cdef class Date(Node): | 508 | cdef 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 | ||
| 548 | cdef Date Date_factory(plist_t c_node, bint managed=True): | 548 | cdef Date Date_factory(plist_t c_node, bint managed=True): |
| 549 | cdef Date instance = Date.__new__(Date) | 549 | cdef Date instance = Date.__new__(Date) |
