[totem/wip/hadess/precise-stepping: 8/10] gst: Add support for showing msecs in time label




commit 3281b72b440f90726396611acb75e17bac489740
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Feb 17 21:48:19 2022 +0100

    gst: Add support for showing msecs in time label

 src/gst/totem-time-helpers.c | 102 +++++++++++++++++++++++++++++++------------
 src/gst/totem-time-helpers.h |   1 +
 src/test-totem.c             |   4 ++
 3 files changed, 80 insertions(+), 27 deletions(-)
---
diff --git a/src/gst/totem-time-helpers.c b/src/gst/totem-time-helpers.c
index c6b3fb507..f2f526ab7 100644
--- a/src/gst/totem-time-helpers.c
+++ b/src/gst/totem-time-helpers.c
@@ -37,8 +37,8 @@ char *
 totem_time_to_string (gint64        msecs,
                      TotemTimeFlag flags)
 {
-       int sec, min, hour, _time;
-       double time_f;
+       gint64 _time;
+       int msec, sec, min, hour;
 
        if (msecs < 0) {
                /* translators: Unknown time */
@@ -48,12 +48,20 @@ totem_time_to_string (gint64        msecs,
        /* When calculating the remaining time,
         * we want to make sure that:
         * current time + time remaining = total run time */
-       time_f = (double) msecs / 1000;
-       if (flags & TOTEM_TIME_FLAG_REMAINING)
-               time_f = ceil (time_f);
-       else
-               time_f = round (time_f);
-       _time = (int) time_f;
+       msec = msecs % 1000;
+       if (flags & TOTEM_TIME_FLAG_MSECS) {
+               _time = msecs - msec;
+               _time = _time / 1000;
+       } else {
+               double time_f;
+
+               time_f = (double) msecs / 1000;
+               if (flags & TOTEM_TIME_FLAG_REMAINING)
+                       time_f = ceil (time_f);
+               else
+                       time_f = round (time_f);
+               _time = (gint64) time_f;
+       }
 
        sec = _time % 60;
        _time = _time - sec;
@@ -63,32 +71,72 @@ totem_time_to_string (gint64        msecs,
 
        if (hour > 0 || flags & TOTEM_TIME_FLAG_FORCE_HOUR) {
                if (!(flags & TOTEM_TIME_FLAG_REMAINING)) {
-                       /* hour:minutes:seconds */
-                       /* Translators: This is a time format, like "9:05:02" for 9
-                        * hours, 5 minutes, and 2 seconds. You may change ":" to
-                        * the separator that your locale uses or use "%Id" instead
-                        * of "%d" if your locale uses localized digits.
+                       if (!(flags & TOTEM_TIME_FLAG_MSECS)) {
+                               /* hour:minutes:seconds */
+                               /* Translators: This is a time format, like "9:05:02" for 9
+                                * hours, 5 minutes, and 2 seconds. You may change ":" to
+                                * the separator that your locale uses or use "%Id" instead
+                                * of "%d" if your locale uses localized digits.
+                                */
+                               return g_strdup_printf (C_("long time format", "%d:%02d:%02d"), hour, min, 
sec);
+                       } else {
+                               /* hour:minutes:seconds.msecs */
+                               /* Translators: This is a time format, like "9:05:02.050" for 9
+                                * hours, 5 minutes, 2 seconds and 50 milliseconds. You may change ":" to
+                                * the separator that your locale uses or use "%Id" instead
+                                * of "%d" if your locale uses localized digits.
+                                */
+                               return g_strdup_printf (C_("long time format", "%d:%02d:%02d.%03d"), hour, 
min, sec, msec);
+                       }
+               } else {
+                       if (!(flags & TOTEM_TIME_FLAG_MSECS)) {
+                               /* -hour:minutes:seconds */
+                               /* Translators: This is a time format, like "-9:05:02" for 9
+                                * hours, 5 minutes, and 2 seconds playback remaining. You may
+                                * change ":" to the separator that your locale uses or use
+                                * "%Id" instead of "%d" if your locale uses localized digits.
+                                */
+                               return g_strdup_printf (C_("long time format", "-%d:%02d:%02d"), hour, min, 
sec);
+                       } else {
+                               /* -hour:minutes:seconds.msecs */
+                               /* Translators: This is a time format, like "-9:05:02.050" for 9
+                                * hours, 5 minutes, 2 seconds and 50 milliseconds playback remaining. You may
+                                * change ":" to the separator that your locale uses or use
+                                * "%Id" instead of "%d" if your locale uses localized digits.
+                                */
+                               return g_strdup_printf (C_("long time format", "-%d:%02d:%02d.%03d"), hour, 
min, sec, msec);
+                       }
+               }
+       }
+
+       if (flags & TOTEM_TIME_FLAG_REMAINING) {
+               if (!(flags & TOTEM_TIME_FLAG_MSECS)) {
+                       /* -minutes:seconds */
+                       /* Translators: This is a time format, like "-5:02" for 5
+                        * minutes and 2 seconds playback remaining. You may change
+                        * ":" to the separator that your locale uses or use "%Id"
+                        * instead of "%d" if your locale uses localized digits.
                         */
-                       return g_strdup_printf (C_("long time format", "%d:%02d:%02d"), hour, min, sec);
+                       return g_strdup_printf (C_("short time format", "-%d:%02d"), min, sec);
                } else {
-                       /* -hour:minutes:seconds */
-                       /* Translators: This is a time format, like "-9:05:02" for 9
-                        * hours, 5 minutes, and 2 seconds playback remaining. You may
-                        * change ":" to the separator that your locale uses or use
-                        * "%Id" instead of "%d" if your locale uses localized digits.
+                       /* -minutes:seconds.msec */
+                       /* Translators: This is a time format, like "-5:02.050" for 5
+                        * minutes 2 seconds and 50 milliseconds playback remaining. You may change
+                        * ":" to the separator that your locale uses or use "%Id"
+                        * instead of "%d" if your locale uses localized digits.
                         */
-                       return g_strdup_printf (C_("long time format", "-%d:%02d:%02d"), hour, min, sec);
+                       return g_strdup_printf (C_("short time format", "-%d:%02d.%03d"), min, sec, msec);
                }
        }
 
-       if (flags & TOTEM_TIME_FLAG_REMAINING) {
-               /* -minutes:seconds */
-               /* Translators: This is a time format, like "-5:02" for 5
-                * minutes and 2 seconds playback remaining. You may change
-                * ":" to the separator that your locale uses or use "%Id"
-                * instead of "%d" if your locale uses localized digits.
+       if (flags & TOTEM_TIME_FLAG_MSECS) {
+               /* minutes:seconds.msec */
+               /* Translators: This is a time format, like "5:02" for 5
+                * minutes 2 seconds and 50 milliseconds. You may change ":" to the
+                * separator that your locale uses or use "%Id" instead of
+                * "%d" if your locale uses localized digits.
                 */
-               return g_strdup_printf (C_("short time format", "-%d:%02d"), min, sec);
+               return g_strdup_printf (C_("short time format", "%d:%02d.%03d"), min, sec, msec);
        }
 
        /* minutes:seconds */
diff --git a/src/gst/totem-time-helpers.h b/src/gst/totem-time-helpers.h
index 8ed34bc9f..d43d64d5c 100644
--- a/src/gst/totem-time-helpers.h
+++ b/src/gst/totem-time-helpers.h
@@ -34,6 +34,7 @@ typedef enum {
        TOTEM_TIME_FLAG_NONE            = 0,
        TOTEM_TIME_FLAG_REMAINING       = 1 << 0,
        TOTEM_TIME_FLAG_FORCE_HOUR      = 1 << 2,
+       TOTEM_TIME_FLAG_MSECS           = 1 << 3,
 } TotemTimeFlag;
 
 char *totem_time_to_string (gint64        msecs,
diff --git a/src/test-totem.c b/src/test-totem.c
index e055b25ff..7c2ec1f02 100644
--- a/src/test-totem.c
+++ b/src/test-totem.c
@@ -165,6 +165,10 @@ test_time_label (void)
        str = totem_time_to_string (1250, TOTEM_TIME_FLAG_REMAINING);
        g_assert_cmpstr (str, ==, "-0:02");
        g_free (str);
+
+       str = totem_time_to_string (1250, TOTEM_TIME_FLAG_MSECS);
+       g_assert_cmpstr (str, ==, "0:01.250");
+       g_free (str);
 }
 
 int main (int argc, char **argv)


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