# HG changeset patch
# Node ID cf3e41a34122
# Parent  472d8f2226bc
diff --git a/ibis/ibisconstants.h b/ibis/ibisconstants.h
--- a/ibis/ibisconstants.h
+++ b/ibis/ibisconstants.h
@@ -324,4 +324,13 @@
  */
 #define IBIS_RPL_YOURHOST "002" IBIS_AVAILABLE_MACRO_IN_0_1
 
+/**
+ * IBIS_TAG_SERVER_TIME:
+ *
+ * A constant for the %SERVER_TIME tag.
+ *
+ * Since: 0.1
+ */
+#define IBIS_TAG_SERVER_TIME "time" IBIS_AVAILABLE_MACRO_IN_0_1
+
 #endif /* IBIS_CONSTANTS_H */
diff --git a/ibis/ibismessage.h b/ibis/ibismessage.h
--- a/ibis/ibismessage.h
+++ b/ibis/ibismessage.h
@@ -197,6 +197,17 @@
 void ibis_message_set_tags(IbisMessage *message, IbisTags *tags);
 
 /**
+ * ibis_message_get_timestamp:
+ * @message: The instance.
+ *
+ * Gets the value of the @time tag if it exists.
+ *
+ * Returns: (transfer full) (nullable): The timestamp.
+ */
+IBIS_AVAILABLE_IN_0_1
+GDateTime *ibis_message_get_timestamp(IbisMessage *message);
+
+/**
  * ibis_message_serialize:
  * @message: The instance.
  *
diff --git a/ibis/ibismessage.c b/ibis/ibismessage.c
--- a/ibis/ibismessage.c
+++ b/ibis/ibismessage.c
@@ -485,6 +485,29 @@
 	}
 }
 
+GDateTime *
+ibis_message_get_timestamp(IbisMessage *message) {
+	GDateTime *dt = NULL;
+	const char *timestamp = NULL;
+
+	g_return_val_if_fail(IBIS_IS_MESSAGE(message), NULL);
+
+	if(!IBIS_IS_TAGS(message->tags)) {
+		return NULL;
+	}
+
+	timestamp = ibis_tags_lookup(message->tags, IBIS_TAG_SERVER_TIME);
+	if(!ibis_str_is_empty(timestamp)) {
+		GTimeZone *tz = g_time_zone_new_utc();
+
+		dt = g_date_time_new_from_iso8601(timestamp, tz);
+
+		g_time_zone_unref(tz);
+	}
+
+	return dt;
+}
+
 GStrv
 ibis_message_get_params(IbisMessage *message) {
 	g_return_val_if_fail(IBIS_IS_MESSAGE(message), NULL);
diff --git a/ibis/tests/test_message.c b/ibis/tests/test_message.c
--- a/ibis/tests/test_message.c
+++ b/ibis/tests/test_message.c
@@ -188,6 +188,78 @@
 }
 
 /******************************************************************************
+ * get_timestamp Tests
+ *****************************************************************************/
+static void
+test_ibis_message_get_timestamp_no_tags(void) {
+	IbisMessage *message = NULL;
+	GDateTime *dt = NULL;
+	GError *error = NULL;
+
+	message = ibis_message_parse("COMMAND", &error);
+	g_assert_no_error(error);
+
+	dt = ibis_message_get_timestamp(message);
+
+	g_assert_null(dt);
+
+	g_assert_finalize_object(message);
+}
+
+static void
+test_ibis_message_get_timestamp_no_time(void) {
+	IbisMessage *message = NULL;
+	GDateTime *dt = NULL;
+	GError *error = NULL;
+
+	message = ibis_message_parse("@msgid=abc123 COMMAND", &error);
+	g_assert_no_error(error);
+
+	dt = ibis_message_get_timestamp(message);
+
+	g_assert_null(dt);
+
+	g_assert_finalize_object(message);
+}
+
+static void
+test_ibis_message_get_timestamp_empty_time(void) {
+	IbisMessage *message = NULL;
+	GDateTime *dt = NULL;
+	GError *error = NULL;
+
+	message = ibis_message_parse("@time COMMAND", &error);
+	g_assert_no_error(error);
+
+	dt = ibis_message_get_timestamp(message);
+
+	g_assert_null(dt);
+
+	g_assert_finalize_object(message);
+}
+
+static void
+test_ibis_message_get_timestamp_normal(void) {
+	IbisMessage *message = NULL;
+	GDateTime *dt = NULL;
+	GError *error = NULL;
+	gint64 timestamp = 0;
+
+	message = ibis_message_parse("@time=2011-10-19T16:40:51.620Z COMMAND",
+	                             &error);
+	g_assert_no_error(error);
+
+	dt = ibis_message_get_timestamp(message);
+
+	/* Once we require glib 2.80 we should use g_date_time_to_unix_usec. */
+	timestamp = g_date_time_to_unix(dt);
+
+	g_assert_true(G_GINT64_CONSTANT(1319042451) == timestamp);
+
+	g_assert_finalize_object(message);
+}
+
+/******************************************************************************
  * Serialization Tests
  *****************************************************************************/
 static void
@@ -725,6 +797,15 @@
 	g_test_add_func("/ibis/message/set-params-variadic",
 	                test_ibis_message_set_params_variadic);
 
+	g_test_add_func("/ibis/message/get-timestamp/no_tags",
+	                test_ibis_message_get_timestamp_no_tags);
+	g_test_add_func("/ibis/message/get-timestamp/no_time",
+	                test_ibis_message_get_timestamp_no_time);
+	g_test_add_func("/ibis/message/get-timestamp/empty_time",
+	                test_ibis_message_get_timestamp_empty_time);
+	g_test_add_func("/ibis/message/get-timestamp/normal",
+	                test_ibis_message_get_timestamp_normal);
+
 	g_test_add_func("/ibis/message/serialize/empty",
 	                test_ibis_message_serialize_empty);
 	g_test_add_func("/ibis/message/serialize/source",
