summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Garnacho <carlosg@gnome.org>2021-04-07 13:14:45 +0200
committerCarlos Garnacho <carlosg@gnome.org>2021-04-07 23:28:04 +0200
commite1c060e3e495491ee424d21cb2d10abcd474f793 (patch)
tree4628ea2a2b970e6519f4bc47a938e12e975a4070
parentca800996ba952a273de2511d9144db6aa6c07f84 (diff)
downloadtracker-wip/carlosg/gdatetime.tar.gz
tests: Remove old date/time helperswip/carlosg/gdatetime
This is now entirely replaced with GDateTime. Also, drop the related tests, the two new small helper functions are tested along with the SPARQL machinery.
-rw-r--r--src/libtracker-common/tracker-date-time.c322
-rw-r--r--src/libtracker-common/tracker-date-time.h17
-rw-r--r--tests/libtracker-common/meson.build1
-rw-r--r--tests/libtracker-common/tracker-date-time-test.c274
4 files changed, 0 insertions, 614 deletions
diff --git a/src/libtracker-common/tracker-date-time.c b/src/libtracker-common/tracker-date-time.c
index 388a9725f..bfc05c782 100644
--- a/src/libtracker-common/tracker-date-time.c
+++ b/src/libtracker-common/tracker-date-time.c
@@ -20,17 +20,6 @@
#include "config.h"
-/* For timegm usage on __GLIBC__ we need _GNU_SOURCE, should be
- * defined in config.h based on configure checks...
- */
-
-#include <strings.h>
-#include <string.h>
-#include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <time.h>
-
#include <glib.h>
#include "tracker-date-time.h"
@@ -39,317 +28,6 @@ GQuark tracker_date_error_quark (void) {
return g_quark_from_static_string ("tracker_date_error-quark");
}
-gdouble
-tracker_string_to_date (const gchar *date_string,
- gint *offset_p,
- GError **error)
-{
- /* TODO Add more checks.
- */
-
- static GRegex *regex = NULL;
-
- GMatchInfo *match_info;
- gchar *match;
- struct tm tm;
- gdouble t;
- gint offset;
- gboolean timezoned;
-
- if (!date_string) {
- g_set_error (error, TRACKER_DATE_ERROR, TRACKER_DATE_ERROR_EMPTY,
- "Empty date string");
- return -1;
- }
-
- /* We should have a valid iso 8601 date in format
- * YYYY-MM-DDThh:mm:ss with optional TZ
- */
-
- if (!regex) {
- GError *e = NULL;
- regex = g_regex_new ("^(-?[0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])T([0-9][0-9]):([0-9][0-9]):([0-9][0-9])(\\.[0-9]+)?(Z|(\\+|-)([0-9][0-9]):?([0-9][0-9]))?$", 0, 0, &e);
- if (e) {
- g_error ("%s", e->message);
- }
- }
-
- if (!g_regex_match (regex, date_string, 0, &match_info)) {
- g_match_info_free (match_info);
- g_set_error (error, TRACKER_DATE_ERROR, TRACKER_DATE_ERROR_INVALID_ISO8601,
- "Not a ISO 8601 date string. Allowed form is [-]CCYY-MM-DDThh:mm:ss[Z|(+|-)hh:mm]");
- return -1;
- }
-
- memset (&tm, 0, sizeof (struct tm));
-
- /* year */
- match = g_match_info_fetch (match_info, 1);
- tm.tm_year = atoi (match) - 1900;
- g_free (match);
-
- /* month */
- match = g_match_info_fetch (match_info, 2);
- tm.tm_mon = atoi (match) - 1;
- g_free (match);
-
- /* day of month */
- match = g_match_info_fetch (match_info, 3);
- tm.tm_mday = atoi (match);
- g_free (match);
-
- /* hour */
- match = g_match_info_fetch (match_info, 4);
- tm.tm_hour = atoi (match);
- g_free (match);
-
- /* minute */
- match = g_match_info_fetch (match_info, 5);
- tm.tm_min = atoi (match);
- g_free (match);
-
- /* second */
- match = g_match_info_fetch (match_info, 6);
- tm.tm_sec = atoi (match);
- g_free (match);
-
- match = g_match_info_fetch (match_info, 8);
- timezoned = (match && strlen (match) > 0);
- g_free (match);
-
- if (timezoned) {
- /* timezoned */
-
- /* mktime() always assumes that "tm" is in locale time but we
- * want to keep control on time, so we go to UTC
- */
-#if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined (__GLIBC__))
- t = mktime (&tm);
- t -= timezone;
-#else
- t = timegm (&tm);
-#endif
-
- offset = 0;
-
- match = g_match_info_fetch (match_info, 9);
- if (match && strlen (match) > 0) {
- /* non-UTC timezone */
-
- gboolean positive_offset;
-
- positive_offset = (match[0] == '+');
- g_free (match);
-
- match = g_match_info_fetch (match_info, 10);
- offset = atoi (match) * 3600;
- g_free (match);
-
- match = g_match_info_fetch (match_info, 11);
- offset += atoi (match) * 60;
- g_free (match);
-
- if (!positive_offset) {
- offset = -offset;
- }
-
- if (offset < -14 * 3600 || offset > 14 * 3600) {
- g_set_error (error, TRACKER_DATE_ERROR, TRACKER_DATE_ERROR_OFFSET,
- "UTC offset too large: %d seconds", offset);
- g_match_info_free (match_info);
- return -1;
- }
-
- t -= offset;
- }
- } else {
- time_t t2;
-
- /* local time */
- tm.tm_isdst = -1;
-
- t = mktime (&tm);
-
- /* calculate UTC offset, requires timegm for correct result
- with past times when timezone had different UTC offset */
-#if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined (__GLIBC__))
- offset = -timezone + (tm.tm_isdst > 0 ? 3600 : 0);
-#else
- t2 = timegm (&tm);
- offset = t2 - (time_t) t;
-#endif
- }
-
- match = g_match_info_fetch (match_info, 7);
- if (match && strlen (match) > 0) {
- char milliseconds[4] = "000\0";
- /* first character of match is decimal point
- we're interested in a maximum of 3 decimal places (milliseconds) */
- memcpy (milliseconds, match + 1, MIN (3, strlen (match + 1)));
- t += (gdouble) atoi (milliseconds) / 1000;
- }
- g_free (match);
-
- g_match_info_free (match_info);
-
- if (offset_p) {
- *offset_p = offset;
- }
-
- return t;
-}
-
-gchar *
-tracker_date_to_string (gdouble date_time,
- gint offset)
-{
- gchar buffer[35];
- time_t seconds;
- gint64 total_milliseconds;
- gint milliseconds;
- struct tm utc_time;
- size_t count, size;
-
- memset (buffer, '\0', sizeof (buffer));
- memset (&utc_time, 0, sizeof (struct tm));
-
- total_milliseconds = (gint64) round (date_time * 1000);
- milliseconds = total_milliseconds % 1000;
- if (milliseconds < 0) {
- 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" */
- count = strftime (buffer, sizeof (buffer), "%4Y-%m-%dT%T", &utc_time);
-
- /* Append milliseconds (if non-zero) and time zone */
- if (milliseconds > 0) {
- 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';
- }
-
- g_assert (count <= sizeof (buffer));
-
- return count > 0 ? g_strdup (buffer) : NULL;
-}
-
-static void
-date_time_value_init (GValue *value)
-{
- value->data[0].v_double = 0;
- value->data[1].v_int = 0;
-}
-
-static void
-date_time_value_copy (const GValue *src_value,
- GValue *dest_value)
-{
- dest_value->data[0].v_double = src_value->data[0].v_double;
- dest_value->data[1].v_int = src_value->data[1].v_int;
-}
-
-GType
-tracker_date_time_get_type (void)
-{
- static GType tracker_date_time_type_id = 0;
- if (G_UNLIKELY (tracker_date_time_type_id == 0)) {
- static const GTypeValueTable value_table = {
- date_time_value_init,
- NULL,
- date_time_value_copy
- };
- static const GTypeInfo type_info = {
- 0,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- 0,
- NULL,
- &value_table
- };
- static const GTypeFundamentalInfo fundamental_info = {
- 0
- };
- tracker_date_time_type_id = g_type_register_fundamental (
- g_type_fundamental_next (),
- "TrackerDateTime",
- &type_info,
- &fundamental_info,
- 0);
- }
- return tracker_date_time_type_id;
-}
-
-void
-tracker_date_time_set (GValue *value,
- gdouble time,
- gint offset)
-{
- g_return_if_fail (G_VALUE_HOLDS (value, TRACKER_TYPE_DATE_TIME));
- g_return_if_fail (offset >= -14 * 3600 && offset <= 14 * 3600);
-
- value->data[0].v_double = time;
- value->data[1].v_int = offset;
-}
-
-void
-tracker_date_time_set_from_string (GValue *value,
- const gchar *date_time_string,
- GError **error)
-{
- gdouble time;
- gint offset;
- GError *new_error = NULL;
-
- g_return_if_fail (G_VALUE_HOLDS (value, TRACKER_TYPE_DATE_TIME));
- g_return_if_fail (date_time_string != NULL);
-
- time = tracker_string_to_date (date_time_string, &offset, &new_error);
-
- if (new_error != NULL) {
- g_propagate_error (error, new_error);
- return;
- }
-
- tracker_date_time_set (value, time, offset);
-}
-
-gdouble
-tracker_date_time_get_time (const GValue *value)
-{
- g_return_val_if_fail (G_VALUE_HOLDS (value, TRACKER_TYPE_DATE_TIME), 0);
-
- /* UTC timestamp */
- return value->data[0].v_double;
-}
-
-gint
-tracker_date_time_get_offset (const GValue *value)
-{
- g_return_val_if_fail (G_VALUE_HOLDS (value, TRACKER_TYPE_DATE_TIME), 0);
-
- /* UTC offset */
- return value->data[1].v_int;
-}
-
GDateTime *
tracker_date_new_from_iso8601 (const gchar *string,
GError **error)
diff --git a/src/libtracker-common/tracker-date-time.h b/src/libtracker-common/tracker-date-time.h
index 6290b0d73..34ddbf318 100644
--- a/src/libtracker-common/tracker-date-time.h
+++ b/src/libtracker-common/tracker-date-time.h
@@ -41,23 +41,6 @@ typedef enum {
GQuark tracker_date_error_quark (void);
-GType tracker_date_time_get_type (void);
-
-void tracker_date_time_set (GValue *value,
- gdouble time,
- gint offset);
-void tracker_date_time_set_from_string (GValue *value,
- const gchar *date_time_string,
- GError **error);
-gdouble tracker_date_time_get_time (const GValue *value);
-gint tracker_date_time_get_offset (const GValue *value);
-
-gdouble tracker_string_to_date (const gchar *date_string,
- gint *offset,
- GError **error);
-gchar * tracker_date_to_string (gdouble date_time,
- gint offset);
-
GDateTime * tracker_date_new_from_iso8601 (const gchar *string,
GError **error);
gchar * tracker_date_format_iso8601 (GDateTime *datetime);
diff --git a/tests/libtracker-common/meson.build b/tests/libtracker-common/meson.build
index a748e57f1..57fc32927 100644
--- a/tests/libtracker-common/meson.build
+++ b/tests/libtracker-common/meson.build
@@ -1,5 +1,4 @@
libtracker_common_tests = [
- 'date-time',
'file-utils',
'utils',
]
diff --git a/tests/libtracker-common/tracker-date-time-test.c b/tests/libtracker-common/tracker-date-time-test.c
deleted file mode 100644
index 888269120..000000000
--- a/tests/libtracker-common/tracker-date-time-test.c
+++ /dev/null
@@ -1,274 +0,0 @@
-/*
- * Copyright (C) 2011, Nokia <ivan.frade@nokia.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- */
-
-#include <time.h>
-#include <string.h>
-
-#include <glib.h>
-#include <glib-object.h>
-
-#include <libtracker-common/tracker-date-time.h>
-
-/* This define was committed in glib 18.07.2011
- * https://bugzilla.gnome.org/show_bug.cgi?id=577231
- */
-#ifndef G_VALUE_INIT
-#define G_VALUE_INIT { 0, { { 0 } } }
-#endif
-
-static void
-test_string_to_date_failures_subprocess ()
-{
- GError *error = NULL;
-
- tracker_string_to_date (NULL, NULL, &error);
-
- if (error) {
- g_warning ("%s", error->message);
- g_error_free (error);
- }
-}
-
-static void
-test_string_to_date_failures ()
-{
- g_test_trap_subprocess ("/libtracker-common/date-time/string_to_date_failures/subprocess", 0, 0);
- g_test_trap_assert_failed ();
- g_test_trap_assert_stderr ("*Empty date string*");
-}
-
-static void
-test_string_to_date (void)
-{
- GDate *expected;
- GDate *result;
- time_t result_time_t;
- const gchar *input = "2008-06-16T11:10:10+0600";
- gchar *timezone = g_strdup (g_getenv ("TZ"));
- GError *error = NULL;
-
- if (! g_setenv ("TZ", "UTC", TRUE)) {
- g_test_message ("unable to set timezone, test results are invalid, skipping\n");
- if (timezone) {
- g_free (timezone);
- }
- return;
- }
-
- expected = g_date_new_dmy (16, G_DATE_JUNE, 2008);
-
- result_time_t = tracker_string_to_date (input, NULL, &error);
- g_assert_no_error (error);
-
- result = g_date_new ();
- g_date_set_time_t (result, result_time_t);
-
- if (timezone) {
- g_setenv ("TZ", timezone, TRUE);
- g_free (timezone);
- } else {
- g_unsetenv ("TZ");
- }
-
- g_assert_cmpint (g_date_get_year (expected), ==, g_date_get_year (result));
- g_assert_cmpint (g_date_get_day (expected), ==, g_date_get_day (result));
- g_assert_cmpint (g_date_get_month (expected), ==, g_date_get_month (result));
-
- g_date_free (expected);
- g_date_free (result);
-
-
- result_time_t = tracker_string_to_date ("", NULL, &error);
- g_assert_cmpint (result_time_t, ==, -1);
- g_assert_error (error, TRACKER_DATE_ERROR, TRACKER_DATE_ERROR_INVALID_ISO8601);
- g_error_free (error);
- error = NULL;
-
- result_time_t = tracker_string_to_date ("i am not a date", NULL, &error);
- g_assert_cmpint (result_time_t, ==, -1);
- g_assert_error (error, TRACKER_DATE_ERROR, TRACKER_DATE_ERROR_INVALID_ISO8601);
- g_error_free (error);
- error = NULL;
-
- /* Fails! Check the code
- result_time_t = tracker_string_to_date ("2008-06-32T04:23:10+0000", NULL);
- g_assert_cmpint (result_time_t, ==, -1);
- */
-
- /* More cases of string->date are tested in tracker_date_time_from_string...
- * it is more convinient to test them there
- */
-}
-
-static void
-test_date_to_string (void)
-{
- struct tm *original;
- time_t input;
- gchar *result;
-
- original = g_new0 (struct tm, 1);
- original->tm_sec = 10;
- original->tm_min = 53;
- original->tm_hour = 23;
- original->tm_mday = 16;
- original->tm_mon = 5;
- original->tm_year = 108;
- original->tm_isdst = 0;
-
-#if !(defined(__FreeBSD__) || defined(__OpenBSD__))
- input = mktime (original) - timezone;
-#else
- input = timegm (original);
-#endif
-
- result = tracker_date_to_string (input, 0);
- g_assert_true (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);
-}
-
-static void
-test_date_time_get_set ()
-{
- GValue value = G_VALUE_INIT;
- GValue copy = G_VALUE_INIT;
-
- g_value_init (&value, TRACKER_TYPE_DATE_TIME);
- g_value_init (&copy, TRACKER_TYPE_DATE_TIME);
-
- tracker_date_time_set (&value, 123456789, 3600);
-
- g_assert_cmpint (tracker_date_time_get_time (&value), ==, 123456789);
- g_assert_cmpint (tracker_date_time_get_offset (&value), ==, 3600);
-
- g_value_copy (&value, &copy);
-
- g_assert_cmpint (tracker_date_time_get_time (&copy), ==, 123456789);
- g_assert_cmpint (tracker_date_time_get_offset (&copy), ==, 3600);
-}
-
-static void
-test_date_time_from_string ()
-{
- GValue value = G_VALUE_INIT;
- GError *error = NULL;
-
- g_value_init (&value, TRACKER_TYPE_DATE_TIME);
-
- tracker_date_time_set_from_string (&value, "2011-10-28T17:43:00+03:00", &error);
- g_assert_true (!error);
- g_assert_cmpint (tracker_date_time_get_time (&value), ==, 1319812980);
- g_assert_cmpint (tracker_date_time_get_offset (&value), ==, 10800);
-
-
- /* Negative offset */
- tracker_date_time_set_from_string (&value, "2011-10-28T17:43:00-03:00", &error);
- g_assert_true (!error);
- g_assert_cmpint (tracker_date_time_get_time (&value), ==, 1319834580);
- g_assert_cmpint (tracker_date_time_get_offset (&value), ==, -10800);
-
- /* No offset */
- tracker_date_time_set_from_string (&value, "2011-10-28T17:43:00Z", &error);
- g_assert_true (!error);
- g_assert_cmpint (tracker_date_time_get_time (&value), ==, 1319823780);
- g_assert_cmpint (tracker_date_time_get_offset (&value), ==, 0);
-
- /* Invalid format */
- tracker_date_time_set_from_string (&value, "2011-10-28T17:43:00Z0900", &error);
- g_assert_true (error);
- g_error_free (error);
- error = NULL;
-
- /* There are no 28 months... */
- tracker_date_time_set_from_string (&value, "2011-28-10T17:43:00Z0900", &error);
- g_assert_true (error);
- g_error_free (error);
- error = NULL;
-
- /* ... nor more than +-12 offsets */
- tracker_date_time_set_from_string (&value, "2011-28-10T17:43:00+17:00", &error);
- g_assert_true (error);
- g_error_free (error);
- error = NULL;
-
- /* ... the same for the glory of the branch % */
- tracker_date_time_set_from_string (&value, "2011-28-10T17:43:00-17:00", &error);
- g_assert_true (error);
- g_error_free (error);
- error = NULL;
-}
-
-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_true (!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)
-{
- g_test_init (&argc, &argv, NULL);
-
- g_test_add_func ("/libtracker-common/date-time/date_to_string",
- test_date_to_string);
- g_test_add_func ("/libtracker-common/date-time/string_to_date",
- test_string_to_date);
- g_test_add_func ("/libtracker-common/date-time/string_to_date_failures",
- test_string_to_date_failures);
- g_test_add_func ("/libtracker-common/date-time/string_to_date_failures/subprocess",
- test_string_to_date_failures_subprocess);
- g_test_add_func ("/libtracker-common/date-time/get_set",
- test_date_time_get_set);
- g_test_add_func ("/libtracker-common/date-time/from_string",
- test_date_time_from_string);
- g_test_add_func ("/libtracker-common/date-time/conversions",
- test_date_time_conversions);
-
- return g_test_run ();
-}