192 lines
7.3 KiB
C++
192 lines
7.3 KiB
C++
#ifndef MOBILISIS_COMPACT_CALENDAR_H
|
|
#define MOBILISIS_COMPACT_CALENDAR_H
|
|
|
|
#include <QDate>
|
|
#include <QFile>
|
|
#include <QHash>
|
|
#include <QJsonArray>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QStringList>
|
|
|
|
namespace Mobilisis {
|
|
|
|
// Date-level profile selection. The caller resolves the weekday when the
|
|
// returned profile is empty, and handles paid periods within that profile.
|
|
class CompactCalendar {
|
|
public:
|
|
bool load(const QString &path, const QString &defaultProfile,
|
|
const QStringList &profiles, QString &error)
|
|
{
|
|
QFile file(path);
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
error = file.errorString();
|
|
return false;
|
|
}
|
|
|
|
QJsonParseError parseError;
|
|
const auto document = QJsonDocument::fromJson(file.readAll(), &parseError);
|
|
const auto root = document.object();
|
|
if (parseError.error != QJsonParseError::NoError ||
|
|
root.value("schemaVersion").toDouble() != 1 ||
|
|
!root.value("holidays").isArray() ||
|
|
(root.contains("exceptions") && !root.value("exceptions").isArray())) {
|
|
error = QStringLiteral("Invalid compact holiday file: %1").arg(path);
|
|
return false;
|
|
}
|
|
|
|
CompactCalendar candidate;
|
|
for (const auto &entry : root.value("holidays").toArray()) {
|
|
const auto rule = entry.toObject();
|
|
const auto profile = rule.contains("workingDayProfileId")
|
|
? rule.value("workingDayProfileId").toString() : defaultProfile;
|
|
if (!profiles.contains(profile) || !validRule(rule)) {
|
|
error = QStringLiteral("Invalid holiday rule in %1").arg(path);
|
|
return false;
|
|
}
|
|
candidate.rules.append(rule);
|
|
candidate.ruleProfiles.append(profile);
|
|
}
|
|
|
|
for (const auto &entry : root.value("exceptions").toArray()) {
|
|
const auto exception = entry.toObject();
|
|
const auto date = parseDate(exception.value("date").toString());
|
|
const auto profile = exception.value("workingDayProfileId").toString();
|
|
if (!date.isValid() || !profiles.contains(profile) ||
|
|
candidate.exceptions.contains(date)) {
|
|
error = QStringLiteral("Invalid or duplicate workday exception in %1").arg(path);
|
|
return false;
|
|
}
|
|
candidate.exceptions.insert(date, profile);
|
|
}
|
|
|
|
*this = candidate;
|
|
error.clear();
|
|
return true;
|
|
}
|
|
|
|
bool resolve(const QDate &date, QString &profile, QString &error) const
|
|
{
|
|
profile.clear();
|
|
error.clear();
|
|
if (!date.isValid()) {
|
|
error = QStringLiteral("Invalid parking date");
|
|
return false;
|
|
}
|
|
const auto exception = exceptions.constFind(date);
|
|
if (exception != exceptions.constEnd()) {
|
|
profile = exception.value();
|
|
return true;
|
|
}
|
|
|
|
for (int i = 0; i < rules.size(); ++i) {
|
|
if (!matches(rules.at(i).toObject(), date))
|
|
continue;
|
|
if (!profile.isEmpty()) {
|
|
profile.clear();
|
|
error = QStringLiteral("Overlapping holiday rules on %1").arg(date.toString(Qt::ISODate));
|
|
return false;
|
|
}
|
|
profile = ruleProfiles.at(i);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
static QDate parseDate(const QString &value)
|
|
{
|
|
const auto date = QDate::fromString(value, Qt::ISODate);
|
|
return date.isValid() && date.toString(Qt::ISODate) == value ? date : QDate();
|
|
}
|
|
|
|
static int weekday(const QJsonObject &rule)
|
|
{
|
|
const QStringList days = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
|
|
return days.indexOf(rule.value("weekday").toString()) + 1;
|
|
}
|
|
|
|
static bool integerInRange(const QJsonValue &value, int min, int max)
|
|
{
|
|
const double number = value.toDouble(max + 1);
|
|
return number >= min && number <= max && number == static_cast<int>(number);
|
|
}
|
|
|
|
static bool validRule(const QJsonObject &rule)
|
|
{
|
|
const auto type = rule.value("type").toString();
|
|
if (type == "annual_fixed" || type == "once") {
|
|
const auto dates = rule.value(type == "once" ? "dates" : "monthDays").toArray();
|
|
QStringList seen;
|
|
for (const auto &value : dates) {
|
|
const auto date = value.toString();
|
|
if (seen.contains(date) ||
|
|
!parseDate(type == "once" ? date : QStringLiteral("2000-") + date).isValid())
|
|
return false;
|
|
seen.append(date);
|
|
}
|
|
return !dates.isEmpty();
|
|
}
|
|
if (type != "annual_rule")
|
|
return false;
|
|
if (rule.value("rule") == "easter_offset") {
|
|
const auto offsets = rule.value("days").toArray();
|
|
QList<int> seen;
|
|
for (const auto &offset : offsets) {
|
|
if (!integerInRange(offset, -366, 366) || seen.contains(offset.toInt()))
|
|
return false;
|
|
seen.append(offset.toInt());
|
|
}
|
|
return !offsets.isEmpty();
|
|
}
|
|
const auto occurrence = rule.value("occurrence");
|
|
return rule.value("rule") == "nth_weekday_of_month" && weekday(rule) > 0 &&
|
|
integerInRange(rule.value("month"), 1, 12) &&
|
|
(occurrence.toDouble() == -1 || integerInRange(occurrence, 1, 5));
|
|
}
|
|
|
|
static QDate easterSunday(int year)
|
|
{
|
|
const int golden = year % 19;
|
|
const int century = year / 100;
|
|
const int remainder = year % 100;
|
|
const int correction = (century - (century + 8) / 25 + 1) / 3;
|
|
const int epact = (19 * golden + century - century / 4 - correction + 15) % 30;
|
|
const int weekdayOffset = (32 + 2 * (century % 4) + 2 * (remainder / 4)
|
|
- epact - remainder % 4) % 7;
|
|
const int adjustment = (golden + 11 * epact + 22 * weekdayOffset) / 451;
|
|
const int dayIndex = epact + weekdayOffset - 7 * adjustment + 114;
|
|
return QDate(year, dayIndex / 31, dayIndex % 31 + 1);
|
|
}
|
|
|
|
static bool matches(const QJsonObject &rule, const QDate &date)
|
|
{
|
|
const auto type = rule.value("type").toString();
|
|
if (type == "once")
|
|
return rule.value("dates").toArray().contains(QJsonValue(date.toString(Qt::ISODate)));
|
|
if (type == "annual_fixed")
|
|
return rule.value("monthDays").toArray().contains(QJsonValue(date.toString("MM-dd")));
|
|
if (rule.value("rule") == "easter_offset") {
|
|
for (const auto &offset : rule.value("days").toArray()) {
|
|
for (int year = date.year() - 1; year <= date.year() + 1; ++year) {
|
|
if (easterSunday(year).addDays(offset.toInt()) == date)
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
if (date.month() != rule.value("month").toInt() || date.dayOfWeek() != weekday(rule))
|
|
return false;
|
|
const int occurrence = rule.value("occurrence").toInt();
|
|
return occurrence == -1 ? date.day() + 7 > date.daysInMonth()
|
|
: (date.day() - 1) / 7 + 1 == occurrence;
|
|
}
|
|
|
|
QJsonArray rules;
|
|
QStringList ruleProfiles;
|
|
QHash<QDate, QString> exceptions;
|
|
};
|
|
|
|
} // namespace Mobilisis
|
|
|
|
#endif
|