From 74b66862d776f81a6da374b7e6cc54d2d5e8e16e Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 2 Feb 2013 20:32:59 +0100 Subject: mp_common: improve OSD/status time formatting Allow negative times. Timestamps can be negative, and we actually display negative time for other reasons too, such as when waiting for the old audio to drain with gapless audio.) Avoid overflows with relatively large time values. (We still don't handle values too large for int64_t.) --- core/mp_common.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'core') diff --git a/core/mp_common.c b/core/mp_common.c index c931c29065..4fef9c37dd 100644 --- a/core/mp_common.c +++ b/core/mp_common.c @@ -23,17 +23,24 @@ char *mp_format_time(double time, bool fractions) { - if (time < 0) + if (time == MP_NOPTS_VALUE) return talloc_strdup(NULL, "unknown"); - int h, m, s = time; + char sign[2] = {0}; + if (time < 0) { + time = -time; + sign[0] = '-'; + } + long long int itime = time; + int64_t h, m, s; + s = itime; h = s / 3600; s -= h * 3600; m = s / 60; s -= m * 60; - char *res = talloc_asprintf(NULL, "%02d:%02d:%02d", h, m, s); + char *res = talloc_asprintf(NULL, "%s%02lld:%02lld:%02lld", sign, h, m, s); if (fractions) res = talloc_asprintf_append(res, ".%03d", - (int)((time - (int)time) * 1000)); + (int)((time - itime) * 1000)); return res; } -- cgit v1.2.3