[gnome-builder/wip/gtk4-port: 739/1774] libide/gtk: Add ide_g_time_span_to_label




commit 67679a240235b4163e065fd8451eaf72c8292c20
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Tue Apr 26 16:41:22 2022 -0300

    libide/gtk: Add ide_g_time_span_to_label
    
    This is a pristine copy from libdazzle, only changing the prefix
    from 'dzl' to 'ide'.

 src/libide/gtk/ide-gtk.c | 34 ++++++++++++++++++++++++++++++++++
 src/libide/gtk/ide-gtk.h |  2 ++
 2 files changed, 36 insertions(+)
---
diff --git a/src/libide/gtk/ide-gtk.c b/src/libide/gtk/ide-gtk.c
index 75e2fd1bb..9bf838d30 100644
--- a/src/libide/gtk/ide-gtk.c
+++ b/src/libide/gtk/ide-gtk.c
@@ -381,3 +381,37 @@ ide_gtk_widget_destroyed (GtkWidget  *widget,
   if (location != NULL)
     *location = NULL;
 }
+
+/**
+ * ide_g_time_span_to_label:
+ * @span: the span of time
+ *
+ * Creates a string describing the time span in hours, minutes, and seconds.
+ * For example, a time span of three and a half minutes would be "3:30".
+ * 2 days, 3 hours, 6 minutes, and 20 seconds would be "51:06:20".
+ *
+ * Returns: (transfer full): A newly allocated string describing the time span.
+ */
+char *
+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);
+}
diff --git a/src/libide/gtk/ide-gtk.h b/src/libide/gtk/ide-gtk.h
index b6108fb2a..0bebec027 100644
--- a/src/libide/gtk/ide-gtk.h
+++ b/src/libide/gtk/ide-gtk.h
@@ -55,5 +55,7 @@ void      ide_gtk_list_store_insert_sorted   (GtkListStore      *store,
 IDE_AVAILABLE_IN_ALL
 void       ide_gtk_widget_destroyed          (GtkWidget         *widget,
                                               GtkWidget        **location);
+IDE_AVAILABLE_IN_ALL
+char      *ide_g_time_span_to_label          (GTimeSpan          span);
 
 G_END_DECLS


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