[tracker/wip/carlosg/sparql1.1: 59/201] libtracker-common: Add offset argument to tracker_date_to_string()



commit 5ce5b7b3046de53ab59fa7848c11a40c2b79372a
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Mar 3 18:24:48 2019 +0100

    libtracker-common: Add offset argument to tracker_date_to_string()
    
    So that we can convert losslessly between time+offset and strings.

 src/libtracker-common/tracker-date-time.c         | 27 ++++++++++++----
 src/libtracker-common/tracker-date-time.h         |  3 +-
 src/libtracker-data/tracker-data-update.c         |  6 ++--
 src/libtracker-data/tracker-db-interface-sqlite.c |  2 +-
 tests/libtracker-common/tracker-date-time-test.c  | 39 +++++++++++++++++++++--
 tests/libtracker-miner/tracker-miner-fs-test.c    |  2 +-
 6 files changed, 66 insertions(+), 13 deletions(-)
---
diff --git a/src/libtracker-common/tracker-date-time.c b/src/libtracker-common/tracker-date-time.c
index e8279d728..162d9081a 100644
--- a/src/libtracker-common/tracker-date-time.c
+++ b/src/libtracker-common/tracker-date-time.c
@@ -201,14 +201,15 @@ tracker_string_to_date (const gchar *date_string,
 }
 
 gchar *
-tracker_date_to_string (gdouble date_time)
+tracker_date_to_string (gdouble date_time,
+                        gint    offset)
 {
-       gchar     buffer[30];
+       gchar buffer[35];
        time_t seconds;
        gint64 total_milliseconds;
        gint milliseconds;
        struct tm utc_time;
-       size_t    count;
+       size_t count, size;
 
        memset (buffer, '\0', sizeof (buffer));
        memset (&utc_time, 0, sizeof (struct tm));
@@ -218,7 +219,7 @@ tracker_date_to_string (gdouble date_time)
        if (milliseconds < 0) {
                milliseconds += 1000;
        }
-       seconds = (time_t) ((total_milliseconds - milliseconds) / 1000);
+       seconds = (time_t) ((total_milliseconds - milliseconds) / 1000) + offset;
        gmtime_r (&seconds, &utc_time);
 
        /* Output is ISO 8601 format : "YYYY-MM-DDThh:mm:ss" */
@@ -226,11 +227,25 @@ tracker_date_to_string (gdouble date_time)
 
        /* Append milliseconds (if non-zero) and time zone */
        if (milliseconds > 0) {
-               snprintf (buffer + count, sizeof (buffer) - count, ".%03dZ", milliseconds);
+               size = snprintf (buffer + count, sizeof (buffer) - count, ".%03d", milliseconds);
+               count += size;
+       }
+
+       if (offset != 0) {
+               gint hours, mins;
+
+               hours = ABS (offset) / 3600;
+               mins = (ABS (offset) % 3600) / 60;
+               size = snprintf (buffer + count, sizeof (buffer) - count, "%c%.2d:%.2d",
+                                offset < 0 ? '-' : '+',
+                                hours, mins);
+               count += size;
        } else {
-               buffer[count] = 'Z';
+               buffer[count++] = 'Z';
        }
 
+       g_assert (count <= sizeof (buffer));
+
        return count > 0 ? g_strdup (buffer) : NULL;
 }
 
diff --git a/src/libtracker-common/tracker-date-time.h b/src/libtracker-common/tracker-date-time.h
index 78b56eb1f..df949e90e 100644
--- a/src/libtracker-common/tracker-date-time.h
+++ b/src/libtracker-common/tracker-date-time.h
@@ -57,7 +57,8 @@ gint     tracker_date_time_get_local_time      (const GValue *value);
 gdouble  tracker_string_to_date                (const gchar  *date_string,
                                                 gint         *offset,
                                                 GError      **error);
-gchar *  tracker_date_to_string                (gdouble       date_time);
+gchar *  tracker_date_to_string                (gdouble       date_time,
+                                                gint          offset);
 
 G_END_DECLS
 
diff --git a/src/libtracker-data/tracker-data-update.c b/src/libtracker-data/tracker-data-update.c
index c56fefaa7..60bdfe07b 100644
--- a/src/libtracker-data/tracker-data-update.c
+++ b/src/libtracker-data/tracker-data-update.c
@@ -1679,6 +1679,7 @@ gvalue_to_string (TrackerPropertyType  type,
 {
        gchar *retval = NULL;
        gint64 datet;
+       gint offset;
 
        switch (type) {
        case TRACKER_PROPERTY_TYPE_STRING:
@@ -1697,13 +1698,14 @@ gvalue_to_string (TrackerPropertyType  type,
                break;
        case TRACKER_PROPERTY_TYPE_DATE:
                datet = g_value_get_int64 (gvalue);
-               retval = tracker_date_to_string (datet);
+               retval = tracker_date_to_string (datet, 0);
                /* it's a date-only, cut off the time */
                retval[10] = '\0';
                break;
        case TRACKER_PROPERTY_TYPE_DATETIME:
                datet = tracker_date_time_get_time (gvalue);
-               retval = tracker_date_to_string (datet);
+               offset = tracker_date_time_get_offset (gvalue);
+               retval = tracker_date_to_string (datet, offset);
                break;
        case TRACKER_PROPERTY_TYPE_RESOURCE:
        default:
diff --git a/src/libtracker-data/tracker-db-interface-sqlite.c 
b/src/libtracker-data/tracker-db-interface-sqlite.c
index 8cd0bcc01..e151d4ed3 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.c
+++ b/src/libtracker-data/tracker-db-interface-sqlite.c
@@ -428,7 +428,7 @@ function_sparql_format_time (sqlite3_context *context,
        }
 
        seconds = sqlite3_value_double (argv[0]);
-       str = tracker_date_to_string (seconds);
+       str = tracker_date_to_string (seconds, 0);
 
        sqlite3_result_text (context, str, -1, g_free);
 }
diff --git a/tests/libtracker-common/tracker-date-time-test.c 
b/tests/libtracker-common/tracker-date-time-test.c
index 7d4e78bb4..f51c36830 100644
--- a/tests/libtracker-common/tracker-date-time-test.c
+++ b/tests/libtracker-common/tracker-date-time-test.c
@@ -138,11 +138,22 @@ test_date_to_string (void)
        input = timegm (original);
 #endif
 
-       result = tracker_date_to_string (input);
-
+       result = tracker_date_to_string (input, 0);
        g_assert (result != NULL && strncmp (result, "2008-06-16T23:53:10Z", 19) == 0);
+       g_free (result);
 
+       result = tracker_date_to_string (input, 7200);
+       g_assert_cmpstr (result, ==, "2008-06-17T01:53:10+02:00");
        g_free (result);
+
+       result = tracker_date_to_string (input, -7200);
+       g_assert_cmpstr (result, ==, "2008-06-16T21:53:10-02:00");
+       g_free (result);
+
+       result = tracker_date_to_string (input, -9000);
+       g_assert_cmpstr (result, ==, "2008-06-16T21:23:10-02:30");
+       g_free (result);
+
        g_free (original);
 }
 
@@ -245,6 +256,28 @@ test_date_time_get_local_time ()
         g_assert_cmpint (tracker_date_time_get_local_time (&value), ==, 63780);
 }
 
+static void
+test_date_time_conversions (void)
+{
+        GError *error = NULL;
+        time_t time;
+        int offset;
+        const gchar *date_str;
+        gchar *result;
+
+        date_str = "2011-10-28T17:43:00+03:00";
+
+       time = tracker_string_to_date (date_str, &offset, &error);
+        g_assert (!error);
+
+        g_assert_cmpint (time, ==, 1319812980);
+        g_assert_cmpint (offset, ==, 10800);
+
+        result = tracker_date_to_string (time, offset);
+        g_assert_cmpstr (result, ==, date_str);
+       g_free (result);
+}
+
 gint
 main (gint argc, gchar **argv) 
 {
@@ -266,6 +299,8 @@ main (gint argc, gchar **argv)
                          test_date_time_get_local_date);
         g_test_add_func ("/libtracker-common/date-time/get_local_time",
                          test_date_time_get_local_time);
+        g_test_add_func ("/libtracker-common/date-time/conversions",
+                         test_date_time_conversions);
 
         return g_test_run ();
 }
diff --git a/tests/libtracker-miner/tracker-miner-fs-test.c b/tests/libtracker-miner/tracker-miner-fs-test.c
index c57579b27..d2e48c322 100644
--- a/tests/libtracker-miner/tracker-miner-fs-test.c
+++ b/tests/libtracker-miner/tracker-miner-fs-test.c
@@ -61,7 +61,7 @@ test_miner_process_file (TrackerMinerFS *miner,
        tracker_resource_add_uri (resource, "rdf:type", "nfo:FileDataObject");
 
        g_file_info_get_modification_time (info, &timeval);
-       str = tracker_date_to_string (timeval.tv_sec);
+       str = tracker_date_to_string (timeval.tv_sec, 0);
        tracker_resource_set_string (resource, "nfo:fileLastModified", str);
        g_free (str);
 


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