|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Julius Künzel <julius.kuenzel@kde.org> |
| 3 | + * |
| 4 | + * This library is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU Lesser General Public |
| 6 | + * License as published by the Free Software Foundation; either |
| 7 | + * version 2.1 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This library is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public |
| 15 | + * License along with consumer library; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + */ |
| 18 | + |
| 19 | +#include <QtTest> |
| 20 | + |
| 21 | +#include <modules/qt/gps_parser.h> |
| 22 | + |
| 23 | +extern int64_t datetimeXMLstring_to_mseconds(const char *text, char *format); |
| 24 | + |
| 25 | +class TestModQt : public QObject |
| 26 | +{ |
| 27 | + Q_OBJECT |
| 28 | + |
| 29 | +public: |
| 30 | + TestModQt() { } |
| 31 | + |
| 32 | + ~TestModQt() { } |
| 33 | + |
| 34 | +private Q_SLOTS: |
| 35 | + void DatetimeXMLstring_to_mseconds_ValidConversion() |
| 36 | + { |
| 37 | + int64_t gps_proc_t = 0; |
| 38 | + |
| 39 | + // ISO 8601 extended date with timezone info Z |
| 40 | + const char *text = "2020-07-11T09:03:23.000Z"; |
| 41 | + gps_proc_t = datetimeXMLstring_to_mseconds(text); |
| 42 | + QCOMPARE(gps_proc_t, 1594458203000); |
| 43 | + |
| 44 | + // ISO 8601 extended date with timezone offset |
| 45 | + text = "2021-02-27T12:10:00+00:00"; |
| 46 | + gps_proc_t = datetimeXMLstring_to_mseconds(text); |
| 47 | + QCOMPARE(gps_proc_t, 1614427800000); |
| 48 | + |
| 49 | + // ISO 8601 extended date with ms and timezone offset |
| 50 | + text = "2020-07-11T09:03:23.000+00:00"; |
| 51 | + gps_proc_t = datetimeXMLstring_to_mseconds(text); |
| 52 | + QCOMPARE(gps_proc_t, 1594458203000); |
| 53 | + |
| 54 | + // ISO 8601 extended date without timezone info |
| 55 | + text = "2020-07-11T09:03:23.000"; |
| 56 | + gps_proc_t = datetimeXMLstring_to_mseconds(text); |
| 57 | + QCOMPARE(gps_proc_t, 1594458203000); |
| 58 | + } |
| 59 | + |
| 60 | + void DatetimeXMLstring_to_mseconds_FormatString() |
| 61 | + { |
| 62 | + int64_t gps_proc_t = 0; |
| 63 | + |
| 64 | + // Unknown input, with format string as hint |
| 65 | + const char *text = "12:10:00 27-02-2021"; |
| 66 | + gps_proc_t = datetimeXMLstring_to_mseconds(text, (char *) "HH:mm:ss dd-MM-yyyy"); |
| 67 | + QCOMPARE(gps_proc_t, 1614427800000); |
| 68 | + |
| 69 | + // Unknown input, without format string |
| 70 | + text = "12:10:00 27-02-2021"; |
| 71 | + gps_proc_t = datetimeXMLstring_to_mseconds(text); |
| 72 | + QCOMPARE(gps_proc_t, 0); |
| 73 | + |
| 74 | + // Valid input, but mismatching format string |
| 75 | + text = "2021-02-27T12:10:00+00:00"; |
| 76 | + gps_proc_t = datetimeXMLstring_to_mseconds(text, (char *) "HH:mm:ss dd-MM-yyyy"); |
| 77 | + QCOMPARE(gps_proc_t, 0); |
| 78 | + } |
| 79 | + |
| 80 | + void DatetimeXMLstring_to_mseconds_InvalidInput() |
| 81 | + { |
| 82 | + int64_t gps_proc_t = 0; |
| 83 | + |
| 84 | + // Invalid input, without format string |
| 85 | + const char *text = "invalid-date-format"; |
| 86 | + gps_proc_t = datetimeXMLstring_to_mseconds(text); |
| 87 | + QCOMPARE(gps_proc_t, 0); |
| 88 | + |
| 89 | + // Invalid input, with format string |
| 90 | + gps_proc_t = datetimeXMLstring_to_mseconds(text, (char *) "%Y-%m-%dT%H:%M:%S"); |
| 91 | + QCOMPARE(gps_proc_t, 0); |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +QTEST_APPLESS_MAIN(TestModQt) |
| 96 | + |
| 97 | +#include "test_mod_qt_gps.moc" |
0 commit comments