summaryrefslogtreecommitdiffstats
path: root/mpvcore
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2013-03-25 20:32:01 +0100
committerwm4 <wm4@nowhere>2013-09-15 02:51:17 +0200
commit58cff195e7d17b348d9ddf5e472420063a9717cc (patch)
tree516a4e3dce86bdf7ccfa01b3ec4b5ea79c7dfe22 /mpvcore
parenta87298e5015f083b6ea294f578dc0d54e1f0f88f (diff)
downloadmpv-58cff195e7d17b348d9ddf5e472420063a9717cc.tar.bz2
mpv-58cff195e7d17b348d9ddf5e472420063a9717cc.tar.xz
mp_common: add function which accepts a format string to format playback time
Diffstat (limited to 'mpvcore')
-rw-r--r--mpvcore/mp_common.c60
-rw-r--r--mpvcore/mp_common.h1
2 files changed, 50 insertions, 11 deletions
diff --git a/mpvcore/mp_common.c b/mpvcore/mp_common.c
index 3ea6e66531..d4947f442f 100644
--- a/mpvcore/mp_common.c
+++ b/mpvcore/mp_common.c
@@ -22,27 +22,65 @@
#include "mpvcore/bstr.h"
#include "mpvcore/mp_common.h"
-char *mp_format_time(double time, bool fractions)
+#define appendf(ptr, ...) \
+ do {(*(ptr)) = talloc_asprintf_append_buffer(*(ptr), __VA_ARGS__);} while(0)
+
+// Return a talloc'ed string formatted according to the format string in fmt.
+// On error, return NULL.
+// Valid formats:
+// %H, %h: hour (%H is padded with 0 to two digits)
+// %M: minutes from 00-59 (hours are subtracted)
+// %m: total minutes (includes hours, unlike %M)
+// %S: seconds from 00-59 (minutes and hours are subtracted)
+// %s: total seconds (includes hours and minutes)
+// %f: like %s, but as float
+// %T: milliseconds (000-999)
+char *mp_format_time_fmt(const char *fmt, double time)
{
if (time == MP_NOPTS_VALUE)
return talloc_strdup(NULL, "unknown");
- char sign[2] = {0};
- if (time < 0) {
- time = -time;
- sign[0] = '-';
- }
+ char *sign = time < 0 ? "-" : "";
+ time = time < 0 ? -time : time;
long long int itime = time;
- long long int h, m, s;
+ long long int h, m, tm, s;
+ int ms;
s = itime;
+ tm = s / 60;
h = s / 3600;
s -= h * 3600;
m = s / 60;
s -= m * 60;
- char *res = talloc_asprintf(NULL, "%s%02lld:%02lld:%02lld", sign, h, m, s);
- if (fractions)
- res = talloc_asprintf_append(res, ".%03d",
- (int)((time - itime) * 1000));
+ ms = (time - itime) * 1000;
+ char *res = talloc_strdup(NULL, "");
+ while (*fmt) {
+ if (fmt[0] == '%') {
+ fmt++;
+ switch (fmt[0]) {
+ case 'h': appendf(&res, "%s%lld", sign, h); break;
+ case 'H': appendf(&res, "%s%02lld", sign, h); break;
+ case 'm': appendf(&res, "%s%lld", sign, tm); break;
+ case 'M': appendf(&res, "%02lld", m); break;
+ case 's': appendf(&res, "%s%lld", sign, itime); break;
+ case 'S': appendf(&res, "%02lld", s); break;
+ case 'T': appendf(&res, "%03d", ms); break;
+ case '%': appendf(&res, "%s", "%"); break;
+ default: goto error;
+ }
+ fmt++;
+ } else {
+ appendf(&res, "%c", *fmt);
+ fmt++;
+ }
+ }
return res;
+error:
+ talloc_free(res);
+ return NULL;
+}
+
+char *mp_format_time(double time, bool fractions)
+{
+ return mp_format_time_fmt(fractions ? "%H:%M:%S.%T" : "%H:%M:%S", time);
}
// Set rc to the union of rc and rc2
diff --git a/mpvcore/mp_common.h b/mpvcore/mp_common.h
index 9a719ae8ab..651136d2cb 100644
--- a/mpvcore/mp_common.h
+++ b/mpvcore/mp_common.h
@@ -49,6 +49,7 @@ extern const char *mplayer_version;
extern const char *mplayer_builddate;
char *mp_format_time(double time, bool fractions);
+char *mp_format_time_fmt(const char *fmt, double time);
struct mp_rect {
int x0, y0;