From 66b6a807227036a256634cfc42f0ecc4ce51d83d Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Mon, 4 Aug 2008 13:49:41 +0100 Subject: [PATCH] print a better seconds/minutes/hours text -- needs localising properly --- libdevkit-power/dkp-object.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/libdevkit-power/dkp-object.c b/libdevkit-power/dkp-object.c index c76e0f6..b27ab72 100644 --- a/libdevkit-power/dkp-object.c +++ b/libdevkit-power/dkp-object.c @@ -207,6 +207,25 @@ dkp_strzero (const gchar *text) return FALSE; } +/** + * dkp_object_seconds_to_time: + **/ +static gchar * +dkp_object_seconds_to_time (guint seconds) +{ + gfloat value = seconds; + if (value < 60) + return g_strdup_printf ("%.0f seconds", value); + value /= 60.0; + if (value < 60) + return g_strdup_printf ("%.1f minutes", value); + value /= 60.0; + if (value < 60) + return g_strdup_printf ("%.1f hours", value); + value /= 24.0; + return g_strdup_printf ("%.1f days", value); +} + /** * dkp_object_print: **/ @@ -217,6 +236,7 @@ dkp_object_print (const DkpObject *obj) struct tm *time_tm; time_t t; gchar time_buf[256]; + gchar *time_str; /* get a human readable time */ t = (time_t) obj->update_time; @@ -243,14 +263,18 @@ dkp_object_print (const DkpObject *obj) g_print (" energy-full-design: %g Wh\n", obj->battery_energy_full_design); g_print (" energy-rate: %g W\n", obj->battery_energy_rate); g_print (" time to full: "); - if (obj->battery_time_to_full >= 0) - g_print ("%d seconds\n", (int) obj->battery_time_to_full); - else + if (obj->battery_time_to_full >= 0) { + time_str = dkp_object_seconds_to_time (obj->battery_time_to_full); + g_print ("%s\n", time_str); + g_free (time_str); + } else g_print ("unknown\n"); g_print (" time to empty: "); - if (obj->battery_time_to_empty >= 0) - g_print ("%d seconds\n", (int) obj->battery_time_to_empty); - else + if (obj->battery_time_to_empty >= 0) { + time_str = dkp_object_seconds_to_time (obj->battery_time_to_empty); + g_print ("%s\n", time_str); + g_free (time_str); + } else g_print ("unknown\n"); g_print (" percentage: %g%%\n", obj->battery_percentage); g_print (" capacity: %g%%\n", obj->battery_capacity);