summaryrefslogtreecommitdiffstats
path: root/swig
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2009-10-18 12:40:50 +0200
committerGravatar Jonathan Beck2009-10-18 12:40:50 +0200
commit89da417a1c3686552fa56725dca8c310d2966a6e (patch)
tree95da1f181d5f842394262fee04440f8665c34115 /swig
parentd21c7d4a9c90232bd8f444c0b620f8fe465387d5 (diff)
downloadlibplist-89da417a1c3686552fa56725dca8c310d2966a6e.tar.gz
libplist-89da417a1c3686552fa56725dca8c310d2966a6e.tar.bz2
Add python datetime typemaps.
Diffstat (limited to 'swig')
-rw-r--r--swig/plist.i38
1 files changed, 38 insertions, 0 deletions
diff --git a/swig/plist.i b/swig/plist.i
index 41931b0..0a40cf6 100644
--- a/swig/plist.i
+++ b/swig/plist.i
@@ -6,6 +6,10 @@
#include <plist/plist.h>
#include <plist/plist++.h>
+#include <ctime>
+//for datetime in python
+#include <datetime.h>
+
typedef struct {
plist_t node;
char should_keep_plist;
@@ -41,6 +45,40 @@ PListNode *allocate_plist_wrapper(plist_t plist, char should_keep_plist) {
$1 = std::vector<char>(buffer, buffer + length);
}
+%typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) timeval {
+ PyDateTime_IMPORT;
+ $1 = PyDateTime_Check($input) ? 1 : 0;
+}
+
+%typemap(out) timeval {
+ struct tm* t = gmtime ( &$1.tv_sec );
+ if (t)
+ {
+ PyDateTime_IMPORT;
+ $result = PyDateTime_FromDateAndTime(t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, $1.tv_usec);
+ }
+}
+
+%typemap(in) (timeval t)
+{
+ PyDateTime_IMPORT;
+ if (!PyDateTime_Check($input)) {
+ PyErr_SetString(PyExc_ValueError,"Expected a datetime");
+ return NULL;
+ }
+ struct tm t = {
+ PyDateTime_DATE_GET_SECOND($input),
+ PyDateTime_DATE_GET_MINUTE($input),
+ PyDateTime_DATE_GET_HOUR($input),
+ PyDateTime_GET_DAY($input),
+ PyDateTime_GET_MONTH($input)-1,
+ PyDateTime_GET_YEAR($input)-1900,
+ 0,0,0
+ };
+ timeval ret = {(int)mktime(&t), PyDateTime_DATE_GET_MICROSECOND($input)};
+ $1 = ret;
+}
+
%apply SWIGTYPE *DYNAMIC { PList::Node* };
%apply SWIGTYPE *DYNAMIC { PList::Structure* };