[tracker/wip/carlosg/sparql1.1: 45/56] libtracker-data: Add function to get timezone duration from a datetime



commit 60d6307a18485efa2867c329a796bc0141ab2366
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Jun 3 11:42:33 2019 +0200

    libtracker-data: Add function to get timezone duration from a datetime

 src/libtracker-data/tracker-db-interface-sqlite.c | 42 +++++++++++++++++++++--
 src/libtracker-data/tracker-sparql.c              | 12 +------
 2 files changed, 41 insertions(+), 13 deletions(-)
---
diff --git a/src/libtracker-data/tracker-db-interface-sqlite.c 
b/src/libtracker-data/tracker-db-interface-sqlite.c
index f43b0cd9b..8c244c3d1 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.c
+++ b/src/libtracker-data/tracker-db-interface-sqlite.c
@@ -463,9 +463,10 @@ function_sparql_timestamp (sqlite3_context *context,
                GError *error = NULL;
                const gchar *str;
                gdouble time;
+               gint offset;
 
                str = sqlite3_value_text (argv[0]);
-               time = tracker_string_to_date (str, NULL, &error);
+               time = tracker_string_to_date (str, &offset, &error);
 
                if (error) {
                        sqlite3_result_error (context, "Failed time string conversion", -1);
@@ -473,7 +474,7 @@ function_sparql_timestamp (sqlite3_context *context,
                        return;
                }
 
-               sqlite3_result_double (context, time);
+               sqlite3_result_double (context, time + offset);
        } else {
                sqlite3_result_error (context, "Invalid argument type", -1);
        }
@@ -523,6 +524,41 @@ function_sparql_time_sort (sqlite3_context *context,
        sqlite3_result_int64 (context, sort_key);
 }
 
+static void
+function_sparql_time_zone_duration (sqlite3_context *context,
+                                    int              argc,
+                                    sqlite3_value   *argv[])
+{
+       if (argc != 1) {
+               sqlite3_result_error (context, "Invalid argument count", -1);
+               return;
+       }
+
+       if (sqlite3_value_type (argv[0]) == SQLITE_NULL) {
+               sqlite3_result_null (context);
+               return;
+       } else if (sqlite3_value_numeric_type (argv[0]) == SQLITE_INTEGER) {
+               sqlite3_result_int (context, 0);
+       } else if (sqlite3_value_type (argv[0]) == SQLITE_TEXT) {
+               GError *error = NULL;
+               const gchar *str;
+               gint offset;
+
+               str = sqlite3_value_text (argv[0]);
+               tracker_string_to_date (str, &offset, &error);
+
+               if (error) {
+                       sqlite3_result_error (context, "Invalid date", -1);
+                       g_error_free (error);
+                       return;
+               }
+
+               sqlite3_result_int (context, offset);
+       } else {
+               sqlite3_result_error (context, "Invalid argument type", -1);
+       }
+}
+
 static void
 function_sparql_cartesian_distance (sqlite3_context *context,
                                     int              argc,
@@ -1532,6 +1568,8 @@ initialize_functions (TrackerDBInterface *db_interface)
                  function_sparql_timestamp },
                { "SparqlTimeSort", 1, SQLITE_ANY | SQLITE_DETERMINISTIC,
                  function_sparql_time_sort },
+               { "SparqlTimezoneDuration", 1, SQLITE_ANY | SQLITE_DETERMINISTIC,
+                 function_sparql_time_zone_duration },
                /* Paths and filenames */
                { "SparqlStringFromFilename", 1, SQLITE_ANY | SQLITE_DETERMINISTIC,
                  function_sparql_string_from_filename },
diff --git a/src/libtracker-data/tracker-sparql.c b/src/libtracker-data/tracker-sparql.c
index 4f389b5d1..893530fd0 100644
--- a/src/libtracker-data/tracker-sparql.c
+++ b/src/libtracker-data/tracker-sparql.c
@@ -5407,21 +5407,11 @@ handle_xpath_function (TrackerSparql  *sparql,
                        return FALSE;
                sparql->current_state.expression_type = TRACKER_PROPERTY_TYPE_INTEGER;
        } else if (g_str_equal (function, FN_NS "timezone-from-dateTime")) {
-               TrackerVariable *variable;
-
                _step (sparql);
-               _append_string (sparql, "( ");
+               _append_string (sparql, "SparqlTimezoneDuration( ");
                _expect (sparql, RULE_TYPE_LITERAL, LITERAL_OPEN_PARENS);
 
                _call_rule (sparql, NAMED_RULE_Expression, error);
-               variable = _last_node_variable (sparql);
-
-               if (!variable) {
-                       _raise (PARSE, "Expected variable", "fn:timezone-from-dateTime");
-               } else {
-                       _append_string_printf (sparql, " - %s ",
-                                              tracker_variable_get_sql_expression (variable));
-               }
 
                _expect (sparql, RULE_TYPE_LITERAL, LITERAL_CLOSE_PARENS);
                _append_string (sparql, ") ");


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