summaryrefslogtreecommitdiff
path: root/libappstream-glib
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2016-01-18 11:21:51 +0000
committerRichard Hughes <richard@hughsie.com>2016-01-22 08:15:45 +0000
commit8f4e1688529b1bdc364937004e68b165103302a0 (patch)
tree1535d692f69ae24321f81d2e89adf4255df55aaa /libappstream-glib
parent69e4f135a7332221b32096beaba063d5de072b67 (diff)
downloadappstream-glib-8f4e1688529b1bdc364937004e68b165103302a0.tar.gz
Support date=iso8601 attributes in <release> markup
Also fixes https://github.com/ximion/appstream/issues/20
Diffstat (limited to 'libappstream-glib')
-rw-r--r--libappstream-glib/as-release.c36
-rw-r--r--libappstream-glib/as-self-test.c30
2 files changed, 66 insertions, 0 deletions
diff --git a/libappstream-glib/as-release.c b/libappstream-glib/as-release.c
index 1b6adb3..d5f0737 100644
--- a/libappstream-glib/as-release.c
+++ b/libappstream-glib/as-release.c
@@ -577,6 +577,35 @@ as_release_node_insert (AsRelease *release, GNode *parent, AsNodeContext *ctx)
}
/**
+ * as_release_iso8601_to_datetime:
+ **/
+static GDateTime *
+as_release_iso8601_to_datetime (const gchar *iso_date)
+{
+ GTimeVal tv;
+ guint dmy[] = {0, 0, 0};
+
+ /* nothing set */
+ if (iso_date == NULL || iso_date[0] == '\0')
+ return NULL;
+
+ /* try to parse complete ISO8601 date */
+ if (g_strstr_len (iso_date, -1, " ") != NULL) {
+ if (g_time_val_from_iso8601 (iso_date, &tv) && tv.tv_sec != 0)
+ return g_date_time_new_from_timeval_utc (&tv);
+ }
+
+ /* g_time_val_from_iso8601() blows goats and won't
+ * accept a valid ISO8601 formatted date without a
+ * time value - try and parse this case */
+ if (sscanf (iso_date, "%u-%u-%u", &dmy[0], &dmy[1], &dmy[2]) != 3)
+ return NULL;
+
+ /* create valid object */
+ return g_date_time_new_utc (dmy[0], dmy[1], dmy[2], 0, 0, 0);
+}
+
+/**
* as_release_node_parse:
* @release: a #AsRelease instance.
* @node: a #GNode.
@@ -600,6 +629,13 @@ as_release_node_parse (AsRelease *release, GNode *node,
tmp = as_node_get_attribute (node, "timestamp");
if (tmp != NULL)
as_release_set_timestamp (release, atol (tmp));
+ tmp = as_node_get_attribute (node, "date");
+ if (tmp != NULL) {
+ g_autoptr(GDateTime) dt = NULL;
+ dt = as_release_iso8601_to_datetime (tmp);
+ if (dt != NULL)
+ as_release_set_timestamp (release, g_date_time_to_unix (dt));
+ }
tmp = as_node_get_attribute (node, "urgency");
if (tmp != NULL)
as_release_set_urgency (release, as_urgency_kind_from_string (tmp));
diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c
index 2c7b888..6bd923b 100644
--- a/libappstream-glib/as-self-test.c
+++ b/libappstream-glib/as-self-test.c
@@ -455,6 +455,35 @@ as_test_release_func (void)
}
static void
+as_test_release_date_func (void)
+{
+ GError *error = NULL;
+ AsNode *n;
+ AsNode *root;
+ const gchar *src = "<release date=\"2016-01-18\"/>";
+ gboolean ret;
+ g_autofree AsNodeContext *ctx = NULL;
+ g_autoptr(AsRelease) release = NULL;
+
+ release = as_release_new ();
+
+ /* to object */
+ root = as_node_from_xml (src, 0, &error);
+ g_assert_no_error (error);
+ g_assert (root != NULL);
+ n = as_node_find (root, "release");
+ g_assert (n != NULL);
+ ctx = as_node_context_new ();
+ ret = as_release_node_parse (release, n, ctx, &error);
+ g_assert_no_error (error);
+ g_assert (ret);
+ as_node_unref (root);
+
+ /* verify */
+ g_assert_cmpint (as_release_get_timestamp (release), ==, 1453075200);
+}
+
+static void
as_test_provide_func (void)
{
GError *error = NULL;
@@ -4469,6 +4498,7 @@ main (int argc, char **argv)
g_test_add_func ("/AppStream/provide", as_test_provide_func);
g_test_add_func ("/AppStream/checksum", as_test_checksum_func);
g_test_add_func ("/AppStream/release", as_test_release_func);
+ g_test_add_func ("/AppStream/release{date}", as_test_release_date_func);
g_test_add_func ("/AppStream/release{appdata}", as_test_release_appdata_func);
g_test_add_func ("/AppStream/release{appstream}", as_test_release_appstream_func);
g_test_add_func ("/AppStream/icon", as_test_icon_func);