Fix millisecond to centisecond convertion

Fix Aegisub/Aegisub#94
This commit is contained in:
wangqr 2018-11-10 15:51:48 -05:00 committed by Thomas Goyne
parent d89c1ce900
commit 5d4973a5f6
4 changed files with 30 additions and 30 deletions

View file

@ -60,25 +60,35 @@ Time::Time(std::string const& text) {
} }
std::string Time::GetAssFormatted(bool msPrecision) const { std::string Time::GetAssFormatted(bool msPrecision) const {
int ass_time = msPrecision ? time : int(*this);
std::string ret(10 + msPrecision, ':'); std::string ret(10 + msPrecision, ':');
ret[0] = '0' + GetTimeHours(); ret[0] = '0' + ass_time / 3600000;
ret[2] = '0' + (time % (60 * 60 * 1000)) / (60 * 1000 * 10); ret[2] = '0' + (ass_time % (60 * 60 * 1000)) / (60 * 1000 * 10);
ret[3] = '0' + (time % (10 * 60 * 1000)) / (60 * 1000); ret[3] = '0' + (ass_time % (10 * 60 * 1000)) / (60 * 1000);
ret[5] = '0' + (time % (60 * 1000)) / (1000 * 10); ret[5] = '0' + (ass_time % (60 * 1000)) / (1000 * 10);
ret[6] = '0' + (time % (10 * 1000)) / 1000; ret[6] = '0' + (ass_time % (10 * 1000)) / 1000;
ret[7] = '.'; ret[7] = '.';
ret[8] = '0' + (time % 1000) / 100; ret[8] = '0' + (ass_time % 1000) / 100;
ret[9] = '0' + (time % 100) / 10; ret[9] = '0' + (ass_time % 100) / 10;
if (msPrecision) if (msPrecision)
ret[10] = '0' + time % 10; ret[10] = '0' + ass_time % 10;
return ret; return ret;
} }
int Time::GetTimeHours() const { return time / 3600000; } std::string Time::GetSrtFormatted() const {
int Time::GetTimeMinutes() const { return (time % 3600000) / 60000; } std::string ret(12, ':');
int Time::GetTimeSeconds() const { return (time % 60000) / 1000; } ret[0] = '0';
int Time::GetTimeMiliseconds() const { return (time % 1000); } ret[1] = '0' + time / 3600000;
int Time::GetTimeCentiseconds() const { return (time % 1000) / 10; } ret[3] = '0' + (time % (60 * 60 * 1000)) / (60 * 1000 * 10);
ret[4] = '0' + (time % (10 * 60 * 1000)) / (60 * 1000);
ret[6] = '0' + (time % (60 * 1000)) / (1000 * 10);
ret[7] = '0' + (time % (10 * 1000)) / 1000;
ret[8] = ',';
ret[9] = '0' + (time % 1000) / 100;
ret[10] = '0' + (time % 100) / 10;
ret[11] = '0' + time % 10;
return ret;
}
SmpteFormatter::SmpteFormatter(vfr::Framerate fps, char sep) SmpteFormatter::SmpteFormatter(vfr::Framerate fps, char sep)
: fps(std::move(fps)) : fps(std::move(fps))

View file

@ -28,16 +28,14 @@ public:
Time(std::string const& text); Time(std::string const& text);
/// Get millisecond, rounded to centisecond precision /// Get millisecond, rounded to centisecond precision
operator int() const { return time / 10 * 10; } // Always round up for 5ms because the range is [start, stop)
operator int() const { return (time + 5) - (time + 5) % 10; }
int GetTimeHours() const; ///< Get the hours portion of this time
int GetTimeMinutes() const; ///< Get the minutes portion of this time
int GetTimeSeconds() const; ///< Get the seconds portion of this time
int GetTimeMiliseconds() const; ///< Get the miliseconds portion of this time
int GetTimeCentiseconds() const; ///< Get the centiseconds portion of this time
/// Return the time as a string /// Return the time as a string
/// @param ms Use milliseconds precision, for non-ASS formats /// @param ms Use milliseconds precision, for non-ASS formats
std::string GetAssFormatted(bool ms=false) const; std::string GetAssFormatted(bool ms=false) const;
/// Return the time as a string
std::string GetSrtFormatted() const;
}; };
} }

View file

@ -279,7 +279,7 @@ public:
std::string WriteSRTTime(agi::Time const& ts) std::string WriteSRTTime(agi::Time const& ts)
{ {
return agi::format("%02d:%02d:%02d,%03d", ts.GetTimeHours(), ts.GetTimeMinutes(), ts.GetTimeSeconds(), ts.GetTimeMiliseconds()); return ts.GetSrtFormatted();
} }
} }

View file

@ -62,17 +62,9 @@ TEST(lagi_time, extra_garbage_is_ignored) {
EXPECT_STREQ("1:23:45.67", Time("1a:b2c3d:e4f5g.!6&7").GetAssFormatted().c_str()); EXPECT_STREQ("1:23:45.67", Time("1a:b2c3d:e4f5g.!6&7").GetAssFormatted().c_str());
} }
TEST(lagi_time, component_getters) {
Time t("1:23:45.67");
EXPECT_EQ(1, t.GetTimeHours());
EXPECT_EQ(23, t.GetTimeMinutes());
EXPECT_EQ(45, t.GetTimeSeconds());
EXPECT_EQ(67, t.GetTimeCentiseconds());
EXPECT_EQ(670, t.GetTimeMiliseconds());
}
TEST(lagi_time, srt_time) { TEST(lagi_time, srt_time) {
EXPECT_STREQ("1:23:45.678", Time("1:23:45,678").GetAssFormatted(true).c_str()); EXPECT_STREQ("1:23:45.678", Time("1:23:45,678").GetAssFormatted(true).c_str());
EXPECT_STREQ("01:23:45,678", Time("1:23:45,678").GetSrtFormatted().c_str());
} }
TEST(lagi_time, smpte_parse_valid) { TEST(lagi_time, smpte_parse_valid) {