[gnome-disk-utility] Streamline time duration routines



commit a59c0e7c7aa7b70ede06586117760b7997391935
Author: David Zeuthen <zeuthen gmail com>
Date:   Thu Jun 28 11:58:18 2012 -0400

    Streamline time duration routines
    
    Signed-off-by: David Zeuthen <zeuthen gmail com>

 src/disks/gduatasmartdialog.c         |    9 ++-
 src/disks/gdubenchmarkdialog.c        |    3 +-
 src/disks/gducreatediskimagedialog.c  |    3 +-
 src/disks/gdudisksettingsdialog.c     |   12 +++-
 src/disks/gdurestorediskimagedialog.c |    3 +-
 src/disks/gdutypes.h                  |    6 ++
 src/disks/gduutils.c                  |  109 +++++++++------------------------
 src/disks/gduutils.h                  |    6 +-
 src/disks/gduwindow.c                 |    2 +-
 9 files changed, 59 insertions(+), 94 deletions(-)
---
diff --git a/src/disks/gduatasmartdialog.c b/src/disks/gduatasmartdialog.c
index 4841350..c697768 100644
--- a/src/disks/gduatasmartdialog.c
+++ b/src/disks/gduatasmartdialog.c
@@ -708,7 +708,8 @@ pretty_to_string (guint64 pretty,
   switch (pretty_unit)
     {
     case 2: /* SK_SMART_ATTRIBUTE_UNIT_MSECONDS */
-      ret = gdu_utils_format_duration_msec (pretty);
+      ret = gdu_utils_format_duration_usec (pretty * 1000,
+                                            GDU_FORMAT_DURATION_FLAGS_SUBSECOND_PRECISION);
       break;
 
     case 3: /* SK_SMART_ATTRIBUTE_UNIT_SECTORS */
@@ -913,7 +914,8 @@ update_updated_label (DialogData *data)
 
   now = time (NULL);
   updated = udisks_drive_ata_get_smart_updated (data->ata);
-  s = gdu_utils_duration_to_string (now - updated, FALSE);
+  s = gdu_utils_format_duration_usec ((now - updated) * G_USEC_PER_SEC,
+                                      GDU_FORMAT_DURATION_FLAGS_NONE);
   s2 = g_strdup_printf (_("%s ago"), s);
   gtk_label_set_text (GTK_LABEL (data->updated_label), s2);
   g_free (s2);
@@ -951,7 +953,8 @@ format_powered_on (UDisksDriveAta *ata)
 
   secs = udisks_drive_ata_get_smart_power_on_seconds (ata);
   if (secs > 0)
-    ret = gdu_utils_format_duration_msec (secs * 1000);
+    ret = gdu_utils_format_duration_usec (secs * G_USEC_PER_SEC,
+                                          GDU_FORMAT_DURATION_FLAGS_NONE);
   return ret;
 }
 
diff --git a/src/disks/gdubenchmarkdialog.c b/src/disks/gdubenchmarkdialog.c
index b016745..7e671db 100644
--- a/src/disks/gdubenchmarkdialog.c
+++ b/src/disks/gdubenchmarkdialog.c
@@ -700,7 +700,8 @@ update_updated_label (DialogData *data)
           time_benchmarked_dt_local = g_date_time_to_local (time_benchmarked_dt);
           time_benchmarked_str = g_date_time_format (time_benchmarked_dt_local, "%c");
 
-          s = gdu_utils_duration_to_string ((now_usec - data->bm_time_benchmarked_usec) / G_USEC_PER_SEC, FALSE);
+          s = gdu_utils_format_duration_usec ((now_usec - data->bm_time_benchmarked_usec),
+                                              GDU_FORMAT_DURATION_FLAGS_NONE);
           /* Translators: The first %s is the date and time the benchmark took place in the preferred
            * format for the locale (e.g. "%c" for strftime()/g_date_time_format()), for example
            * "Tue 12 Jun 2012 03:57:08 PM EDT". The second %s is how long ago that is from right
diff --git a/src/disks/gducreatediskimagedialog.c b/src/disks/gducreatediskimagedialog.c
index e8dfb53..3ff129d 100644
--- a/src/disks/gducreatediskimagedialog.c
+++ b/src/disks/gducreatediskimagedialog.c
@@ -250,7 +250,8 @@ write_cb (GOutputStream  *output_stream,
     {
       s2 = g_format_size (data->total_bytes_read);
       s3 = g_format_size (data->block_size);
-      s4 = gdu_utils_duration_to_string (usec_remaining / G_USEC_PER_SEC, TRUE);
+      s4 = gdu_utils_format_duration_usec (usec_remaining,
+                                           GDU_FORMAT_DURATION_FLAGS_NONE);
       s5 = g_format_size (bytes_per_sec);
       s = g_strdup_printf ("%s of %s copied â %s remaining (%s/sec)", s2, s3, s4, s5);
       g_free (s5);
diff --git a/src/disks/gdudisksettingsdialog.c b/src/disks/gdudisksettingsdialog.c
index e399ffe..3b186d8 100644
--- a/src/disks/gdudisksettingsdialog.c
+++ b/src/disks/gdudisksettingsdialog.c
@@ -279,15 +279,18 @@ update_standby_label (DialogData *data)
     }
   else if (value < 241)
     {
-      s = gdu_utils_format_duration_msec (value * 5 * 1000);
+      s = gdu_utils_format_duration_usec (value * 5 * G_USEC_PER_SEC,
+                                          GDU_FORMAT_DURATION_FLAGS_NONE);
     }
   else if (value < 252)
     {
-      s = gdu_utils_format_duration_msec ((value - 240) * 30 * 60 * 1000);
+      s = gdu_utils_format_duration_usec ((value - 240) * 30 * 60 * G_USEC_PER_SEC,
+                                          GDU_FORMAT_DURATION_FLAGS_NONE);
     }
   else if (value == 252)
     {
-      s = gdu_utils_format_duration_msec (21 * 60 * 1000);
+      s = gdu_utils_format_duration_usec (21 * 60 * G_USEC_PER_SEC,
+                                          GDU_FORMAT_DURATION_FLAGS_NONE);
     }
   else if (value == 253)
     {
@@ -299,7 +302,8 @@ update_standby_label (DialogData *data)
     }
   else if (value == 255)
     {
-      s = gdu_utils_format_duration_msec ((21 * 60 + 15) * 1000);
+      s = gdu_utils_format_duration_usec ((21 * 60 + 15) * G_USEC_PER_SEC,
+                                          GDU_FORMAT_DURATION_FLAGS_NONE);
     }
   gtk_label_set_text (GTK_LABEL (data->standby_value_label), s);
   g_free (s);
diff --git a/src/disks/gdurestorediskimagedialog.c b/src/disks/gdurestorediskimagedialog.c
index d28f64c..5648cbe 100644
--- a/src/disks/gdurestorediskimagedialog.c
+++ b/src/disks/gdurestorediskimagedialog.c
@@ -290,7 +290,8 @@ write_cb (GOutputStream  *output_stream,
     {
       s2 = g_format_size (data->total_bytes_read);
       s3 = g_format_size (data->file_size);
-      s4 = gdu_utils_duration_to_string (usec_remaining / G_USEC_PER_SEC, TRUE);
+      s4 = gdu_utils_format_duration_usec (usec_remaining,
+                                           GDU_FORMAT_DURATION_FLAGS_NONE);
       s5 = g_format_size (bytes_per_sec);
       s = g_strdup_printf ("%s of %s copied â %s remaining (%s/sec)", s2, s3, s4, s5);
       g_free (s5);
diff --git a/src/disks/gdutypes.h b/src/disks/gdutypes.h
index 917908a..c106d11 100644
--- a/src/disks/gdutypes.h
+++ b/src/disks/gdutypes.h
@@ -52,6 +52,12 @@ typedef struct _GduPasswordStrengthWidget GduPasswordStrengthWidget;
 struct _GduEstimator;
 typedef struct _GduEstimator GduEstimator;
 
+typedef enum
+{
+  GDU_FORMAT_DURATION_FLAGS_NONE                 = 0,
+  GDU_FORMAT_DURATION_FLAGS_SUBSECOND_PRECISION  = (1<<0)
+} GduFormatDurationFlags;
+
 G_END_DECLS
 
 #endif /* __GDU_TYPES_H__ */
diff --git a/src/disks/gduutils.c b/src/disks/gduutils.c
index 6d85482..5893311 100644
--- a/src/disks/gduutils.c
+++ b/src/disks/gduutils.c
@@ -97,56 +97,6 @@ gdu_utils_configure_file_chooser_for_disk_images (GtkFileChooser *file_chooser)
 
 /* ---------------------------------------------------------------------------------------------------- */
 
-gchar *
-gdu_utils_duration_to_string (guint    duration_sec,
-                              gboolean include_second_precision)
-{
-  gchar *s;
-
-  if (duration_sec < 60)
-    {
-      if (include_second_precision)
-        {
-          s = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
-                                            "%d second",
-                                            "%d seconds",
-                                            duration_sec),
-                               duration_sec);
-        }
-      else
-        {
-          s = g_strdup (_("Less than a minute"));
-        }
-    }
-  else if (duration_sec < 3600)
-    {
-      s = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
-                                        "%d minute",
-                                        "%d minutes",
-                                        duration_sec / 60),
-                           duration_sec / 60);
-    }
-  else if (duration_sec < 86400)
-    {
-      s = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
-                                        "%d hour",
-                                        "%d hours",
-                                        duration_sec / 3600),
-                           duration_sec / 3600);
-    }
-  else
-    {
-      s = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
-                                        "%d day",
-                                        "%d days",
-                                        duration_sec / 86400),
-                           duration_sec / 86400);
-    }
-  return s;
-}
-
-/* ---------------------------------------------------------------------------------------------------- */
-
 /* wouldn't need this if glade would support GtkInfoBar #$ #$@#!! # */
 
 GtkWidget *
@@ -470,24 +420,25 @@ milliseconds_to_string (gint value)
   return g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%d milli-second", "%d milli-seconds", value), value);
 }
 
-#define MSEC_PER_YEAR   (1000.0 * 60 * 60 * 24 * 365.25)
-#define MSEC_PER_MONTH  (1000.0 * 60 * 60 * 24 * 365.25 / 12.0)
-#define MSEC_PER_DAY    (1000.0 * 60 * 60 * 24)
-#define MSEC_PER_HOUR   (1000.0 * 60 * 60)
-#define MSEC_PER_MINUTE (1000.0 * 60)
-#define MSEC_PER_SECOND (1000.0)
+#define USEC_PER_YEAR   (G_USEC_PER_SEC * 60LL * 60 * 24 * 365.25)
+#define USEC_PER_MONTH  (G_USEC_PER_SEC * 60LL * 60 * 24 * 365.25 / 12.0)
+#define USEC_PER_DAY    (G_USEC_PER_SEC * 60LL * 60 * 24)
+#define USEC_PER_HOUR   (G_USEC_PER_SEC * 60LL * 60)
+#define USEC_PER_MINUTE (G_USEC_PER_SEC * 60LL)
+#define MSEC_PER_USEC   (1000LL)
 
 gchar *
-gdu_utils_format_duration_msec (guint64 msec)
+gdu_utils_format_duration_usec (guint64                usec,
+                                GduFormatDurationFlags flags)
 {
   gchar *ret;
-  guint years = 0;
-  guint months = 0;
-  guint days = 0;
-  guint hours = 0;
-  guint minutes = 0;
-  guint seconds = 0;
-  guint milliseconds = 0;
+  guint64 years = 0;
+  guint64 months = 0;
+  guint64 days = 0;
+  guint64 hours = 0;
+  guint64 minutes = 0;
+  guint64 seconds = 0;
+  guint64 milliseconds = 0;
   gchar *years_str = NULL;
   gchar *months_str = NULL;
   gchar *days_str = NULL;
@@ -497,27 +448,27 @@ gdu_utils_format_duration_msec (guint64 msec)
   gchar *milliseconds_str = NULL;
   guint64 t;
 
-  t = msec;
+  t = usec;
 
-  years  = floor (t / MSEC_PER_YEAR);
-  t -= years * MSEC_PER_YEAR;
+  years  = floor (t / USEC_PER_YEAR);
+  t -= years * USEC_PER_YEAR;
 
-  months = floor (t / MSEC_PER_MONTH);
-  t -= months * MSEC_PER_MONTH;
+  months = floor (t / USEC_PER_MONTH);
+  t -= months * USEC_PER_MONTH;
 
-  days = floor (t / MSEC_PER_DAY);
-  t -= days * MSEC_PER_DAY;
+  days = floor (t / USEC_PER_DAY);
+  t -= days * USEC_PER_DAY;
 
-  hours = floor (t / MSEC_PER_HOUR);
-  t -= hours * MSEC_PER_HOUR;
+  hours = floor (t / USEC_PER_HOUR);
+  t -= hours * USEC_PER_HOUR;
 
-  minutes = floor (t / MSEC_PER_MINUTE);
-  t -= minutes * MSEC_PER_MINUTE;
+  minutes = floor (t / USEC_PER_MINUTE);
+  t -= minutes * USEC_PER_MINUTE;
 
-  seconds = floor (t / MSEC_PER_SECOND);
-  t -= seconds * MSEC_PER_SECOND;
+  seconds = floor (t / G_USEC_PER_SEC);
+  t -= seconds * G_USEC_PER_SEC;
 
-  milliseconds = floor (t);
+  milliseconds = floor (t / MSEC_PER_USEC);
 
   years_str = years_to_string (years);
   months_str = months_to_string (months);
@@ -552,7 +503,7 @@ gdu_utils_format_duration_msec (guint64 msec)
       /* Translators: Used for durations less than one hour but greater than one minute. First %s is number of minutes, second %s is seconds */
       ret = g_strdup_printf (C_("duration-minute-to-hour", "%s and %s"), minutes_str, seconds_str);
     }
-  else if (seconds > 0)
+  else if (seconds > 0 || !(flags & GDU_FORMAT_DURATION_FLAGS_SUBSECOND_PRECISION))
     {
       /* Translators: Used for durations less than one minute byte greater than one second. First %s is number of seconds */
       ret = g_strdup_printf (C_("duration-second-to-minute", "%s"), seconds_str);
diff --git a/src/disks/gduutils.h b/src/disks/gduutils.h
index fe85949..4783e92 100644
--- a/src/disks/gduutils.h
+++ b/src/disks/gduutils.h
@@ -34,9 +34,6 @@ gboolean gdu_utils_has_configuration (UDisksBlock  *block,
 
 void gdu_utils_configure_file_chooser_for_disk_images (GtkFileChooser *file_chooser);
 
-gchar *gdu_utils_duration_to_string (guint    duration_sec,
-                                     gboolean include_second_precision);
-
 GtkWidget *gdu_utils_create_info_bar (GtkMessageType  message_type,
                                       const gchar    *markup,
                                       GtkWidget     **out_label);
@@ -57,7 +54,8 @@ void gdu_options_update_entry_option (GtkWidget       *options_entry,
 
 const gchar *gdu_utils_get_seat (void);
 
-gchar *gdu_utils_format_duration_msec (guint64 msec);
+gchar *gdu_utils_format_duration_usec (guint64                usec,
+                                       GduFormatDurationFlags flags);
 
 G_END_DECLS
 
diff --git a/src/disks/gduwindow.c b/src/disks/gduwindow.c
index 2d26de0..24f7025 100644
--- a/src/disks/gduwindow.c
+++ b/src/disks/gduwindow.c
@@ -1713,7 +1713,7 @@ get_job_progress_text (GduWindow *window,
       gchar *s2, *s3;
 
       usec_left = expected_end_time_usec - g_get_real_time ();
-      s2 = gdu_utils_duration_to_string (usec_left / G_USEC_PER_SEC, FALSE);
+      s2 = gdu_utils_format_duration_usec (usec_left, GDU_FORMAT_DURATION_FLAGS_NONE);
       s3 = g_strdup_printf (C_("job-remaining", "%s remaining"), s2);
       s = g_strdup_printf ("<small>%s â %s</small>", desc, s3);
       g_free (s2);



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