Add ability to parse more of "innovative" datetime formats. close #4760

This commit is contained in:
Krzysztof Sobiecki 2015-02-25 15:29:50 +01:00
parent 0f2d7b9879
commit 24b6fbf36c
3 changed files with 48 additions and 33 deletions

View File

@ -92,7 +92,7 @@ class PlayerInterface : public QObject {
virtual void Play() = 0;
virtual void ShowOSD() = 0;
signals:
signals:
void Playing();
void Paused();
void Stopped();

View File

@ -100,8 +100,7 @@ QString PrettyTime(int seconds) {
QString ret;
if (hours)
ret.sprintf("%d:%02d:%02d", hours, minutes,
seconds); // NOLINT(runtime/printf)
ret.sprintf("%d:%02d:%02d", hours, minutes, seconds); // NOLINT(runtime/printf)
else
ret.sprintf("%d:%02d", minutes, seconds); // NOLINT(runtime/printf)
@ -162,15 +161,11 @@ QString PrettySize(quint64 bytes) {
if (bytes <= 1000)
ret = QString::number(bytes) + " bytes";
else if (bytes <= 1000 * 1000)
ret.sprintf("%.1f KB",
static_cast<float>(bytes) / 1000); // NOLINT(runtime/printf)
ret.sprintf("%.1f KB", static_cast<float>(bytes) / 1000); // NOLINT(runtime/printf)
else if (bytes <= 1000 * 1000 * 1000)
ret.sprintf("%.1f MB", static_cast<float>(bytes) /
(1000 * 1000)); // NOLINT(runtime/printf)
ret.sprintf("%.1f MB", static_cast<float>(bytes) / (1000 * 1000)); // NOLINT(runtime/printf)
else
ret.sprintf("%.1f GB",
static_cast<float>(bytes) /
(1000 * 1000 * 1000)); // NOLINT(runtime/printf)
ret.sprintf("%.1f GB", static_cast<float>(bytes) / (1000 * 1000 * 1000)); // NOLINT(runtime/printf)
}
return ret;
}
@ -564,24 +559,19 @@ bool ParseUntilElement(QXmlStreamReader* reader, const QString& name) {
}
QDateTime ParseRFC822DateTime(const QString& text) {
// This sucks but we need it because some podcasts don't quite follow the
// spec properly - they might have 1-digit hour numbers for example.
QDateTime ret;
QRegExp re(
"([a-zA-Z]{3}),? (\\d{1,2}) ([a-zA-Z]{3}) (\\d{4}) "
"(\\d{1,2}):(\\d{1,2}):(\\d{1,2})");
if (re.indexIn(text) != -1) {
ret = QDateTime(
QDate::fromString(QString("%1 %2 %3 %4")
.arg(re.cap(1), re.cap(3), re.cap(2), re.cap(4)),
Qt::TextDate),
QTime(re.cap(5).toInt(), re.cap(6).toInt(), re.cap(7).toInt()));
QRegExp regexp("(\\d{1,2}) (\\w{3,12}) (\\d+) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})");
if (regexp.indexIn(text) == -1) {
return QDateTime();
}
if (ret.isValid()) return ret;
// Because http://feeds.feedburner.com/reasonabledoubts/Msxh?format=xml
QRegExp re2(
"(\\d{1,2}) ([a-zA-Z]{3}) (\\d{4}) "
"(\\d{1,2}):(\\d{1,2}):(\\d{1,2})");
enum class MatchNames {
DAYS = 1,
MONTHS,
YEARS,
HOURS,
MINUTES,
SECONDS
};
QMap<QString, int> monthmap;
monthmap["Jan"] = 1;
@ -596,13 +586,28 @@ QDateTime ParseRFC822DateTime(const QString& text) {
monthmap["Oct"] = 10;
monthmap["Nov"] = 11;
monthmap["Dec"] = 12;
monthmap["January"] = 1;
monthmap["February"] = 2;
monthmap["March"] = 3;
monthmap["April"] = 4;
monthmap["May"] = 5;
monthmap["June"] = 6;
monthmap["July"] = 7;
monthmap["August"] = 8;
monthmap["September"] = 9;
monthmap["October"] = 10;
monthmap["November"] = 11;
monthmap["December"] = 12;
if (re2.indexIn(text) != -1) {
QDate date(re2.cap(3).toInt(), monthmap[re2.cap(2)], re2.cap(1).toInt());
ret = QDateTime(date, QTime(re2.cap(4).toInt(), re2.cap(5).toInt(),
re2.cap(6).toInt()));
}
return ret;
const QDate date(regexp.cap(static_cast<int>(MatchNames::YEARS)).toInt(),
monthmap[regexp.cap(static_cast<int>(MatchNames::MONTHS))],
regexp.cap(static_cast<int>(MatchNames::DAYS)).toInt());
const QTime time(regexp.cap(static_cast<int>(MatchNames::HOURS)).toInt(),
regexp.cap(static_cast<int>(MatchNames::MINUTES)).toInt(),
regexp.cap(static_cast<int>(MatchNames::SECONDS)).toInt());
return QDateTime (date, time);
}
const char* EnumToString(const QMetaObject& meta, const char* name, int value) {

View File

@ -21,6 +21,7 @@
#include "core/utilities.h"
#include <QDateTime>
#include <QtDebug>
TEST(UtilitiesTest, HmacFunctions) {
@ -35,3 +36,12 @@ TEST(UtilitiesTest, HmacFunctions) {
bool result_sha256 = result_hash_sha256 == QString("f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8");
EXPECT_TRUE(result_sha256);
}
TEST(UtilitiesTest, ParseRFC822DateTim) {
QDateTime result_DateTime = Utilities::ParseRFC822DateTime(QString("22 Feb 2008 00:16:17 GMT"));
EXPECT_TRUE(result_DateTime.isValid());
result_DateTime = Utilities::ParseRFC822DateTime(QString("Thu, 13 Dec 2012 13:27:52 +0000"));
EXPECT_TRUE(result_DateTime.isValid());
result_DateTime = Utilities::ParseRFC822DateTime(QString("Mon, 12 March 2012 20:00:00 +0100"));
EXPECT_TRUE(result_DateTime.isValid());
}