summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cython/plist.pyx48
-rw-r--r--cython/plist_util.c31
-rw-r--r--cython/plist_util.h4
-rw-r--r--include/plist/Date.h6
-rw-r--r--include/plist/plist.h84
-rw-r--r--src/Date.cpp22
-rw-r--r--src/Node.cpp2
-rw-r--r--src/plist.c49
8 files changed, 174 insertions, 72 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 *:
void plist_get_real_val(plist_t node, double *val)
void plist_set_real_val(plist_t node, double val)
- plist_t plist_new_date(int32_t sec, int32_t usec)
- void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec)
- void plist_set_date_val(plist_t node, int32_t sec, int32_t usec)
+ plist_t plist_new_unix_date(int64_t sec)
+ void plist_get_unix_date_val(plist_t node, int64_t *sec)
+ void plist_set_unix_date_val(plist_t node, int64_t sec)
void plist_get_key_val(plist_t node, char **val)
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):
instance._c_node = c_node
return instance
-MAC_EPOCH = 978307200
-
cdef extern from "plist_util.h":
- void datetime_to_ints(object obj, int32_t* sec, int32_t* usec)
- object ints_to_datetime(int32_t sec, int32_t usec)
+ int64_t datetime_to_timestamp(object obj)
+ object timestamp_to_datetime(int64_t sec)
int check_datetime(object obj)
cdef plist_t create_date_plist(value=None):
cdef plist_t node = NULL
- cdef int32_t secs
- cdef int32_t usecs
if value is None:
- node = plist_new_date(0, 0)
+ node = plist_new_unix_date(0)
+ elif isinstance(value, int):
+ node = plist_new_unix_date(value)
+ elif isinstance(value, float):
+ node = plist_new_unix_date(int(value))
elif check_datetime(value):
- datetime_to_ints(value, &secs, &usecs)
- secs -= MAC_EPOCH
- node = plist_new_date(secs, usecs)
+ node = plist_new_unix_date(datetime_to_timestamp(value))
return node
cdef class Date(Node):
@@ -531,19 +529,21 @@ cdef class Date(Node):
return d >= other
cpdef object get_value(self):
- cdef int32_t secs = 0
- cdef int32_t usecs = 0
- plist_get_date_val(self._c_node, &secs, &usecs)
- secs += MAC_EPOCH
- return ints_to_datetime(secs, usecs)
+ cdef int64_t secs = 0
+ plist_get_unix_date_val(self._c_node, &secs)
+ return timestamp_to_datetime(secs)
cpdef set_value(self, object value):
- cdef int32_t secs
- cdef int32_t usecs
- if not check_datetime(value):
- raise ValueError("Expected a datetime")
- datetime_to_ints(value, &secs, &usecs)
- plist_set_date_val(self._c_node, secs, usecs)
+ cdef int64_t secs = 0
+ if isinstance(value, int):
+ secs = value
+ elif isinstance(value, float):
+ secs = int(value)
+ elif check_datetime(value):
+ secs = datetime_to_timestamp(value)
+ else:
+ raise ValueError("Expected int or datetime")
+ plist_set_unix_date_val(self._c_node, secs)
cdef Date Date_factory(plist_t c_node, bint managed=True):
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 @@
#include <time.h>
#include <datetime.h>
-void datetime_to_ints(PyObject* obj, int32_t* sec, int32_t* usec) {
+int64_t datetime_to_timestamp(PyObject* obj) {
PyDateTime_IMPORT;
if (!PyDateTime_Check(obj)) {
- PyErr_SetString(PyExc_ValueError,"Expected a datetime");
- sec = NULL;
- usec = NULL;
- return;
+ PyErr_SetString(PyExc_ValueError,"Expected a datetime");
+ return 0;
}
struct tm t;
memset(&t, 0, sizeof(t));
@@ -19,22 +17,21 @@ void datetime_to_ints(PyObject* obj, int32_t* sec, int32_t* usec) {
t.tm_mday = PyDateTime_GET_DAY(obj);
t.tm_mon = PyDateTime_GET_MONTH(obj)-1;
t.tm_year = PyDateTime_GET_YEAR(obj)-1900;
- *sec = (int32_t)mktime(&t);
- *usec = PyDateTime_DATE_GET_MICROSECOND(obj);
+ return mktime(&t);
}
-PyObject* ints_to_datetime(int32_t sec, int32_t usec) {
- time_t sec_tt = sec;
+PyObject* timestamp_to_datetime(int64_t sec) {
+ time_t sec_tt = sec;
struct tm* t = gmtime(&sec_tt);
if(t){
- PyDateTime_IMPORT;
- return PyDateTime_FromDateAndTime(t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, usec);
+ PyDateTime_IMPORT;
+ return PyDateTime_FromDateAndTime(t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, 0);
}
- return NULL;
+ return NULL;
}
int check_datetime(PyObject* ob) {
- if(ob){
- PyDateTime_IMPORT;
- return PyDateTime_Check(ob);
- }
- return 0;
+ if(ob){
+ PyDateTime_IMPORT;
+ return PyDateTime_Check(ob);
+ }
+ return 0;
}
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 @@
#include <Python.h>
-void datetime_to_ints(PyObject* obj, int32_t* sec, int32_t* usec);
-PyObject* ints_to_datetime(int32_t sec, int32_t usec);
+int64_t datetime_to_timestamp(PyObject* obj);
+PyObject* timestamp_to_datetime(int64_t sec);
int check_datetime(PyObject* obj);
diff --git a/include/plist/Date.h b/include/plist/Date.h
index 5113cf3..7026699 100644
--- a/include/plist/Date.h
+++ b/include/plist/Date.h
@@ -40,13 +40,13 @@ public :
Date(plist_t node, Node* parent = NULL);
Date(const Date& d);
Date& operator=(const Date& d);
- Date(timeval t);
+ Date(int64_t t);
virtual ~Date();
Node* Clone() const;
- void SetValue(timeval t);
- timeval GetValue() const;
+ void SetValue(int64_t t);
+ int64_t GetValue() const;
};
};
diff --git a/include/plist/plist.h b/include/plist/plist.h
index aff81e9..41af8ce 100644
--- a/include/plist/plist.h
+++ b/include/plist/plist.h
@@ -263,12 +263,11 @@ extern "C"
/**
* Create a new plist_t type #PLIST_DATE
*
- * @param sec the number of seconds since 01/01/2001
- * @param usec the number of microseconds
+ * @param sec The number of seconds since 01/01/1970 (UNIX timestamp)
* @return the created item
* @sa #plist_type
*/
- PLIST_API plist_t plist_new_date(int32_t sec, int32_t usec);
+ PLIST_API plist_t plist_new_unix_date(int64_t sec);
/**
* Create a new plist_t type #PLIST_UID
@@ -775,10 +774,9 @@ extern "C"
* This function does nothing if node is not of type #PLIST_DATE
*
* @param node the node
- * @param sec a pointer to an int32_t variable. Represents the number of seconds since 01/01/2001.
- * @param usec a pointer to an int32_t variable. Represents the number of microseconds
+ * @param sec a pointer to an int64_t variable. Represents the number of seconds since 01/01/1970 (UNIX timestamp).
*/
- PLIST_API void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec);
+ PLIST_API void plist_get_unix_date_val(plist_t node, int64_t *sec);
/**
* Get the value of a #PLIST_UID node.
@@ -867,10 +865,9 @@ extern "C"
* Forces type of node to #PLIST_DATE
*
* @param node the node
- * @param sec the number of seconds since 01/01/2001
- * @param usec the number of microseconds
+ * @param sec the number of seconds since 01/01/1970 (UNIX timestamp)
*/
- PLIST_API void plist_set_date_val(plist_t node, int32_t sec, int32_t usec);
+ PLIST_API void plist_set_unix_date_val(plist_t node, int64_t sec);
/**
* Set the value of a node.
@@ -1213,16 +1210,15 @@ extern "C"
/**
* Helper function to compare the value of a PLIST_DATE node against
- * a given set of seconds and fraction of a second since epoch.
+ * a given number of seconds since epoch (UNIX timestamp).
*
* @param datenode node of type PLIST_DATE
- * @param cmpsec number of seconds since epoch to compare against
- * @param cmpusec fraction of a second in microseconds to compare against
+ * @param cmpval Number of seconds to compare against (UNIX timestamp)
* @return 0 if the node's date is equal to the supplied values,
* 1 if the node's date is greater than the supplied values,
* or -1 if the node's date is less than the supplied values.
*/
- PLIST_API int plist_date_val_compare(plist_t datenode, int32_t cmpsec, int32_t cmpusec);
+ PLIST_API int plist_unix_date_val_compare(plist_t datenode, int64_t cmpval);
/**
* Helper function to compare the value of a PLIST_STRING node against
@@ -1382,6 +1378,68 @@ extern "C"
*/
PLIST_API const char* libplist_version();
+
+ /********************************************
+ * *
+ * Deprecated API *
+ * *
+ ********************************************/
+
+ /**
+ * Create a new plist_t type #PLIST_DATE
+ *
+ * @deprecated Deprecated. Use plist_new_unix_date instead.
+ *
+ * @param sec the number of seconds since 01/01/2001
+ * @param usec the number of microseconds
+ * @return the created item
+ * @sa #plist_type
+ */
+ PLIST_WARN_DEPRECATED("use plist_new_unix_date instead")
+ PLIST_API plist_t plist_new_date(int32_t sec, int32_t usec);
+
+ /**
+ * Get the value of a #PLIST_DATE node.
+ * This function does nothing if node is not of type #PLIST_DATE
+ *
+ * @deprecated Deprecated. Use plist_get_unix_date_val instead.
+ *
+ * @param node the node
+ * @param sec a pointer to an int32_t variable. Represents the number of seconds since 01/01/2001.
+ * @param usec a pointer to an int32_t variable. Represents the number of microseconds
+ */
+ PLIST_WARN_DEPRECATED("use plist_get_unix_date_val instead")
+ PLIST_API void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec);
+
+ /**
+ * Set the value of a node.
+ * Forces type of node to #PLIST_DATE
+ *
+ * @deprecated Deprecated. Use plist_set_unix_date_val instead.
+ *
+ * @param node the node
+ * @param sec the number of seconds since 01/01/2001
+ * @param usec the number of microseconds
+ */
+ PLIST_WARN_DEPRECATED("use plist_set_unix_date_val instead")
+ PLIST_API void plist_set_date_val(plist_t node, int32_t sec, int32_t usec);
+
+ /**
+ * Helper function to compare the value of a PLIST_DATE node against
+ * a given set of seconds and fraction of a second since epoch.
+ *
+ * @deprecated Deprecated. Use plist_unix_date_val_compare instead.
+ *
+ * @param datenode node of type PLIST_DATE
+ * @param cmpsec number of seconds since epoch to compare against
+ * @param cmpusec fraction of a second in microseconds to compare against
+ * @return 0 if the node's date is equal to the supplied values,
+ * 1 if the node's date is greater than the supplied values,
+ * or -1 if the node's date is less than the supplied values.
+ */
+ PLIST_WARN_DEPRECATED("use plist_unix_date_val_compare instead")
+ PLIST_API int plist_date_val_compare(plist_t datenode, int32_t cmpsec, int32_t cmpusec);
+
/*@}*/
#ifdef __cplusplus
diff --git a/src/Date.cpp b/src/Date.cpp
index 8b8e650..cbfa123 100644
--- a/src/Date.cpp
+++ b/src/Date.cpp
@@ -34,8 +34,8 @@ Date::Date(plist_t node, Node* parent) : Node(node, parent)
Date::Date(const PList::Date& d) : Node(PLIST_DATE)
{
- timeval t = d.GetValue();
- plist_set_date_val(_node, t.tv_sec, t.tv_usec);
+ int64_t t = d.GetValue();
+ plist_set_unix_date_val(_node, t);
}
Date& Date::operator=(const PList::Date& d)
@@ -45,9 +45,9 @@ Date& Date::operator=(const PList::Date& d)
return *this;
}
-Date::Date(timeval t) : Node(PLIST_DATE)
+Date::Date(int64_t t) : Node(PLIST_DATE)
{
- plist_set_date_val(_node, t.tv_sec, t.tv_usec);
+ plist_set_unix_date_val(_node, t);
}
Date::~Date()
@@ -59,18 +59,16 @@ Node* Date::Clone() const
return new Date(*this);
}
-void Date::SetValue(timeval t)
+void Date::SetValue(int64_t t)
{
- plist_set_date_val(_node, t.tv_sec, t.tv_usec);
+ plist_set_unix_date_val(_node, t);
}
-timeval Date::GetValue() const
+int64_t Date::GetValue() const
{
- int32_t tv_sec = 0;
- int32_t tv_usec = 0;
- plist_get_date_val(_node, &tv_sec, &tv_usec);
- timeval t = {tv_sec, tv_usec};
- return t;
+ int64_t sec = 0;
+ plist_get_unix_date_val(_node, &sec);
+ return sec;
}
} // namespace PList
diff --git a/src/Node.cpp b/src/Node.cpp
index 0bd428a..1043b31 100644
--- a/src/Node.cpp
+++ b/src/Node.cpp
@@ -73,7 +73,7 @@ Node::Node(plist_type type, Node* parent) : _parent(parent)
_node = plist_new_data(NULL,0);
break;
case PLIST_DATE:
- _node = plist_new_date(0,0);
+ _node = plist_new_unix_date(0);
break;
case PLIST_ARRAY:
_node = plist_new_array();
diff --git a/src/plist.c b/src/plist.c
index 3f86105..4fc2f00 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -46,6 +46,8 @@
#include <hashtable.h>
#include <ptrarray.h>
+#define MAC_EPOCH 978307200
+
#ifdef _MSC_VER
typedef SSIZE_T ssize_t;
#endif
@@ -528,6 +530,15 @@ plist_t plist_new_date(int32_t sec, int32_t usec)
return plist_new_node(data);
}
+plist_t plist_new_unix_date(int64_t sec)
+{
+ plist_data_t data = plist_new_plist_data();
+ data->type = PLIST_DATE;
+ data->realval = (double)sec - MAC_EPOCH;
+ data->length = sizeof(double);
+ return plist_new_node(data);
+}
+
plist_t plist_new_null(void)
{
plist_data_t data = plist_new_plist_data();
@@ -1392,6 +1403,20 @@ void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec)
}
}
+void plist_get_unix_date_val(plist_t node, int64_t *sec)
+{
+ if (!node || !sec)
+ return;
+ plist_type type = plist_get_node_type(node);
+ uint64_t length = 0;
+ double val = 0;
+ if (PLIST_DATE != type)
+ return;
+ plist_get_type_and_value(node, &type, (void *) &val, &length);
+ assert(length == sizeof(double));
+ *sec = (int64_t)val + MAC_EPOCH;
+}
+
int plist_data_compare(const void *a, const void *b)
{
plist_data_t val_a = NULL;
@@ -1551,6 +1576,12 @@ void plist_set_date_val(plist_t node, int32_t sec, int32_t usec)
plist_set_element_val(node, PLIST_DATE, &val, sizeof(double));
}
+void plist_set_unix_date_val(plist_t node, int64_t sec)
+{
+ double val = (double)(sec - MAC_EPOCH);
+ plist_set_element_val(node, PLIST_DATE, &val, sizeof(double));
+}
+
int plist_bool_val_is_true(plist_t boolnode)
{
if (!PLIST_IS_BOOLEAN(boolnode)) {
@@ -1686,6 +1717,24 @@ int plist_date_val_compare(plist_t datenode, int32_t cmpsec, int32_t cmpusec)
return 1;
}
+int plist_unix_date_val_compare(plist_t datenode, int64_t cmpval)
+{
+ if (!PLIST_IS_DATE(datenode)) {
+ return -1;
+ }
+ int64_t dateval = 0;
+ plist_get_unix_date_val(datenode, &dateval);
+ if (dateval == cmpval) {
+ return 0;
+ }
+
+ if (dateval < cmpval) {
+ return -1;
+ }
+
+ return 1;
+}
+
int plist_string_val_compare(plist_t strnode, const char* cmpval)
{
if (!PLIST_IS_STRING(strnode)) {