[gnome-builder/wip/chergert/pipeline: 13/48] util: add GTimeSpan to String utility
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/wip/chergert/pipeline: 13/48] util: add GTimeSpan to String utility
- Date: Sat, 4 Feb 2017 00:09:54 +0000 (UTC)
commit a0aaed47eb2eea599d5b7da48939193ba2544268
Author: Christian Hergert <chergert redhat com>
Date: Fri Feb 3 12:19:57 2017 -0800
util: add GTimeSpan to String utility
This simplifies the process of converting a GTimeSpan into a string for
various UI. We should use it going forward to remove duplication.
libide/util/ide-glib.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
libide/util/ide-glib.h | 5 +++++
2 files changed, 49 insertions(+), 0 deletions(-)
---
diff --git a/libide/util/ide-glib.c b/libide/util/ide-glib.c
index 0bfe8a3..9e3b32f 100644
--- a/libide/util/ide-glib.c
+++ b/libide/util/ide-glib.c
@@ -181,3 +181,47 @@ ide_gettext (const gchar *message)
return g_dgettext (GETTEXT_PACKAGE, message);
return NULL;
}
+
+gchar *
+ide_g_time_span_to_label (GTimeSpan span)
+{
+ gint64 hours;
+ gint64 minutes;
+ gint64 seconds;
+
+ span = ABS (span);
+
+ hours = span / G_TIME_SPAN_HOUR;
+ minutes = (span % G_TIME_SPAN_HOUR) / G_TIME_SPAN_MINUTE;
+ seconds = (span % G_TIME_SPAN_MINUTE) / G_TIME_SPAN_SECOND;
+
+ g_assert (minutes < 60);
+ g_assert (seconds < 60);
+
+ if (hours == 0)
+ return g_strdup_printf ("%02"G_GINT64_FORMAT":%02"G_GINT64_FORMAT,
+ minutes, seconds);
+ else
+ return g_strdup_printf ("%02"G_GINT64_FORMAT":%02"G_GINT64_FORMAT":%02"G_GINT64_FORMAT,
+ hours, minutes, seconds);
+}
+
+gboolean
+ide_g_time_span_to_label_mapping (GBinding *binding,
+ const GValue *from_value,
+ GValue *to_value,
+ gpointer user_data)
+{
+ GTimeSpan span;
+
+ g_assert (G_IS_BINDING (binding));
+ g_assert (from_value != NULL);
+ g_assert (G_VALUE_HOLDS (from_value, G_TYPE_INT64));
+ g_assert (to_value != NULL);
+ g_assert (G_VALUE_HOLDS (to_value, G_TYPE_STRING));
+
+ span = g_value_get_int64 (from_value);
+ g_value_take_string (to_value, ide_g_time_span_to_label (span));
+
+ return TRUE;
+}
diff --git a/libide/util/ide-glib.h b/libide/util/ide-glib.h
index 1895dd6..437f805 100644
--- a/libide/util/ide-glib.h
+++ b/libide/util/ide-glib.h
@@ -33,6 +33,11 @@ void ide_g_task_return_pointer_from_main (GTask *task,
GDestroyNotify notify);
void ide_g_task_return_error_from_main (GTask *task,
GError *error);
+gchar *ide_g_time_span_to_label (GTimeSpan span);
+gboolean ide_g_time_span_to_label_mapping (GBinding *binding,
+ const GValue *from_value,
+ GValue *to_value,
+ gpointer user_data);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]