1
1
/*
2
2
* 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>
4
4
* Copyright (C) 2012 Christophe Dumez <chris@qbittorrent.org>
5
5
*
6
6
* This program is free software; you can redistribute it and/or
29
29
30
30
#include " rss_parser.h"
31
31
32
- #include < QDateTime>
33
32
#include < QDebug>
34
- #include < QGlobalStatic>
35
33
#include < QHash>
36
- #include < QMetaObject>
37
34
#include < QRegularExpression>
38
35
#include < QStringList>
39
36
#include < QTimeZone>
@@ -360,7 +357,7 @@ namespace
360
357
};
361
358
362
359
// Ported to Qt from KDElibs4
363
- QDateTime parseDate (const QString &string)
360
+ QDateTime parseDate (const QString &string, const QDateTime &fallbackDate )
364
361
{
365
362
const char16_t shortDay[][4 ] =
366
363
{
@@ -383,7 +380,7 @@ namespace
383
380
384
381
const QString str = string.trimmed ();
385
382
if (str.isEmpty ())
386
- return QDateTime::currentDateTime () ;
383
+ return fallbackDate ;
387
384
388
385
int nyear = 6 ; // indexes within string to values
389
386
int nmonth = 4 ;
@@ -403,14 +400,14 @@ namespace
403
400
const bool h1 = (parts[3 ] == u" -" );
404
401
const bool h2 = (parts[5 ] == u" -" );
405
402
if (h1 != h2)
406
- return QDateTime::currentDateTime () ;
403
+ return fallbackDate ;
407
404
}
408
405
else
409
406
{
410
407
// Check for the obsolete form "Wdy Mon DD HH:MM:SS YYYY"
411
408
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};
412
409
if (str.indexOf (rx, 0 , &rxMatch) != 0 )
413
- return QDateTime::currentDateTime () ;
410
+ return fallbackDate ;
414
411
415
412
nyear = 7 ;
416
413
nmonth = 2 ;
@@ -428,14 +425,14 @@ namespace
428
425
const int hour = parts[nhour].toInt (&ok[2 ]);
429
426
const int minute = parts[nmin].toInt (&ok[3 ]);
430
427
if (!ok[0 ] || !ok[1 ] || !ok[2 ] || !ok[3 ])
431
- return QDateTime::currentDateTime () ;
428
+ return fallbackDate ;
432
429
433
430
int second = 0 ;
434
431
if (!parts[nsec].isEmpty ())
435
432
{
436
433
second = parts[nsec].toInt (&ok[0 ]);
437
434
if (!ok[0 ])
438
- return QDateTime::currentDateTime () ;
435
+ return fallbackDate ;
439
436
}
440
437
441
438
const bool leapSecond = (second == 60 );
@@ -519,21 +516,21 @@ namespace
519
516
520
517
const QDate qDate (year, month + 1 , day); // convert date, and check for out-of-range
521
518
if (!qDate.isValid ())
522
- return QDateTime::currentDateTime () ;
519
+ return fallbackDate ;
523
520
524
521
const QTime qTime (hour, minute, second);
525
522
QDateTime result (qDate, qTime, QTimeZone::UTC);
526
523
if (offset)
527
524
result = result.addSecs (-offset);
528
525
if (!result.isValid ())
529
- return QDateTime::currentDateTime () ; // invalid date/time
526
+ return fallbackDate ; // invalid date/time
530
527
531
528
if (leapSecond)
532
529
{
533
530
// Validate a leap second time. Leap seconds are inserted after 23:59:59 UTC.
534
531
// Convert the time to UTC and check that it is 00:00:00.
535
532
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
537
534
}
538
535
539
536
return result;
@@ -551,6 +548,7 @@ RSS::Private::Parser::Parser(const QString &lastBuildDate)
551
548
void RSS::Private::Parser::parse (const QByteArray &feedData)
552
549
{
553
550
QXmlStreamReader xml {feedData};
551
+ m_fallbackDate = QDateTime::currentDateTime ();
554
552
XmlStreamEntityResolver resolver;
555
553
xml.setEntityResolver (&resolver);
556
554
bool foundChannel = false ;
@@ -642,7 +640,7 @@ void RSS::Private::Parser::parseRssArticle(QXmlStreamReader &xml)
642
640
}
643
641
else if (name == u" pubDate" )
644
642
{
645
- article[Article::KeyDate] = parseDate (xml.readElementText ().trimmed ());
643
+ article[Article::KeyDate] = parseDate (xml.readElementText ().trimmed (), m_fallbackDate );
646
644
}
647
645
else if (name == u" author" )
648
646
{
@@ -700,7 +698,6 @@ void RSS::Private::Parser::parseRSSChannel(QXmlStreamReader &xml)
700
698
701
699
void RSS::Private::Parser::parseAtomArticle (QXmlStreamReader &xml)
702
700
{
703
- const auto currentDateTime = QDateTime::currentDateTime ();
704
701
QVariantHash article;
705
702
bool doubleContent = false ;
706
703
@@ -757,7 +754,7 @@ void RSS::Private::Parser::parseAtomArticle(QXmlStreamReader &xml)
757
754
{
758
755
// ATOM uses standard compliant date, don't do fancy stuff
759
756
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 );
761
758
}
762
759
else if (name == u" author" )
763
760
{
0 commit comments