testdatetime.cpp
1 //------------------------------------------------------------------------------
2 // testdatetime.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2024 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "testdatetime.h"
24 
25 #include "datetime.h"
26 #include "tests/asserts.h"
27 
28 #include <QDate>
29 #include <QTime>
30 #include <QTimeZone>
31 
32 bool TestDateTimeISO8601::executeTest()
33 {
34  T_ASSERT_EQUAL("1970-01-01T00:00:00Z", DateTime::toISO8601(
35  QDateTime::fromMSecsSinceEpoch(0, Qt::UTC)));
36 
37  // Getting the timezone by its name may fail on its own,
38  // but it's not what we're testing here, so try to recover
39  // on a fall-back, and skip the test if altogether if the
40  // timezone cannot be defined.
41  QTimeZone warsawZone("Europe/Warsaw");
42  if (!warsawZone.isValid())
43  warsawZone = QTimeZone("UTC+02:00");
44  if (warsawZone.isValid())
45  {
46  QDateTime warsaw = QDateTime(QDate(2024, 07, 04), QTime(17, 00, 00), warsawZone);
47  T_ASSERT_EQUAL("2024-07-04T17:00:00+02:00", DateTime::toISO8601(warsaw));
48  }
49  else
50  {
51  testLog << "skipped Europe/Warsaw test because couldn't resolve the timezone";
52  }
53 
54  QDateTime offsetM1 = QDateTime(QDate(2024, 07, 04), QTime(17, 00, 00), QTimeZone(-1 * 60 * 60));
55  T_ASSERT_EQUAL("2024-07-04T17:00:00-01:00", DateTime::toISO8601(offsetM1));
56 
57  return true;
58 }
59 
60 bool TestDateTimeToPathFriendlyUTCISO8601::executeTest()
61 {
62  T_ASSERT_EQUAL("1970-01-01T000000Z", DateTime::toPathFriendlyUTCISO8601(
63  QDateTime::fromMSecsSinceEpoch(0, Qt::UTC)));
64 
65  QTimeZone warsawZone("Europe/Warsaw");
66  if (!warsawZone.isValid())
67  warsawZone = QTimeZone("UTC+02:00");
68  if (warsawZone.isValid())
69  {
70  QDateTime warsaw = QDateTime(QDate(2024, 07, 04), QTime(17, 00, 00), warsawZone);
71  T_ASSERT_EQUAL("2024-07-04T150000Z", DateTime::toPathFriendlyUTCISO8601(warsaw));
72  }
73  else
74  {
75  testLog << "skipped Europe/Warsaw test because couldn't resolve the timezone";
76  }
77 
78  QDateTime offsetM1 = QDateTime(QDate(2024, 07, 04), QTime(12, 37, 21), QTimeZone(-1 * 60 * 60));
79  T_ASSERT_EQUAL("2024-07-04T133721Z", DateTime::toPathFriendlyUTCISO8601(offsetM1));
80 
81  return true;
82 }
83 
84 bool TestDateTimeParsePathFriendlyUTCISO8601::executeTest()
85 {
86  T_ASSERT_DATETIME_EQUAL(QDateTime(QDate(1970, 01, 01), QTime(0, 0, 0), Qt::UTC),
87  DateTime::fromPathFriendlyUTCISO8601("1970-01-01T000000Z"));
88 
89  T_ASSERT_DATETIME_EQUAL(QDateTime(QDate(2024, 07, 04), QTime(13, 37, 21), Qt::UTC),
90  DateTime::fromPathFriendlyUTCISO8601("2024-07-04T133721Z"));
91 
92  T_ASSERT_FALSE(DateTime::fromPathFriendlyUTCISO8601("doomseeker").isValid());
93  T_ASSERT_FALSE(DateTime::fromPathFriendlyUTCISO8601("20240704T133721Z").isValid());
94  T_ASSERT_FALSE(DateTime::fromPathFriendlyUTCISO8601("2024-07-04T13:37:21Z").isValid());
95 
96  return true;
97 }