fixed logic regarding range over multiple years

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-07-03 09:21:25 +03:00
parent ba159ba971
commit 5d5f1b86fd
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
2 changed files with 20 additions and 19 deletions

View File

@ -34,11 +34,7 @@
*/ */
#include "ui/themes/CatPack.h" #include "ui/themes/CatPack.h"
#include <qdatetime.h> #include <QDate>
#include <qjsonarray.h>
#include <qjsonobject.h>
#include <qobject.h>
#include <QDateTime>
#include <QDir> #include <QDir>
#include <QFileInfo> #include <QFileInfo>
#include "FileSystem.h" #include "FileSystem.h"
@ -47,10 +43,10 @@
QString BasicCatPack::path() QString BasicCatPack::path()
{ {
const QDateTime now = QDateTime::currentDateTime(); const auto now = QDate::currentDate();
const QDateTime birthday(QDate(now.date().year(), 11, 30), QTime(0, 0)); const auto birthday = QDate(now.year(), 11, 30);
const QDateTime xmas(QDate(now.date().year(), 12, 25), QTime(0, 0)); const auto xmas = QDate(now.year(), 12, 25);
const QDateTime halloween(QDate(now.date().year(), 10, 31), QTime(0, 0)); const auto halloween = QDate(now.year(), 10, 31);
QString cat = QString(":/backgrounds/%1").arg(m_id); QString cat = QString(":/backgrounds/%1").arg(m_id);
if (std::abs(now.daysTo(xmas)) <= 4) { if (std::abs(now.daysTo(xmas)) <= 4) {
@ -85,8 +81,8 @@ JsonCatPack::JsonCatPack(QFileInfo& manifestInfo) : BasicCatPack(manifestInfo.di
for (auto v : variants) { for (auto v : variants) {
auto variant = Json::ensureObject(v, QJsonObject(), "Cat variant"); auto variant = Json::ensureObject(v, QJsonObject(), "Cat variant");
m_variants << Variant{ FS::PathCombine(path, Json::requireString(variant, "path", "Variant path")), m_variants << Variant{ FS::PathCombine(path, Json::requireString(variant, "path", "Variant path")),
date(Json::requireString(variant, "startTime", "Variant startTime")), PartialDate(Json::requireString(variant, "startTime", "Variant startTime")),
date(Json::requireString(variant, "endTime", "Variant endTime")) }; PartialDate(Json::requireString(variant, "endTime", "Variant endTime")) };
} }
} catch (const Exception& e) { } catch (const Exception& e) {
@ -104,9 +100,14 @@ QString JsonCatPack::path()
for (auto var : m_variants) { for (auto var : m_variants) {
QDate startDate(now.year(), var.startTime.month, var.startTime.day); QDate startDate(now.year(), var.startTime.month, var.startTime.day);
QDate endDate(now.year(), var.endTime.month, var.endTime.day); QDate endDate(now.year(), var.endTime.month, var.endTime.day);
if (startDate.daysTo(endDate) < 0) // in this case end date should be next year if (startDate > endDate) { // it's spans over multiple years
endDate = endDate.addYears(1); if (endDate <= now) // end date is in the past so jump one year into the future for endDate
if (startDate.daysTo(now) >= 0 && now.daysTo(endDate) >= 0) endDate = endDate.addYears(1);
else // end date is in the future so jump one year into the past for startDate
startDate = startDate.addYears(-1);
}
if (startDate >= now && now >= endDate)
return var.path; return var.path;
} }
return m_defaultPath; return m_defaultPath;

View File

@ -35,7 +35,7 @@
#pragma once #pragma once
#include <QDateTime> #include <QDate>
#include <QFileInfo> #include <QFileInfo>
#include <QList> #include <QList>
#include <QString> #include <QString>
@ -73,8 +73,8 @@ class FileCatPack : public BasicCatPack {
class JsonCatPack : public BasicCatPack { class JsonCatPack : public BasicCatPack {
public: public:
struct date { struct PartialDate {
date(QString d) PartialDate(QString d)
{ {
auto sp = d.split("-"); auto sp = d.split("-");
day = sp[0].toInt(); day = sp[0].toInt();
@ -86,8 +86,8 @@ class JsonCatPack : public BasicCatPack {
}; };
struct Variant { struct Variant {
QString path; QString path;
date startTime; PartialDate startTime;
date endTime; PartialDate endTime;
}; };
JsonCatPack(QFileInfo& manifestInfo); JsonCatPack(QFileInfo& manifestInfo);
virtual QString path(); virtual QString path();