2006-01-16 22:02:54 +01:00
|
|
|
// Copyright (c) 2005, Rodrigo Braz Monteiro
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file ass_time.cpp
|
|
|
|
/// @brief Class for managing timestamps in subtitles
|
|
|
|
/// @ingroup subs_storage
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-10-09 18:34:38 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
#include "ass_time.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
#ifndef AGI_PRE
|
2006-01-16 22:02:54 +01:00
|
|
|
#include <algorithm>
|
2011-12-22 22:28:41 +01:00
|
|
|
#include <cmath>
|
2012-03-29 21:04:36 +02:00
|
|
|
|
|
|
|
#include <wx/tokenzr.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "utils.h"
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-12-22 22:28:51 +01:00
|
|
|
AssTime::AssTime(int time) : time(mid(0, time, 10 * 60 * 60 * 1000 - 1)) { }
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-02-14 01:35:06 +01:00
|
|
|
AssTime::AssTime(wxString const& text)
|
|
|
|
: time(0)
|
|
|
|
{
|
2011-12-22 22:28:41 +01:00
|
|
|
size_t pos = 0, end = 0;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
int colons = text.Freq(':');
|
2012-03-25 06:05:06 +02:00
|
|
|
|
2007-06-23 08:07:32 +02:00
|
|
|
// Set start so that there are only two colons at most
|
2011-12-22 22:28:41 +01:00
|
|
|
for (; colons > 2; --colons) pos = text.find(':', pos) + 1;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
// Hours
|
|
|
|
if (colons == 2) {
|
|
|
|
while (text[end++] != ':') { }
|
2012-02-14 01:35:06 +01:00
|
|
|
time += AegiStringToInt(text, pos, end) * 60 * 60 * 1000;
|
2011-12-22 22:28:41 +01:00
|
|
|
pos = end;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
// Minutes
|
|
|
|
if (colons >= 1) {
|
|
|
|
while (text[end++] != ':') { }
|
2012-02-14 01:35:06 +01:00
|
|
|
time += AegiStringToInt(text, pos, end) * 60 * 1000;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
// Milliseconds (includes seconds)
|
2012-02-14 01:35:06 +01:00
|
|
|
time += AegiStringToFix(text, 3, end, text.size());
|
2012-03-12 00:04:56 +01:00
|
|
|
|
|
|
|
// Limit to the valid range
|
|
|
|
time = mid(0, time, 10 * 60 * 60 * 1000 - 1);
|
2006-02-21 23:09:15 +01:00
|
|
|
}
|
|
|
|
|
2012-10-12 03:52:36 +02:00
|
|
|
wxString AssTime::GetAssFormated(bool msPrecision) const {
|
2012-01-31 01:43:23 +01:00
|
|
|
wxChar ret[] = {
|
|
|
|
'0' + GetTimeHours(),
|
|
|
|
':',
|
|
|
|
'0' + (time % (60 * 60 * 1000)) / (60 * 1000 * 10),
|
|
|
|
'0' + (time % (10 * 60 * 1000)) / (60 * 1000),
|
|
|
|
':',
|
|
|
|
'0' + (time % (60 * 1000)) / (1000 * 10),
|
|
|
|
'0' + (time % (10 * 1000)) / 1000,
|
|
|
|
'.',
|
|
|
|
'0' + (time % 1000) / 100,
|
|
|
|
'0' + (time % 100) / 10,
|
|
|
|
'0' + time % 10
|
|
|
|
};
|
|
|
|
|
|
|
|
return wxString(ret, msPrecision ? 11 : 10);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:28:41 +01:00
|
|
|
int AssTime::GetTimeHours() const { return time / 3600000; }
|
|
|
|
int AssTime::GetTimeMinutes() const { return (time % 3600000) / 60000; }
|
|
|
|
int AssTime::GetTimeSeconds() const { return (time % 60000) / 1000; }
|
|
|
|
int AssTime::GetTimeMiliseconds() const { return (time % 1000); }
|
|
|
|
int AssTime::GetTimeCentiseconds() const { return (time % 1000) / 10; }
|
2009-05-10 05:50:58 +02:00
|
|
|
|
2012-03-29 21:04:36 +02:00
|
|
|
SmpteFormatter::SmpteFormatter(agi::vfr::Framerate fps, char sep)
|
2011-12-22 22:28:13 +01:00
|
|
|
: fps(fps)
|
2012-03-29 21:04:36 +02:00
|
|
|
, sep(sep)
|
2011-12-22 22:22:49 +01:00
|
|
|
{
|
2009-05-10 05:50:58 +02:00
|
|
|
}
|
|
|
|
|
2012-03-29 21:04:36 +02:00
|
|
|
wxString SmpteFormatter::ToSMPTE(AssTime time) const {
|
|
|
|
int h=0, m=0, s=0, f=0;
|
|
|
|
fps.SmpteAtTime(time, &h, &m, &s, &f);
|
|
|
|
return wxString::Format("%02d%c%02d%c%02d%c%02d", h, sep, m, sep, s, sep, f);
|
|
|
|
}
|
2009-05-13 22:24:21 +02:00
|
|
|
|
2012-03-29 21:04:36 +02:00
|
|
|
AssTime SmpteFormatter::FromSMPTE(wxString const& str) const {
|
|
|
|
long h, m, s, f;
|
|
|
|
wxArrayString toks = wxStringTokenize(str, sep);
|
|
|
|
if (toks.size() != 4) return 0;
|
|
|
|
toks[0].ToLong(&h);
|
|
|
|
toks[1].ToLong(&m);
|
|
|
|
toks[2].ToLong(&s);
|
|
|
|
toks[3].ToLong(&f);
|
|
|
|
return fps.TimeAtSmpte(h, m, s, f);
|
2009-05-10 05:50:58 +02:00
|
|
|
}
|