summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2012-03-30 10:39:55 (GMT)
committerChristian Mollekopf <chrigi_1@fastmail.fm>2012-03-30 10:39:55 (GMT)
commita8069183b064e0bf014e4847ec2e39bf3ed168a5 (patch)
treed9350dd707b56155f80a5dc9af6edf3e1dc2861a
parent117d3282db66581429babf51c9af906210d86d93 (diff)
downloadlibkolabxml-a8069183b064e0bf014e4847ec2e39bf3ed168a5.tar.gz
Make it possible to override the system timestamp (needed for testing).
-rw-r--r--src/kolabconversions.h4
-rw-r--r--src/kolabformat.cpp5
-rw-r--r--src/kolabformat.h12
-rw-r--r--src/utils.cpp19
-rw-r--r--src/utils.h11
-rw-r--r--src/xcalconversions.h4
-rw-r--r--src/xcardconversions.h2
7 files changed, 49 insertions, 8 deletions
diff --git a/src/kolabconversions.h b/src/kolabconversions.h
index a1d7cb9..056839a 100644
--- a/src/kolabconversions.h
+++ b/src/kolabconversions.h
@@ -52,10 +52,10 @@ std::string serializeObject(const Kolab::Note &note, const std::string prod = st
KolabXSD::Note::creation_date_type created = fromDateTime(note.created());
if (!note.created().isValid()) {
- created = fromDateTime(getCurrentTime());
+ created = fromDateTime(timestamp());
}
- KolabXSD::Note n(getUID(note.uid()), prod+KOLAB_LIB_VERSION_STRING, created, fromDateTime(getCurrentTime()));
+ KolabXSD::Note n(getUID(note.uid()), prod+KOLAB_LIB_VERSION_STRING, created, fromDateTime(timestamp()));
if (!note.categories().empty()) {
KolabXSD::Note::categories_sequence categories;
diff --git a/src/kolabformat.cpp b/src/kolabformat.cpp
index 98a42a2..466d4be 100644
--- a/src/kolabformat.cpp
+++ b/src/kolabformat.cpp
@@ -56,6 +56,11 @@ std::string getSerializedUID()
return Utils::createdUid();
}
+void overrideTimestamp(const cDateTime& dt)
+{
+ Utils::setOverrideTimestamp(dt);
+}
+
Kolab::Event readEvent(const std::string& s, bool isUrl)
{
Kolab::XCAL::IncidenceTrait <Kolab::Event >::IncidencePtr ptr = XCAL::deserializeIncidence< XCAL::IncidenceTrait<Kolab::Event> >(s, isUrl);
diff --git a/src/kolabformat.h b/src/kolabformat.h
index 090650c..991bea3 100644
--- a/src/kolabformat.h
+++ b/src/kolabformat.h
@@ -57,6 +57,18 @@ std::string xCalVersion();
*/
std::string getSerializedUID();
+/**
+ * Use this function to override the timestamp which is normally generated upon serialization from the system time.
+ * To override the timestamp call this function once. You will need to clear the timestamp manually by setting a default constructed cDateTime().
+ *
+ * The timestamp is used as lastModifiedDate on every write, and as creation-date on the first write (when there is no creation-date yet).
+ *
+ * The supplied timestamp must be in UTC format.
+ *
+ * This function exists primarily for testing purpose.
+ */
+void overrideTimestamp(const Kolab::cDateTime &dt);
+
Kolab::Event readEvent(const std::string& s, bool isUrl);
std::string writeEvent(const Kolab::Event &);
diff --git a/src/utils.cpp b/src/utils.cpp
index 164fd25..c4fde94 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -48,6 +48,7 @@ struct Global {
ErrorSeverity errorBit;
std::string errorMessage;
+ cDateTime overrideTimestamp;
};
boost::thread_specific_ptr<Global> ptr;
@@ -106,7 +107,7 @@ std::string getUID(const std::string &s)
boost::uuids::uuid u; // initialize uuid
return boost::uuids::to_string(u);
#else
- uuid_t *uuid;
+ uuid_t *uuid;
char *str = 0;
uuid_create(&uuid);
uuid_make(uuid, UUID_MAKE_V1);
@@ -128,6 +129,22 @@ cDateTime getCurrentTime()
return cDateTime(ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, true);
}
+void setOverrideTimestamp(const cDateTime &dt)
+{
+ ThreadLocal::inst().overrideTimestamp = dt;
+}
+
+cDateTime timestamp()
+{
+ const cDateTime &dt = ThreadLocal::inst().overrideTimestamp;
+ if (dt.isValid()) {
+ LOG("Timestamp overridden");
+ return dt;
+ }
+ return getCurrentTime();
+}
+
+
void logMessage(const std::string &m, ErrorSeverity s)
{
diff --git a/src/utils.h b/src/utils.h
index 15bf376..fe84d1e 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -31,8 +31,6 @@ namespace Kolab {
*/
std::string getUID(const std::string & = std::string());
-cDateTime getCurrentTime();
-
void logMessage(const std::string &,const std::string &, int, ErrorSeverity s);
#define LOG(message) logMessage(message,__FILE__, __LINE__, NoError);
@@ -74,6 +72,15 @@ void setKolabVersion(const std::string &);
std::string kolabVersion();
/**
+ * A timestamp which overrides the one normally used.
+ */
+void setOverrideTimestamp(const cDateTime &);
+/**
+ * From system time or overrideTimestamp if set.
+ */
+cDateTime timestamp();
+
+/**
* Helper functions for save conversion of integer types (so we can catch overflows)
*/
diff --git a/src/xcalconversions.h b/src/xcalconversions.h
index c4dc4ad..a4e1b46 100644
--- a/src/xcalconversions.h
+++ b/src/xcalconversions.h
@@ -1365,14 +1365,14 @@ template <typename T> struct IncidenceConverter < Incidence, T >
}
static xml_schema::date_time dtstamp() {
- return fromDateTime(getCurrentTime());
+ return fromDateTime(timestamp());
}
static xml_schema::date_time created(const T &inc) {
if (inc.created().isValid()) {
return fromDateTime(inc.created());
}
- return fromDateTime(getCurrentTime());
+ return fromDateTime(timestamp());
}
};
diff --git a/src/xcardconversions.h b/src/xcardconversions.h
index 22e6e54..5dd956c 100644
--- a/src/xcardconversions.h
+++ b/src/xcardconversions.h
@@ -722,7 +722,7 @@ std::string serializeCard(const T &card, const std::string prod = std::string())
setCreatedUid(Shared::fromURN(uid.uri()));
vcard_4_0::vcard::x_kolab_version_type kolab_version(KOLAB_FORMAT_VERSION);
vcard_4_0::vcard::prodid_type prodid(prod+KOLAB_LIB_VERSION_STRING);
- vcard_4_0::vcard::rev_type rev(fromDateTime(getCurrentTime()));
+ vcard_4_0::vcard::rev_type rev(fromDateTime(timestamp()));
vcard_4_0::vcard::kind_type kind(getType<T>());
vcard_4_0::vcard::fn_type fn(card.name());