[grilo] core: Improve grl_date_time_from_iso8601()



commit 5fd04c7ef80be58626c2cf1e0c670e82dabd84ae
Author: Juan A. Suarez Romero <jasuarez igalia com>
Date:   Sat Mar 23 22:55:30 2013 +0000

    core: Improve grl_date_time_from_iso8601()
    
    Handle more date precisions: YYYY, YYYY-MM, YYYY-MM-DD, YYYYMMDD.

 src/grl-util.c |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)
---
diff --git a/src/grl-util.c b/src/grl-util.c
index a05e1aa..94935d2 100644
--- a/src/grl-util.c
+++ b/src/grl-util.c
@@ -27,6 +27,8 @@
 
 #include "grl-util.h"
 
+#include <string.h>
+
 /**
  * grl_paging_translate:
  * @skip: number of elements to skip
@@ -134,6 +136,8 @@ grl_date_time_from_iso8601 (const gchar *date)
 {
   GTimeVal t = { 0, };
   gboolean ret;
+  gchar *date_time;
+  gint date_length;
 
   ret = g_time_val_from_iso8601 (date, &t);
 
@@ -142,7 +146,19 @@ grl_date_time_from_iso8601 (const gchar *date)
   if (!ret || (t.tv_sec == 0 && t.tv_usec == 0)) {
     /* We might be in the case where there is a date alone. In that case, we
      * take the convention of setting it to noon GMT */
-    gchar *date_time = g_strdup_printf ("%sT12:00:00Z", date);
+
+    /* Date can could be YYYY, YYYY-MM, YYYY-MM-DD or YYYYMMDD */
+    date_length = strlen (date);
+    switch (date_length) {
+    case 4:
+      date_time = g_strdup_printf ("%s-01-01T12:00:00Z", date);
+      break;
+    case 7:
+      date_time = g_strdup_printf ("%s-01T12:00:00Z", date);
+      break;
+    default:
+      date_time = g_strdup_printf ("%sT12:00:00Z", date);
+    }
     ret = g_time_val_from_iso8601 (date_time, &t);
     g_free (date_time);
   }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]