diff options
| author | 2011-09-26 17:14:39 +0200 | |
|---|---|---|
| committer | 2011-09-26 17:14:39 +0200 | |
| commit | 2ca52d65bb113e8639e732f67fec3c3223c0a444 (patch) | |
| tree | b937e0c7a6a93eca914c0571bd71c85a3f2408b4 /cython/plist_util.c | |
| parent | 36ad4384303e94b19cdf7a5ff43182efebe1b398 (diff) | |
| download | libplist-2ca52d65bb113e8639e732f67fec3c3223c0a444.tar.gz libplist-2ca52d65bb113e8639e732f67fec3c3223c0a444.tar.bz2 | |
Added cython bindings.
Diffstat (limited to 'cython/plist_util.c')
| -rw-r--r-- | cython/plist_util.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/cython/plist_util.c b/cython/plist_util.c new file mode 100644 index 0000000..70c5be3 --- /dev/null +++ b/cython/plist_util.c | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | #include "plist_util.h" | ||
| 2 | |||
| 3 | #include <time.h> | ||
| 4 | #include <datetime.h> | ||
| 5 | |||
| 6 | void datetime_to_ints(PyObject* obj, int32_t* sec, int32_t* usec) { | ||
| 7 | PyDateTime_IMPORT; | ||
| 8 | if (!PyDateTime_Check(obj)) { | ||
| 9 | PyErr_SetString(PyExc_ValueError,"Expected a datetime"); | ||
| 10 | sec = NULL; | ||
| 11 | usec = NULL; | ||
| 12 | return; | ||
| 13 | } | ||
| 14 | struct tm t = { | ||
| 15 | PyDateTime_DATE_GET_SECOND(obj), | ||
| 16 | PyDateTime_DATE_GET_MINUTE(obj), | ||
| 17 | PyDateTime_DATE_GET_HOUR(obj), | ||
| 18 | PyDateTime_GET_DAY(obj), | ||
| 19 | PyDateTime_GET_MONTH(obj)-1, | ||
| 20 | PyDateTime_GET_YEAR(obj)-1900, | ||
| 21 | 0,0,0 | ||
| 22 | }; | ||
| 23 | *sec = (int32_t)mktime(&t); | ||
| 24 | *usec = PyDateTime_DATE_GET_MICROSECOND(obj); | ||
| 25 | } | ||
| 26 | PyObject* ints_to_datetime(int32_t sec, int32_t usec) { | ||
| 27 | time_t sec_tt = sec; | ||
| 28 | struct tm* t = gmtime(&sec_tt); | ||
| 29 | if(t){ | ||
| 30 | PyDateTime_IMPORT; | ||
| 31 | return PyDateTime_FromDateAndTime(t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, usec); | ||
| 32 | } | ||
| 33 | return NULL; | ||
| 34 | } | ||
| 35 | int check_datetime(PyObject* ob) { | ||
| 36 | if(ob){ | ||
| 37 | PyDateTime_IMPORT; | ||
| 38 | return PyDateTime_Check(ob); | ||
| 39 | } | ||
| 40 | return 0; | ||
| 41 | } | ||
