summaryrefslogtreecommitdiff
path: root/src/libtracker-common
diff options
context:
space:
mode:
authorCarlos Garnacho <carlosg@gnome.org>2022-03-14 16:38:28 +0100
committerCarlos Garnacho <carlosg@gnome.org>2022-03-14 16:46:19 +0100
commit7b5ace74d80f3f6009782c7f0616f119d72df7c2 (patch)
tree594ac2f3ecce17354088b7585850ddbe4936fa2f /src/libtracker-common
parent32bd56cc4a5d7d700a300b3231abe551e18252e5 (diff)
downloadtracker-7b5ace74d80f3f6009782c7f0616f119d72df7c2.tar.gz
libtracker-common: Do not miss subsecond info printing iso8601 dates
We used %T as a shortcut to print times, but that translates to %H:%M:%S and misses subsecond information. To avoid that, check whether the passed GDateTime contains subsecond information and resort to %H:%M:%S.%f in that case. Since we prefer shorter iso8601 strings for storage purposes, combine this handling with the existing paths for timezone information, so iso8601 strings that don't need either can cut down those extra chars. Fixes subsecond information being lost on database inserts, and possibly other date comparison/printing misbehaviors.
Diffstat (limited to 'src/libtracker-common')
-rw-r--r--src/libtracker-common/tracker-date-time.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/libtracker-common/tracker-date-time.c b/src/libtracker-common/tracker-date-time.c
index bfc05c782..494b9f5cc 100644
--- a/src/libtracker-common/tracker-date-time.c
+++ b/src/libtracker-common/tracker-date-time.c
@@ -54,8 +54,17 @@ tracker_date_new_from_iso8601 (const gchar *string,
gchar *
tracker_date_format_iso8601 (GDateTime *datetime)
{
- if (g_date_time_get_utc_offset (datetime) == 0)
- return g_date_time_format (datetime, "%C%y-%m-%dT%TZ");
- else
+ gboolean has_offset, has_subsecond;
+
+ has_offset = g_date_time_get_utc_offset (datetime) != 0;
+ has_subsecond = g_date_time_get_microsecond (datetime) != 0;
+
+ if (has_offset && has_subsecond)
+ return g_date_time_format (datetime, "%C%y-%m-%dT%H:%M:%S.%f%:z");
+ else if (has_offset)
return g_date_time_format (datetime, "%C%y-%m-%dT%T%:z");
+ else if (has_subsecond)
+ return g_date_time_format (datetime, "%C%y-%m-%dT%H:%M:%S.%fZ");
+ else
+ return g_date_time_format (datetime, "%C%y-%m-%dT%TZ");
}