diff options
| author | Nicholas Piper <nicholas@users.sourceforge.net> | 2007-08-07 22:48:06 +0000 |
|---|---|---|
| committer | Nicholas Piper <nicholas@users.sourceforge.net> | 2007-08-07 22:48:06 +0000 |
| commit | face4a2bacd4de4d8b791068015d78ce05b61064 (patch) | |
| tree | b5514079171346a5314ed80eb95e944b2f8f48c2 /bindings/python/gpod.i.in | |
| parent | 9abfb8f16c0c42adeaf157df625ad5e242280e0e (diff) | |
| download | libgpod-face4a2bacd4de4d8b791068015d78ce05b61064.tar.gz libgpod-face4a2bacd4de4d8b791068015d78ce05b61064.tar.xz libgpod-face4a2bacd4de4d8b791068015d78ce05b61064.zip | |
Try to deal with typestamps in a sane way in the Python bindings
git-svn-id: https://gtkpod.svn.sf.net/svnroot/gtkpod/libgpod/trunk@1672 f01d2545-417e-4e96-918e-98f8d0dbbcb6
Diffstat (limited to 'bindings/python/gpod.i.in')
| -rw-r--r-- | bindings/python/gpod.i.in | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/bindings/python/gpod.i.in b/bindings/python/gpod.i.in index b7da152..7e4366c 100644 --- a/bindings/python/gpod.i.in +++ b/bindings/python/gpod.i.in @@ -56,6 +56,8 @@ version = '.'.join(map(str, version_info)) #include "itdb.h" #include "itdb_device.h" #include "itdb_private.h" +#include <time.h> +#include <datetime.h> #ifdef HAVE_GDKPIXBUF #ifdef HAVE_PYGOBJECT #include <gdk-pixbuf/gdk-pixbuf.h> @@ -298,6 +300,7 @@ PyObject* sw_get_photo(GList *list, gint index) { #ifdef HAVE_GDKPIXBUF #ifdef HAVE_PYGOBJECT g_type_init (); + PyDateTime_IMPORT; init_pygobject (); #endif #endif @@ -311,8 +314,59 @@ PyObject* sw_get_photo(GList *list, gint index) { # them utf8 encoded Strings. typedef char gchar; -# treat time_t as long -typedef long time_t; +%typemap(in) time_t { + struct tm tmvalue; + PyObject* pydatetime = NULL; + if (PyDateTime_Check($input)) { + pydatetime = $input; + Py_INCREF(pydatetime); + } else if ((PyInt_Check($input)) || (PyLong_Check($input)) || (PyFloat_Check($input))) { + PyObject* pyargs; + Py_INCREF($input); + pyargs = PyTuple_Pack(1, $input); + if ((pydatetime = PyDateTime_FromTimestamp(pyargs)) == NULL) { + Py_DECREF(pyargs); + Py_DECREF($input); + SWIG_fail; + } + Py_DECREF(pyargs); + Py_DECREF($input); + } else { + PyErr_SetString(PyExc_ValueError, "$symname: Value must be a datetime.datetime, int or float"); + SWIG_fail; + } + tmvalue.tm_year = PyDateTime_GET_YEAR(pydatetime) - 1900; + tmvalue.tm_mon = PyDateTime_GET_MONTH(pydatetime) - 1; + tmvalue.tm_mday = PyDateTime_GET_DAY(pydatetime); + tmvalue.tm_hour = PyDateTime_DATE_GET_HOUR(pydatetime); + tmvalue.tm_min = PyDateTime_DATE_GET_MINUTE(pydatetime); + tmvalue.tm_sec = PyDateTime_DATE_GET_SECOND(pydatetime); + + Py_DECREF(pydatetime); + + $1 = mktime(&tmvalue); + if ($1 == -1) { + PyErr_SetString(PyExc_ValueError, "$symname: Failed to parse provided time"); + SWIG_fail; + } +} + +%typemap(out) time_t { +#ifdef 0 + /* for libgpod 0.6.x.. maybe ? */ + struct tm *tmvalue; + tmvalue = localtime(&$1); + $result = PyDateTime_FromDateAndTime(tmvalue->tm_year + 1900, + tmvalue->tm_mon + 1, + tmvalue->tm_mday, + tmvalue->tm_hour, + tmvalue->tm_min, + tmvalue->tm_sec, + 0); +#else + $result = PyLong_FromUnsignedLong((unsigned long) $1); +#endif +} %typemap(in) guint8 { unsigned long ival; |
