File indexing completed on 2022-11-29 19:02:36

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <KOpeningHours/Display>
0008 #include <KOpeningHours/Interval>
0009 #include <KOpeningHours/OpeningHours>
0010 
0011 #include <QCoreApplication>
0012 #include <QCommandLineParser>
0013 #include <QDateTime>
0014 #include <QTimeZone>
0015 
0016 #include <iostream>
0017 
0018 using namespace KOpeningHours;
0019 
0020 void printInterval(const Interval &i)
0021 {
0022     if (!i.hasOpenBegin()) {
0023         std::cout << qPrintable(i.begin().toString(QStringLiteral("ddd yyyy-MM-dd hh:mm")));
0024     } else {
0025         std::cout << "since ever";
0026     }
0027     std::cout << " - ";
0028     if (!i.hasOpenEnd()) {
0029         std::cout << qPrintable(i.end().toString(QStringLiteral("ddd yyyy-MM-dd hh:mm")));
0030         if (i.hasOpenEndTime()) {
0031             std::cout << '+';
0032         }
0033     } else {
0034         std::cout << "until all eternity";
0035     }
0036     std::cout << ": ";
0037     switch (i.state()) {
0038         case Interval::Open:
0039             std::cout << "open";
0040             break;
0041         case Interval::Closed:
0042             std::cout << "closed";
0043             break;
0044         case Interval::Unknown:
0045             std::cout << "unknown";
0046             break;
0047         case Interval::Invalid:
0048             break;
0049     }
0050     if (!i.comment().isEmpty()) {
0051         std::cout << " (" << qPrintable(i.comment()) << ")";
0052     }
0053     std::cout << std::endl;
0054 }
0055 
0056 static QString errorString(OpeningHours::Error error)
0057 {
0058     switch (error) {
0059     case OpeningHours::Null:
0060         return QStringLiteral("Empty specification");
0061     case OpeningHours::SyntaxError:
0062         return QStringLiteral("Syntax error");
0063     case OpeningHours::MissingLocation:
0064         return QStringLiteral("Missing location");
0065     case OpeningHours::MissingRegion:
0066         return QStringLiteral("Missing region");
0067     case OpeningHours::UnsupportedFeature:
0068         return QStringLiteral("Unsupported feature");
0069     case OpeningHours::IncompatibleMode:
0070         return QStringLiteral("Incompatible mode");
0071     case OpeningHours::EvaluationError:
0072         return QStringLiteral("Evaluation error");
0073     case OpeningHours::NoError:
0074         break;
0075     }
0076     return {};
0077 }
0078 
0079 int main(int argc, char **argv)
0080 {
0081     QCoreApplication app(argc, argv);
0082     QCommandLineParser parser;
0083     parser.addHelpOption();
0084     parser.addVersionOption();
0085 
0086     QCommandLineOption regionOpt({QStringLiteral("r"), QStringLiteral("region")}, QStringLiteral("ISO 3166-1/2 region code for public holidays"), QStringLiteral("region"));
0087     parser.addOption(regionOpt);
0088     QCommandLineOption coordOpt({QStringLiteral("c"), QStringLiteral("coordinate")}, QStringLiteral("Geographic coordinate"), QStringLiteral("lat/lon"));
0089     parser.addOption(coordOpt);
0090     QCommandLineOption tzOpt({QStringLiteral("t"), QStringLiteral("timezone")}, QStringLiteral("IANA timezone id"), QStringLiteral("tz"));
0091     parser.addOption(tzOpt);
0092 
0093     parser.addPositionalArgument(QStringLiteral("expression"), QStringLiteral("OSM opening hours expression."));
0094     parser.process(app);
0095 
0096     if (parser.positionalArguments().isEmpty()) {
0097         parser.showHelp(1);
0098     }
0099 
0100     OpeningHours oh(parser.positionalArguments().at(0).toUtf8());
0101     if (parser.isSet(regionOpt)) {
0102         oh.setRegion(parser.value(regionOpt));
0103     }
0104     if (parser.isSet(coordOpt)) {
0105         const auto coords = parser.value(coordOpt).split(QLatin1Char('/'), Qt::SkipEmptyParts);
0106         if (coords.size() != 2) {
0107             parser.showHelp(1);
0108         }
0109         oh.setLocation(coords[0].toFloat(), coords[1].toFloat());
0110     }
0111     if (parser.isSet(tzOpt)) {
0112         oh.setTimeZone(QTimeZone(parser.value(tzOpt).toUtf8()));
0113     }
0114 
0115     if (oh.error() != OpeningHours::NoError) {
0116         qWarning() << errorString(oh.error());
0117         return 1;
0118     }
0119 
0120     std::cout << qPrintable(Display::currentState(oh)) << std::endl << std::endl;
0121 
0122     auto interval = oh.interval(QDateTime::currentDateTime());
0123     printInterval(interval);
0124     for (int i = 0; interval.isValid() && i < 20; ++i) {
0125         interval = oh.nextInterval(interval);
0126         if (interval.isValid()) {
0127             printInterval(interval);
0128         }
0129     }
0130 
0131     return 0;
0132 }