[gnome-builder/wip/chergert/pipeline-merge: 10/64] util: add GTimeSpan to String utility



commit 24783e42561f83735d8696a288b62a0f7a7fd0f6
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]