Skip to content

Commit

Permalink
Consolidate formatting of time strings.
Browse files Browse the repository at this point in the history
This started as a task to eliminate the obsolete QString::sprintf
function, and evolved into a task to consolidate the formatting of
tine strings through out the code.  There are now two functions to
call to get a formatted time.  The only difference is whether you pass
a time in seconds or in milliseconds.  The formatting argument to
these functions is the same as for the QTime::toString formatting
argument.
  • Loading branch information
linuxdude42 committed Apr 16, 2020
1 parent 9d1d257 commit c47ae6d
Show file tree
Hide file tree
Showing 21 changed files with 120 additions and 238 deletions.
11 changes: 2 additions & 9 deletions mythplugins/mytharchive/mytharchive/thumbfinder.cpp
Expand Up @@ -402,19 +402,12 @@ void ThumbFinder::updateThumb(void)

QString ThumbFinder::frameToTime(int64_t frame, bool addFrame) const
{
QString str;

int sec = (int) (frame / m_fps);
frame = frame - (int) (sec * m_fps);
int min = sec / 60;
sec %= 60;
int hour = min / 60;
min %= 60;

QString str = MythFormatTime(sec, "HH:mm:ss");
if (addFrame)
str = str.sprintf("%01d:%02d:%02d.%02d", hour, min, sec, (int) frame);
else
str = str.sprintf("%02d:%02d:%02d", hour, min, sec);
str += QString(".%1").arg(frame,10,2,QChar('0'));
return str;
}

Expand Down
10 changes: 3 additions & 7 deletions mythplugins/mythmusic/mythmusic/cdrip.cpp
Expand Up @@ -30,6 +30,7 @@ using namespace std;
#include <lcddevice.h>
#include <mythmediamonitor.h>
#include <mythdirs.h>
#include <mythmiscutil.h>

// MythUI
#include <mythdialogbox.h>
Expand Down Expand Up @@ -1221,14 +1222,9 @@ void Ripper::updateTrackList(void)
item->SetText(metadata->Title(), "title");
item->SetText(metadata->Artist(), "artist");

int length = track->length / 1000;
if (length > 0)
if (track->length >= ONESECINMS)
{
int min = length / 60;
int sec = length % 60;
QString s;
s.sprintf("%02d:%02d", min, sec);
item->SetText(s, "length");
item->SetText(MythFormatTimeMs(track->length, "mm:ss"), "length");
}
else
item->SetText("", "length");
Expand Down
33 changes: 4 additions & 29 deletions mythplugins/mythmusic/mythmusic/musiccommon.cpp
Expand Up @@ -2154,37 +2154,12 @@ void MusicCommon::updatePlaylistStats(void)

QString MusicCommon::getTimeString(int exTime, int maxTime)
{
QString time_string;

int eh = exTime / 3600;
int em = (exTime / 60) % 60;
int es = exTime % 60;

int maxh = maxTime / 3600;
int maxm = (maxTime / 60) % 60;
int maxs = maxTime % 60;

if (maxTime <= 0)
{
if (eh > 0)
time_string.sprintf("%d:%02d:%02d", eh, em, es);
else
time_string.sprintf("%02d:%02d", em, es);
}
else
{
if (maxh > 0)
{
time_string.sprintf("%d:%02d:%02d / %02d:%02d:%02d", eh, em,
es, maxh, maxm, maxs);
}
else
{
time_string.sprintf("%02d:%02d / %02d:%02d", em, es, maxm, maxs);
}
}
return MythFormatTime(exTime,
(exTime >= ONEHOURINSEC) ? "H:mm:ss" : "mm:ss");

return time_string;
QString fmt = (maxTime >= ONEHOURINSEC) ? "H:mm:ss" : "mm:ss";
return MythFormatTime(exTime, fmt) + " / " + MythFormatTime(maxTime, fmt);

This comment has been minimized.

Copy link
@ijc

ijc Mar 6, 2022

Contributor

@linuxdude42 This causes a regression if the time has >=24h in it (e.g. I have some playlists where the total time is more than 1 day)

This is because under the hood MythFormatTime (now MythDate::formatTime) uses QTime::toString which says that H is:

The hour without a leading zero (0 to 23, even with AM/PM display)

The previous code would happily return 24:mm:ss or even 234:mm:ss, now it returns the empty string (so depending on how far along you are in a long playlist you either get / or H:mm:ss / ).

I can't see how to return to the old behaviour when using QTime under the hood. It doesn't look like QDateTime would do what we need either.

We could manually extract the number of days and prepend it, either here or in MythDate::formatTime?

Any better ideas?

This comment has been minimized.

Copy link
@ijc

ijc Mar 12, 2022

Contributor

FYI #520

}

void MusicCommon::searchButtonList(void)
Expand Down
10 changes: 10 additions & 0 deletions mythtv/libs/libmythbase/mythmiscutil.cpp
Expand Up @@ -1212,4 +1212,14 @@ int naturalCompare(const QString &_a, const QString &_b, Qt::CaseSensitivity cas
return currA->isNull() ? -1 : + 1;
}

QString MythFormatTimeMs(int msecs, QString fmt)
{
return QTime::fromMSecsSinceStartOfDay(msecs).toString(fmt);
}

QString MythFormatTime(int secs, QString fmt)
{
return QTime::fromMSecsSinceStartOfDay(secs*1000).toString(fmt);
}

/* vim: set expandtab tabstop=4 shiftwidth=4: */
32 changes: 32 additions & 0 deletions mythtv/libs/libmythbase/mythmiscutil.h
Expand Up @@ -86,6 +86,38 @@ MBASE_PUBLIC void setHttpProxy(void);
MBASE_PUBLIC int naturalCompare(const QString &_a, const QString &_b,
Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive);

#define ONESECINMS ( 1000)
#define ONEMININMS ( 60*1000)
#define ONEHOURINMS (60*60*1000)
#define ONEMININSEC ( 60)
#define ONEHOURINSEC (60*60)

/**
* \brief Format a milliseconds time value
*
* Convert a millisecond time value into a textual representation of the value.
*
* \param msecs The time value in milliseconds.
*
* \param fmt A formatting string specifying how to output the time.
* See QTime::toString for the a definition fo valid formatting
* characters.
*/
MBASE_PUBLIC QString MythFormatTimeMs(int msecs, QString fmt);

/**
* \brief Format a seconds time value
*
* Convert a second time value into a textual representation of the value.
*
* \param secs The time value in seconds.
*
* \param mft A formatting string specifying how to output the time.
* See QTime::toString for the a definition fo valid formatting
* characters.
*/
MBASE_PUBLIC QString MythFormatTime(int secs, QString fmt);

// CPU Tick timing function
#ifdef MMX
#ifdef _WIN32
Expand Down
13 changes: 4 additions & 9 deletions mythtv/libs/libmythmetadata/lyricsdata.h
Expand Up @@ -12,6 +12,7 @@
#include "mythmetaexp.h"
#include "mythcorecontext.h"
#include "musicmetadata.h"
#include "mythmiscutil.h"

class LyricsData;

Expand All @@ -36,15 +37,9 @@ class META_PUBLIC LyricsLine
private:
QString formatTime(void) const
{
QString res;
int minutes = m_time / (1000 * 60);
int seconds = m_time % (1000 * 60) / 1000;
int hundredths = (m_time % 1000) / 10;

return QString("[%1:%2.%3]")
.arg(minutes, 2,10,QChar('0'))
.arg(seconds, 2,10,QChar('0'))
.arg(hundredths, 2,10,QChar('0'));
QString timestr = MythFormatTimeMs(m_time,"mm:ss.zzz");
timestr.chop(1); // Chop 1 to return hundredths
return QString("[%1]").arg(timestr);
}
};

Expand Down
20 changes: 3 additions & 17 deletions mythtv/libs/libmythmetadata/musicmetadata.cpp
Expand Up @@ -22,6 +22,7 @@
#include "storagegroup.h"
#include "mythsystem.h"
#include "mythcoreutil.h"
#include "mythmiscutil.h"

// mythbase
#include "mythsorthelper.h"
Expand Down Expand Up @@ -1092,23 +1093,8 @@ void MusicMetadata::toMap(InfoMap &metadataMap, const QString &prefix)
metadataMap[prefix + "genre"] = m_genre;
metadataMap[prefix + "year"] = (m_year > 0 ? QString("%1").arg(m_year) : "");

int len = m_length / 1000;
int eh = len / 3600;
int em = (len / 60) % 60;
int es = len % 60;
if (eh > 0)
{
metadataMap[prefix + "length"] = QString("%1:%2:%3")
.arg(eh,1,10)
.arg(em,2,10,QChar('0'))
.arg(es,2,10,QChar('0'));
}
else
{
metadataMap[prefix + "length"] = QString("%1:%2")
.arg(em,2,10,QChar('0'))
.arg(es,2,10,QChar('0'));
}
QString fmt = (m_length >= ONEHOURINMS) ? "H:mm:ss" : "mm:ss";
metadataMap[prefix + "length"] = MythFormatTimeMs(m_length, fmt);

if (m_lastPlay.isValid())
{
Expand Down
30 changes: 15 additions & 15 deletions mythtv/libs/libmythtv/Bluray/mythbdbuffer.cpp
Expand Up @@ -11,6 +11,7 @@
#include "mythlogging.h"
#include "mythcorecontext.h"
#include "mythlocale.h"
#include "mythmiscutil.h"
#include "mythdirs.h"
#include "libbluray/bluray.h"
#include "io/mythiowrapper.h"
Expand Down Expand Up @@ -560,6 +561,14 @@ uint32_t MythBDBuffer::GetCurrentChapter(void)
return 0;
}

uint64_t MythBDBuffer::GetChapterStartTimeMs(uint32_t Chapter)
{
if (Chapter >= GetNumChapters())
return 0;
QMutexLocker locker(&m_infoLock);
return m_currentTitleInfo->chapters[Chapter].start / 90;
}

uint64_t MythBDBuffer::GetChapterStartTime(uint32_t Chapter)
{
if (Chapter >= GetNumChapters())
Expand Down Expand Up @@ -706,14 +715,9 @@ bool MythBDBuffer::UpdateTitleInfo(void)
m_timeDiff = 0;
m_titlesize = bd_get_title_size(m_bdnav);
uint32_t chapter_count = GetNumChapters();
uint64_t total_secs = m_currentTitleLength / 90000;
int hours = static_cast<int>(total_secs / 60 / 60);
int minutes = static_cast<int>((total_secs / 60) - (static_cast<uint64_t>(hours * 60)));
double secs = static_cast<double>(total_secs) - static_cast<double>(hours * 60 * 60 + minutes * 60);
QString duration = QString("%1:%2:%3")
.arg(hours, 2, 10, QChar('0'))
.arg(minutes, 2, 10, QChar('0'))
.arg(secs, 2, 'f', 1, QChar('0'));
uint64_t total_msecs = m_currentTitleLength / 90;
QString duration = MythFormatTimeMs(total_msecs, "HH:mm:ss.zzz");
duration.chop(2); // Chop 2 to show tenths
LOG(VB_GENERAL, LOG_INFO, LOC + QString("New title info: Index %1 Playlist: %2 Duration: %3 ""Chapters: %5")
.arg(m_currentTitle).arg(m_currentTitleInfo->playlist).arg(duration).arg(chapter_count));
LOG(VB_GENERAL, LOG_INFO, LOC + QString("New title info: Clips: %1 Angles: %2 Title Size: %3 Frame Rate %4")
Expand All @@ -723,13 +727,9 @@ bool MythBDBuffer::UpdateTitleInfo(void)
for (uint i = 0; i < chapter_count; i++)
{
uint64_t framenum = GetChapterStartFrame(i);
total_secs = GetChapterStartTime(i);
hours = static_cast<int>(total_secs / 60 / 60);
minutes = static_cast<int>((total_secs / 60) - (static_cast<uint64_t>(hours * 60)));
secs = static_cast<double>(total_secs) - static_cast<double>(hours * 60 * 60 + minutes * 60);
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Chapter %1 found @ [%2:%3:%4]->%5")
.arg(i + 1, 2, 10, QChar('0')).arg(hours, 2, 10, QChar('0'))
.arg(minutes, 2, 10, QChar('0')).arg(secs, 6, 'f', 3, QChar('0'))
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Chapter %1 found @ [%2]->%3")
.arg(i + 1, 2, 10, QChar('0'))
.arg(MythFormatTimeMs(GetChapterStartTimeMs(i), "HH:mm:ss.zzz"))
.arg(framenum));
}

Expand Down
1 change: 1 addition & 0 deletions mythtv/libs/libmythtv/Bluray/mythbdbuffer.h
Expand Up @@ -56,6 +56,7 @@ class MTV_PUBLIC MythBDBuffer : public MythOpticalBuffer
uint32_t GetNumChapters (void);
uint32_t GetCurrentChapter (void);
uint64_t GetNumAngles (void) const;
uint64_t GetChapterStartTimeMs(uint32_t Chapter);
uint64_t GetChapterStartTime (uint32_t Chapter);
uint64_t GetChapterStartFrame (uint32_t Chapter);
bool IsHDMVNavigation (void) const;
Expand Down
9 changes: 2 additions & 7 deletions mythtv/libs/libmythtv/Bluray/mythbdplayer.cpp
Expand Up @@ -239,14 +239,9 @@ QString MythBDPlayer::GetTitleName(int Title) const
{
if (Title >= 0 && Title < GetNumTitles())
{
int secs = GetTitleDuration(Title);
// BD doesn't provide title names, so show title number and duration
int hours = secs / 60 / 60;
int minutes = (secs / 60) - (hours * 60);
secs = secs % 60;
QString name = QString("%1 (%2:%3:%4)").arg(Title+1)
.arg(hours, 2, 10, QChar(48)).arg(minutes, 2, 10, QChar(48))
.arg(secs, 2, 10, QChar(48));
QString timestr = MythFormatTime(GetTitleDuration(Title), "HH:mm:ss");
QString name = QString("%1 (%2)").arg(Title+1).arg(timestr);
return name;
}
return QString();
Expand Down
18 changes: 2 additions & 16 deletions mythtv/libs/libmythtv/captions/srtwriter.cpp
Expand Up @@ -25,20 +25,6 @@ void SRTWriter::AddSubtitle(const OneSubtitle &sub, int number)
*/
QString SRTWriter::FormatTime(uint64_t time_in_msec)
{
uint64_t msec = time_in_msec % 1000;
time_in_msec /= 1000;

uint64_t ss = time_in_msec % 60;
time_in_msec /= 60;

uint64_t mm = time_in_msec % 60;
time_in_msec /= 60;

uint64_t hh = time_in_msec;

return QString("%1:%2:%3,%4")
.arg(hh,2,10,QChar('0'))
.arg(mm,2,10,QChar('0'))
.arg(ss,2,10,QChar('0'))
.arg(msec,3,10,QChar('0'));
QTime time = QTime::fromMSecsSinceStartOfDay(time_in_msec);
return time.toString("HH:mm:ss,zzz");
}
1 change: 1 addition & 0 deletions mythtv/libs/libmythtv/captions/srtwriter.h
Expand Up @@ -6,6 +6,7 @@
#include <QImage>
#include <QPoint>
#include <QHash>
#include <QTime>

#include <QtXml/QDomDocument>
#include <QtXml/QDomElement>
Expand Down
9 changes: 3 additions & 6 deletions mythtv/libs/libmythtv/commbreakmap.cpp
Expand Up @@ -2,6 +2,7 @@
#include "commbreakmap.h"

#include "mythcontext.h"
#include "mythmiscutil.h"
#include "programinfo.h"

#define LOC QString("CommBreakMap: ")
Expand Down Expand Up @@ -216,9 +217,7 @@ bool CommBreakMap::AutoCommercialSkip(uint64_t &jumpToFrame,

int skipped_seconds = (int)((m_commBreakIter.key() -
framesPlayed) / video_frame_rate);
QString skipTime = QString("%1:%2")
.arg(skipped_seconds / 60)
.arg(abs(skipped_seconds) % 60, 2, 10, QChar('0'));
QString skipTime = MythFormatTime(skipped_seconds, "mm:ss");
if (kCommSkipOn == m_autocommercialskip)
{
//: %1 is the skip time
Expand Down Expand Up @@ -339,9 +338,7 @@ bool CommBreakMap::DoSkipCommercials(uint64_t &jumpToFrame,
MergeShortCommercials(video_frame_rate);
int skipped_seconds = (int)(((int64_t)(m_commBreakIter.key()) -
(int64_t)framesPlayed) / video_frame_rate);
QString skipTime = QString("%1:%2")
.arg(skipped_seconds / 60)
.arg(abs(skipped_seconds) % 60, 2, 10, QChar('0'));
QString skipTime = MythFormatTime(skipped_seconds, "mm:ss");

if ((m_lastIgnoredManualSkip.secsTo(MythDate::current()) > 3) &&
(abs(skipped_seconds) >= m_maxskip))
Expand Down
13 changes: 4 additions & 9 deletions mythtv/libs/libmythtv/decoders/avformatdecoder.cpp
Expand Up @@ -1257,18 +1257,13 @@ int AvFormatDecoder::OpenFile(MythMediaBuffer *Buffer, bool novideo,
int64_t start = m_ic->chapters[i]->start;
long double total_secs = (long double)start * (long double)num /
(long double)den;
int hours = (int)total_secs / 60 / 60;
int minutes = ((int)total_secs / 60) - (hours * 60);
double secs = (double)total_secs -
(double)(hours * 60 * 60 + minutes * 60);
auto msec = static_cast<uint64_t>(total_secs * 1000.0);
auto framenum = (long long)(total_secs * m_fps);
LOG(VB_PLAYBACK, LOG_INFO, LOC +
QString("Chapter %1 found @ [%2:%3:%4]->%5")
QString("Chapter %1 found @ [%2]->%3")
.arg(i + 1, 2,10,QChar('0'))
.arg(hours, 2,10,QChar('0'))
.arg(minutes, 2,10,QChar('0'))
.arg(secs, 6,'f',3,QChar('0'))
.arg(framenum));
.arg(MythFormatTimeMs(msec, "HH:mm:ss.zzz")
.arg(framenum)));
}

if (getenv("FORCE_DTS_TIMESTAMPS"))
Expand Down

0 comments on commit c47ae6d

Please sign in to comment.