summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp
blob: bfc811eebe775a63cc2d7d7cde59d1cfe1ed06c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QTest>
#include <private/qdatetimeparser_p.h>

using namespace Qt::StringLiterals;

QT_BEGIN_NAMESPACE

// access to needed members in QDateTimeParser
class QDTPUnitTestParser : public QDateTimeParser
{
public:
    QDTPUnitTestParser() : QDateTimeParser(QMetaType::QDateTime, QDateTimeParser::DateTimeEdit) { }

    // forward data structures
    using QDateTimeParser::ParsedSection;
    using QDateTimeParser::State;

    // function to manipulate private internals
    void setText(QString text) { m_text = text; }

    // forwarding of methods
    using QDateTimeParser::parseSection;
};

bool operator==(const QDTPUnitTestParser::ParsedSection &a,
                const QDTPUnitTestParser::ParsedSection &b)
{
    return a.value == b.value && a.used == b.used && a.zeroes == b.zeroes && a.state == b.state;
}

// pretty printing for ParsedSection
char *toString(const QDTPUnitTestParser::ParsedSection &section)
{
    using QTest::toString;
    return toString(QByteArray("ParsedSection(") + "state=" + QByteArray::number(section.state)
                    + ", value=" + QByteArray::number(section.value)
                    + ", used=" + QByteArray::number(section.used)
                    + ", zeros=" + QByteArray::number(section.zeroes) + ")");
}

QT_END_NAMESPACE

class tst_QDateTimeParser : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void reparse();
    void parseSection_data();
    void parseSection();

    void intermediateYear_data();
    void intermediateYear();
};

void tst_QDateTimeParser::reparse()
{
    const QDateTime when = QDate(2023, 6, 15).startOfDay();
    // QTBUG-114575: 6.2 through 6.5 got back a bogus Qt::TimeZone (with zero offset):
    const auto expect = ([](QStringView name) {
        // When local time is UTC or a fixed offset from it, the parser prefers
        // to interpret a UTC or offset suffix as such, rather than as local
        // time (thereby avoiding DST-ness checks). We have to match that here.
        if (name == "UTC"_L1)
            return Qt::UTC;
        if (name.startsWith(u'+') || name.startsWith(u'-')) {
            if (std::all_of(name.begin() + 1, name.end(), [](QChar ch) { return ch == u'0'; }))
                return Qt::UTC;
            if (std::all_of(name.begin() + 1, name.end(), [](QChar ch) { return ch.isDigit(); }))
                return Qt::OffsetFromUTC;
            // Potential hh:mm offset ?  Not yet seen as local tzname[] entry.
        }
        return Qt::LocalTime;
    });

    const QStringView format = u"dd/MM/yyyy HH:mm t";
    QDateTimeParser who(QMetaType::QDateTime, QDateTimeParser::DateTimeEdit);
    QVERIFY(who.parseFormat(format));
    {
        // QDTP defaults to the system locale.
        const auto state = who.parse(QLocale::system().toString(when, format), -1, when, false);
        QCOMPARE(state.state, QDateTimeParser::Acceptable);
        QVERIFY(!state.conflicts);
        QCOMPARE(state.padded, 0);
        QCOMPARE(state.value.timeSpec(), expect(when.timeZoneAbbreviation()));
        QCOMPARE(state.value, when);
    }
    {
        // QDT::toString() uses the C locale:
        who.setDefaultLocale(QLocale::c());
        const QString zoneName = ([when]() {
#if QT_CONFIG(timezone)
            if (QLocale::c() != QLocale::system()) {
                const QString local = when.timeRepresentation().displayName(
                    when, QTimeZone::ShortName, QLocale::c());
                if (!local.isEmpty())
                    return local;
            }
#endif
            return when.timeZoneAbbreviation();
        })();
        const auto state = who.parse(when.toString(format), -1, when, false);
        QCOMPARE(state.state, QDateTimeParser::Acceptable);
        QVERIFY(!state.conflicts);
        QCOMPARE(state.padded, 0);
        QCOMPARE(state.value.timeSpec(), expect(zoneName));
        QCOMPARE(state.value, when);
    }
}

void tst_QDateTimeParser::parseSection_data()
{
    QTest::addColumn<QString>("format");
    QTest::addColumn<QString>("input");
    QTest::addColumn<int>("sectionIndex");
    QTest::addColumn<int>("offset");
    QTest::addColumn<QDTPUnitTestParser::ParsedSection>("expected");

    using ParsedSection = QDTPUnitTestParser::ParsedSection;
    using State = QDTPUnitTestParser::State;
    QTest::newRow("short-year-begin")
        << "yyyy_MM_dd" << "200_12_15" << 0 << 0
        << ParsedSection(State::Intermediate ,200, 3, 0);

    QTest::newRow("short-year-middle")
        << "MM-yyyy-dd" << "12-200-15" << 1 << 3
        << ParsedSection(State::Intermediate, 200, 3, 0);

    QTest::newRow("negative-year-middle-5")
        << "MM-yyyy-dd" << "12--2000-15" << 1 << 3
        << ParsedSection(State::Acceptable, -2000, 5, 0);

    QTest::newRow("short-negative-year-middle-4")
        << "MM-yyyy-dd" << "12--200-15" << 1 << 3
        << ParsedSection(State::Intermediate, -200, 4, 0);

    QTest::newRow("short-negative-year-middle-3")
        << "MM-yyyy-dd" << "12--20-15" << 1 << 3
        << ParsedSection(State::Intermediate, -20, 3, 0);

    QTest::newRow("short-negative-year-middle-2")
        << "MM-yyyy-dd" << "12--2-15" << 1 << 3
        << ParsedSection(State::Intermediate, -2, 2, 0);

    QTest::newRow("short-negative-year-middle-1")
        << "MM-yyyy-dd" << "12---15"  << 1 << 3
        << ParsedSection(State::Intermediate, 0, 1, 0);

    // Here the -15 will be understood as year, with separator and day omitted,
    // although it could equally be read as month and day with missing year.
    QTest::newRow("short-negative-year-middle-0")
        << "MM-yyyy-dd" << "12--15"  << 1 << 3
        << ParsedSection(State::Intermediate, -15, 3, 0);
}

void tst_QDateTimeParser::parseSection()
{
    QFETCH(QString, format);
    QFETCH(QString, input);
    QFETCH(int, sectionIndex);
    QFETCH(int, offset);
    QFETCH(QDTPUnitTestParser::ParsedSection, expected);

    QDTPUnitTestParser testParser;

    QVERIFY(testParser.parseFormat(format));
    QDateTime val(QDate(1900, 1, 1).startOfDay());

    testParser.setText(input);
    auto result = testParser.parseSection(val, sectionIndex, offset);
    QCOMPARE(result, expected);
}

void tst_QDateTimeParser::intermediateYear_data()
{
    QTest::addColumn<QString>("format");
    QTest::addColumn<QString>("input");
    QTest::addColumn<QDate>("expected");

    QTest::newRow("short-year-begin")
        << "yyyy_MM_dd" << "200_12_15" << QDate(200, 12, 15);
    QTest::newRow("short-year-mid")
        << "MM_yyyy_dd" << "12_200_15" << QDate(200, 12, 15);
    QTest::newRow("short-year-end")
        << "MM_dd_yyyy" << "12_15_200" << QDate(200, 12, 15);
}

void tst_QDateTimeParser::intermediateYear()
{
    QFETCH(QString, format);
    QFETCH(QString, input);
    QFETCH(QDate, expected);

    QDateTimeParser testParser(QMetaType::QDateTime, QDateTimeParser::DateTimeEdit);

    QVERIFY(testParser.parseFormat(format));

    // Indian/Cocos has a transition at the start of 1900, so it started this
    // day at 00:02:20, throwing a time offset into QDTP.
    QDateTime val(QDate(1900, 1, 1).startOfDay());
    const QDateTimeParser::StateNode tmp = testParser.parse(input, -1, val, false);
    QCOMPARE(tmp.state, QDateTimeParser::Intermediate);
    QCOMPARE(tmp.value.date(), expected);
}

QTEST_APPLESS_MAIN(tst_QDateTimeParser)

#include "tst_qdatetimeparser.moc"