Skip to content

Commit 5c89ff1

Browse files
committed
Get current time only once when parse RSS feed
1 parent 2d1c4fc commit 5c89ff1

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

src/base/rss/rss_parser.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Bittorrent Client using Qt and libtorrent.
3-
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
3+
* Copyright (C) 2015-2024 Vladimir Golovnev <glassez@yandex.ru>
44
* Copyright (C) 2012 Christophe Dumez <chris@qbittorrent.org>
55
*
66
* This program is free software; you can redistribute it and/or
@@ -29,11 +29,8 @@
2929

3030
#include "rss_parser.h"
3131

32-
#include <QDateTime>
3332
#include <QDebug>
34-
#include <QGlobalStatic>
3533
#include <QHash>
36-
#include <QMetaObject>
3734
#include <QRegularExpression>
3835
#include <QStringList>
3936
#include <QTimeZone>
@@ -360,7 +357,7 @@ namespace
360357
};
361358

362359
// Ported to Qt from KDElibs4
363-
QDateTime parseDate(const QString &string)
360+
QDateTime parseDate(const QString &string, const QDateTime &fallbackDate)
364361
{
365362
const char16_t shortDay[][4] =
366363
{
@@ -383,7 +380,7 @@ namespace
383380

384381
const QString str = string.trimmed();
385382
if (str.isEmpty())
386-
return QDateTime::currentDateTime();
383+
return fallbackDate;
387384

388385
int nyear = 6; // indexes within string to values
389386
int nmonth = 4;
@@ -403,14 +400,14 @@ namespace
403400
const bool h1 = (parts[3] == u"-");
404401
const bool h2 = (parts[5] == u"-");
405402
if (h1 != h2)
406-
return QDateTime::currentDateTime();
403+
return fallbackDate;
407404
}
408405
else
409406
{
410407
// Check for the obsolete form "Wdy Mon DD HH:MM:SS YYYY"
411408
rx = QRegularExpression {u"^([A-Z][a-z]+)\\s+(\\S+)\\s+(\\d\\d)\\s+(\\d\\d):(\\d\\d):(\\d\\d)\\s+(\\d\\d\\d\\d)$"_s};
412409
if (str.indexOf(rx, 0, &rxMatch) != 0)
413-
return QDateTime::currentDateTime();
410+
return fallbackDate;
414411

415412
nyear = 7;
416413
nmonth = 2;
@@ -428,14 +425,14 @@ namespace
428425
const int hour = parts[nhour].toInt(&ok[2]);
429426
const int minute = parts[nmin].toInt(&ok[3]);
430427
if (!ok[0] || !ok[1] || !ok[2] || !ok[3])
431-
return QDateTime::currentDateTime();
428+
return fallbackDate;
432429

433430
int second = 0;
434431
if (!parts[nsec].isEmpty())
435432
{
436433
second = parts[nsec].toInt(&ok[0]);
437434
if (!ok[0])
438-
return QDateTime::currentDateTime();
435+
return fallbackDate;
439436
}
440437

441438
const bool leapSecond = (second == 60);
@@ -519,21 +516,21 @@ namespace
519516

520517
const QDate qDate(year, month + 1, day); // convert date, and check for out-of-range
521518
if (!qDate.isValid())
522-
return QDateTime::currentDateTime();
519+
return fallbackDate;
523520

524521
const QTime qTime(hour, minute, second);
525522
QDateTime result(qDate, qTime, QTimeZone::UTC);
526523
if (offset)
527524
result = result.addSecs(-offset);
528525
if (!result.isValid())
529-
return QDateTime::currentDateTime(); // invalid date/time
526+
return fallbackDate; // invalid date/time
530527

531528
if (leapSecond)
532529
{
533530
// Validate a leap second time. Leap seconds are inserted after 23:59:59 UTC.
534531
// Convert the time to UTC and check that it is 00:00:00.
535532
if ((hour*3600 + minute*60 + 60 - offset + 86400*5) % 86400) // (max abs(offset) is 100 hours)
536-
return QDateTime::currentDateTime(); // the time isn't the last second of the day
533+
return fallbackDate; // the time isn't the last second of the day
537534
}
538535

539536
return result;
@@ -551,6 +548,7 @@ RSS::Private::Parser::Parser(const QString &lastBuildDate)
551548
void RSS::Private::Parser::parse(const QByteArray &feedData)
552549
{
553550
QXmlStreamReader xml {feedData};
551+
m_fallbackDate = QDateTime::currentDateTime();
554552
XmlStreamEntityResolver resolver;
555553
xml.setEntityResolver(&resolver);
556554
bool foundChannel = false;
@@ -642,7 +640,7 @@ void RSS::Private::Parser::parseRssArticle(QXmlStreamReader &xml)
642640
}
643641
else if (name == u"pubDate")
644642
{
645-
article[Article::KeyDate] = parseDate(xml.readElementText().trimmed());
643+
article[Article::KeyDate] = parseDate(xml.readElementText().trimmed(), m_fallbackDate);
646644
}
647645
else if (name == u"author")
648646
{
@@ -700,7 +698,6 @@ void RSS::Private::Parser::parseRSSChannel(QXmlStreamReader &xml)
700698

701699
void RSS::Private::Parser::parseAtomArticle(QXmlStreamReader &xml)
702700
{
703-
const auto currentDateTime = QDateTime::currentDateTime();
704701
QVariantHash article;
705702
bool doubleContent = false;
706703

@@ -757,7 +754,7 @@ void RSS::Private::Parser::parseAtomArticle(QXmlStreamReader &xml)
757754
{
758755
// ATOM uses standard compliant date, don't do fancy stuff
759756
const QDateTime articleDate = QDateTime::fromString(xml.readElementText().trimmed(), Qt::ISODate);
760-
article[Article::KeyDate] = (articleDate.isValid() ? articleDate : currentDateTime);
757+
article[Article::KeyDate] = (articleDate.isValid() ? articleDate : m_fallbackDate);
761758
}
762759
else if (name == u"author")
763760
{

src/base/rss/rss_parser.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Bittorrent Client using Qt and libtorrent.
3-
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
3+
* Copyright (C) 2015-2024 Vladimir Golovnev <glassez@yandex.ru>
44
* Copyright (C) 2012 Christophe Dumez <chris@qbittorrent.org>
55
*
66
* This program is free software; you can redistribute it and/or
@@ -29,6 +29,7 @@
2929

3030
#pragma once
3131

32+
#include <QDateTime>
3233
#include <QList>
3334
#include <QObject>
3435
#include <QSet>
@@ -66,6 +67,7 @@ namespace RSS::Private
6667
void parseAtomChannel(QXmlStreamReader &xml);
6768
void addArticle(QVariantHash article);
6869

70+
QDateTime m_fallbackDate;
6971
QString m_baseUrl;
7072
ParsingResult m_result;
7173
QSet<QString> m_articleIDs;

0 commit comments

Comments
 (0)