From 560d1c9a16a3bcbf8234bf297ec36e8a6e7bf453 Mon Sep 17 00:00:00 2001 From: Jesper Jensen Date: Fri, 20 Feb 2026 23:42:09 +0100 Subject: Fancy new UI --- src/mytime.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'src/mytime.c') diff --git a/src/mytime.c b/src/mytime.c index ffb7323..e71259b 100644 --- a/src/mytime.c +++ b/src/mytime.c @@ -1,6 +1,8 @@ #include "mytime.h" #include +#include +#include #include #include @@ -36,3 +38,58 @@ void progress_time(uint64_t diff) { // Right now the diff is only seconds resolution global_time.tv_sec += diff; } + +struct timespec time_from_double(double t) { + double fraction; + double integral; + fraction = modf(t, &integral); + return (struct timespec) { + .tv_sec = integral, + .tv_nsec = fraction * 1000000000.0, + }; +} + +struct formatted_time format_time(struct timespec time) { + struct formatted_time res = {0}; + + struct tm tm; + gmtime_r(&time.tv_sec, &tm); + if(strftime(res.buf, 32, "%Y-%m-%d %H:%M:%S", &tm) >= 32) { + abort(); + } + + return res; +} + +struct formatted_time format_duration(struct timespec time) { + struct formatted_time res = {0}; + + int hours = time.tv_sec / 3600; + int mins = (time.tv_sec % 3600) / 60; + int secs = time.tv_sec % 60; + + if (hours > 0) { + snprintf(res.buf, 32, "%dh %02dm %02ds", hours, mins, secs); + } else if (mins > 0) { + snprintf(res.buf, 32, "%dm %02ds", mins, secs); + } else { + snprintf(res.buf, 32, "%ds", secs); + } + + + return res; +} + +struct timespec time_sub(const struct timespec *t1, const struct timespec *t2) { + assert(t1); + assert(t2); + struct timespec diff = { + .tv_sec = t1->tv_sec - t2->tv_sec, + .tv_nsec = t1->tv_nsec - t2->tv_nsec + }; + if (diff.tv_nsec < 0) { + diff.tv_nsec += 1000000000; + diff.tv_sec--; + } + return diff; +} -- cgit v1.2.3