File indexing completed on 2024-04-14 03:51:18

0001 /*
0002     This file is part of kcontacts.
0003     SPDX-FileCopyrightText: 2016 Benjamin Löwe <benni@mytum.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "datetimetest.h"
0009 #include <QTest>
0010 #include <QTimeZone>
0011 #include <vcardtool_p.h>
0012 
0013 DateTimeTest::DateTimeTest(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 DateTimeTest::~DateTimeTest()
0019 {
0020 }
0021 
0022 void DateTimeTest::shouldParseDateTime()
0023 {
0024     using namespace KContacts;
0025 
0026     QDateTime expected;
0027     QDateTime dt;
0028     bool timeIsValid;
0029 
0030     dt = VCardTool::parseDateTime(QStringLiteral("2016-01-20T12:33:30+0200"));
0031     expected = QDateTime(QDate(2016, 1, 20), QTime(12, 33, 30), QTimeZone::fromSecondsAheadOfUtc(2 * 3600));
0032     QCOMPARE(dt, expected);
0033     QVERIFY(dt.isValid());
0034 
0035     dt = VCardTool::parseDateTime(QStringLiteral("2016-01-20T12:33:30+02"));
0036     QCOMPARE(dt, expected);
0037     QVERIFY(dt.isValid());
0038 
0039     dt = VCardTool::parseDateTime(QStringLiteral("20160120T123330+0200"));
0040     QCOMPARE(dt, expected);
0041     QVERIFY(dt.isValid());
0042 
0043     dt = VCardTool::parseDateTime(QStringLiteral("2016-01-20T12:33Z"));
0044     expected = QDateTime(QDate(2016, 1, 20), QTime(12, 33, 0), QTimeZone::UTC);
0045     QCOMPARE(dt, expected);
0046     QVERIFY(dt.isValid());
0047 
0048     dt = VCardTool::parseDateTime(QStringLiteral("2016-01-20T12:33-0300"));
0049     expected = QDateTime(QDate(2016, 1, 20), QTime(12, 33, 0), QTimeZone::fromSecondsAheadOfUtc(-3 * 3600));
0050     QCOMPARE(dt, expected);
0051     QVERIFY(dt.isValid());
0052 
0053     dt = VCardTool::parseDateTime(QStringLiteral("2016-01-20T12:33"), &timeIsValid);
0054     expected = QDateTime(QDate(2016, 1, 20), QTime(12, 33, 0), QTimeZone::LocalTime);
0055     QCOMPARE(dt, expected);
0056     QVERIFY(timeIsValid);
0057     QVERIFY(dt.isValid());
0058 
0059     dt = VCardTool::parseDateTime(QStringLiteral("2016-01-20"), &timeIsValid);
0060     expected = QDateTime(QDate(2016, 1, 20), QTime(), QTimeZone::LocalTime);
0061     QCOMPARE(dt, expected);
0062     QVERIFY(dt.isValid());
0063     QVERIFY(!timeIsValid);
0064     QCOMPARE(dt.time(), QTime(0, 0));
0065 
0066     dt = VCardTool::parseDateTime(QStringLiteral("T1233Z"), &timeIsValid);
0067     QVERIFY(!dt.isValid());
0068     QVERIFY(timeIsValid);
0069     QCOMPARE(dt.time(), QTime(12, 33));
0070 
0071     dt = VCardTool::parseDateTime(QStringLiteral("--0120"));
0072     expected = QDateTime(QDate(-1, 1, 20).startOfDay());
0073     QCOMPARE(dt, expected);
0074     QVERIFY(dt.isValid());
0075 
0076     dt = VCardTool::parseDateTime(QStringLiteral("--01-20"));
0077     expected = QDateTime(QDate(-1, 1, 20).startOfDay());
0078     QCOMPARE(dt, expected);
0079     QVERIFY(dt.isValid());
0080 }
0081 
0082 void DateTimeTest::shouldCreateDateTime()
0083 {
0084     using namespace KContacts;
0085 
0086     QDateTime dt;
0087     QString str;
0088     QString expected;
0089 
0090     dt = QDateTime(QDate(2016, 1, 20), QTime(12, 33, 30), QTimeZone::UTC);
0091     str = VCardTool::createDateTime(dt, VCard::v4_0);
0092     expected = QStringLiteral("20160120T123330Z");
0093     QCOMPARE(str, expected);
0094 
0095     dt = QDateTime(QDate(2016, 1, 20), QTime(), QTimeZone::UTC);
0096     str = VCardTool::createDateTime(dt, VCard::v4_0);
0097     expected = QStringLiteral("20160120T000000Z");
0098     QCOMPARE(str, expected);
0099 
0100     dt = QDateTime(QDate(2016, 1, 20), QTime(12, 33, 30), QTimeZone::LocalTime);
0101     str = VCardTool::createDateTime(dt, VCard::v4_0);
0102     expected = QStringLiteral("20160120T123330");
0103     QCOMPARE(str, expected);
0104 
0105     dt = QDateTime(QDate(2016, 1, 20), QTime(12, 33, 30), QTimeZone::fromSecondsAheadOfUtc(-2 * 3600));
0106     str = VCardTool::createDateTime(dt, VCard::v4_0);
0107     expected = QStringLiteral("20160120T123330-0200");
0108     QCOMPARE(str, expected);
0109 
0110     dt = QDateTime(QDate(2016, 1, 20), QTime(12, 33, 30), QTimeZone::fromSecondsAheadOfUtc(+2.5 * 3600));
0111     str = VCardTool::createDateTime(dt, VCard::v4_0);
0112     expected = QStringLiteral("20160120T123330+0230");
0113     QCOMPARE(str, expected);
0114 
0115     dt = QDateTime(QDate(2016, 1, 20), QTime(12, 33, 30), QTimeZone::fromSecondsAheadOfUtc(-2 * 3600));
0116     str = VCardTool::createDateTime(dt, VCard::v3_0);
0117     expected = QStringLiteral("2016-01-20T12:33:30-02:00");
0118     QCOMPARE(str, expected);
0119 
0120     dt = QDateTime(QDate(), QTime(12, 33, 30), QTimeZone::fromSecondsAheadOfUtc(-2 * 3600));
0121     str = VCardTool::createDateTime(dt, VCard::v3_0);
0122     expected = QString();
0123     QCOMPARE(str, expected);
0124 
0125     dt = QDateTime(QDate(2016, 1, 20), QTime(12, 33, 30));
0126     str = VCardTool::createDateTime(dt, VCard::v3_0, false);
0127     expected = QStringLiteral("2016-01-20");
0128     QCOMPARE(str, expected);
0129 
0130     dt = QDateTime(QDate(2016, 1, 20), QTime(12, 33, 30));
0131     str = VCardTool::createDateTime(dt, VCard::v4_0, false);
0132     expected = QStringLiteral("20160120");
0133     QCOMPARE(str, expected);
0134 }
0135 
0136 void DateTimeTest::shouldCreateDate()
0137 {
0138     using namespace KContacts;
0139 
0140     QDate d;
0141     QString str;
0142     QString expected;
0143 
0144     d = QDate(2016, 1, 20);
0145     str = VCardTool::createDate(d, VCard::v3_0);
0146     expected = QStringLiteral("2016-01-20");
0147     QCOMPARE(str, expected);
0148 
0149     d = QDate(2016, 1, 20);
0150     str = VCardTool::createDate(d, VCard::v4_0);
0151     expected = QStringLiteral("20160120");
0152     QCOMPARE(str, expected);
0153 
0154     d = QDate(-1, 1, 20);
0155     str = VCardTool::createDate(d, VCard::v3_0);
0156     expected = QStringLiteral("--01-20");
0157     QCOMPARE(str, expected);
0158 
0159     d = QDate(-1, 1, 20);
0160     str = VCardTool::createDate(d, VCard::v4_0);
0161     expected = QStringLiteral("--0120");
0162     QCOMPARE(str, expected);
0163 }
0164 
0165 QTEST_MAIN(DateTimeTest)
0166 
0167 #include "moc_datetimetest.cpp"