1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include <QtTest/QtTest> |
30 | #include <QtCore/private/qfilesystemmetadata_p.h> |
31 | |
32 | class tst_QFileSystemMetaData : public QObject |
33 | { |
34 | Q_OBJECT |
35 | |
36 | private slots: |
37 | void timeSinceEpoch(); |
38 | }; |
39 | |
40 | #if defined(QT_BUILD_INTERNAL) && defined(QT_SHARED) |
41 | #ifdef Q_OS_WIN |
42 | static FILETIME epochToFileTime(long seconds) |
43 | { |
44 | const qint64 sec = 10000000; |
45 | // FILETIME is time in 1e-7s units since 1601's start: epoch is 1970's |
46 | // start, 369 years (of which 3*24 +69/4 = 89 were leap) later. |
47 | const qint64 offset = qint64(365 * 369 + 89) * 24 * 3600; |
48 | const qint64 convert = (offset + seconds) * sec; |
49 | FILETIME parts; |
50 | parts.dwHighDateTime = convert >> 32; |
51 | parts.dwLowDateTime = convert & 0xffffffff; |
52 | return parts; |
53 | } |
54 | #endif |
55 | |
56 | void tst_QFileSystemMetaData::timeSinceEpoch() |
57 | { |
58 | // Regression test for QTBUG-48306, used to fail for TZ=Russia/Moscow |
59 | // Oct 22 2014 6:00 UTC; TZ=Russia/Moscow changed from +4 to +3 on Oct 26. |
60 | const long afterEpochUtc = 1413957600L; |
61 | QFileSystemMetaData meta; |
62 | #ifdef Q_OS_WIN |
63 | WIN32_FIND_DATA data; |
64 | memset(&data, 0, sizeof(data)); |
65 | data.dwFileAttributes = FILE_ATTRIBUTE_NORMAL; |
66 | /* data.ftLastAccessTime = data.ftLastWriteTime = */ |
67 | data.ftCreationTime = epochToFileTime(afterEpochUtc); |
68 | meta.fillFromFindData(data); |
69 | QCOMPARE(meta.birthTime().toUTC(), |
70 | QDateTime::fromMSecsSinceEpoch(afterEpochUtc * qint64(1000), Qt::UTC)); |
71 | #else |
72 | QT_STATBUF data; |
73 | memset(s: &data, c: 0, n: sizeof(data)); |
74 | data.st_ctime = afterEpochUtc; |
75 | meta.fillFromStatBuf(statBuffer: data); |
76 | QCOMPARE(meta.metadataChangeTime().toUTC(), |
77 | QDateTime::fromMSecsSinceEpoch(afterEpochUtc * qint64(1000), Qt::UTC)); |
78 | #endif |
79 | } |
80 | #else // i.e. no Q_AUTOTEST_EXPORT |
81 | void tst_QFileSystemMetaData::timeSinceEpoch() |
82 | { |
83 | QSKIP("QFileSystemMetaData methods aren't available to test" ); |
84 | } |
85 | #endif |
86 | QTEST_MAIN(tst_QFileSystemMetaData) |
87 | #include <tst_qfilesystemmetadata.moc> |
88 | |