RelativeTime toString addition (with code)

Hi there,

in a discussion on discord there was the question raised to convert seconds into a SMPTE string, so i came up with an addition for RelativeTime:

String RelativeTime::toString (bool includeDays) const
{
    String result;
    result.preallocateBytes (32);

    if (numSeconds < 0)
        result << '-';

    bool empty = true;
    if (includeDays) {
        auto days = (int)inDays();
        if (days > 0) {
            result << String (days) << ' ';
            empty = false;
        }
    }

    result << String ((int)inHours() % 24).paddedLeft ('0', empty ? 1 : 2) << ':';
    result << String ((int)inMinutes() % 60).paddedLeft ('0', 2) << ':';
    result << String ((int)inSeconds() % 60).paddedLeft ('0', 2);
    auto millis = (int)inMilliseconds() % 1000;
    if (millis > 0)
        result << '.' << String (millis).paddedLeft ('0', 3);

    return result.trimEnd();
}

Pretty straight forward. Maybe it’s useful for others as well, so it would be cool if that could be added…

Cheers,
Daniel

You’d have to define the frame rate (and drop frame) if you’re really trying to work with SMPTE. I have a SMPTE class I wrote about 20 years ago.

Rail

You are right, I thought I wrote “something similar to SMPTE”, but that’s what I wrote in the chat (or just wanted to write… :wink: ). Sure, this is a video unaware version of it.
Would be great to complete it though, but the frame rate info will not always be present.
It could be added as parameter though…

Thanks for the correction