| 1 | // Copyright (C) 2023 Ahmad Samir <a.samirh78@gmail.com> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef Q20CHRONO_H |
| 5 | #define Q20CHRONO_H |
| 6 | |
| 7 | #include <QtCore/qtconfigmacros.h> |
| 8 | |
| 9 | #include <chrono> |
| 10 | |
| 11 | // |
| 12 | // W A R N I N G |
| 13 | // ------------- |
| 14 | // |
| 15 | // This file is not part of the Qt API. Types and functions defined in this |
| 16 | // file can reliably be replaced by their std counterparts, once available. |
| 17 | // You may use these definitions in your own code, but be aware that we |
| 18 | // will remove them once Qt depends on the C++ version that supports |
| 19 | // them in namespace std. There will be NO deprecation warning, the |
| 20 | // definitions will JUST go away. |
| 21 | // |
| 22 | // If you can't agree to these terms, don't use these definitions! |
| 23 | // |
| 24 | // We mean it. |
| 25 | // |
| 26 | |
| 27 | QT_BEGIN_NAMESPACE |
| 28 | |
| 29 | namespace q20 { |
| 30 | namespace chrono { |
| 31 | |
| 32 | #if defined(__GLIBCXX__) |
| 33 | // https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/include/bits/chrono.h |
| 34 | using IntRep = int64_t; |
| 35 | #else |
| 36 | // https://github.com/llvm/llvm-project/blob/main/libcxx/include/__chrono/duration.h |
| 37 | // https://github.com/microsoft/STL/blob/main/stl/inc/__msvc_chrono.hpp |
| 38 | using IntRep = int; |
| 39 | #endif |
| 40 | |
| 41 | #if __cpp_lib_chrono >= 201907L |
| 42 | using std::chrono::days; |
| 43 | using std::chrono::weeks; |
| 44 | using std::chrono::years; |
| 45 | using std::chrono::months; |
| 46 | |
| 47 | static_assert(std::is_same_v<days::rep, IntRep>); |
| 48 | static_assert(std::is_same_v<weeks::rep, IntRep>); |
| 49 | static_assert(std::is_same_v<years::rep, IntRep>); |
| 50 | static_assert(std::is_same_v<months::rep, IntRep>); |
| 51 | #else |
| 52 | using days = std::chrono::duration<IntRep, std::ratio<86400>>; |
| 53 | using weeks = std::chrono::duration<IntRep, std::ratio_multiply<std::ratio<7>, days::period>>; |
| 54 | using years = std::chrono::duration<IntRep, std::ratio_multiply<std::ratio<146097, 400>, days::period>>; |
| 55 | using months = std::chrono::duration<IntRep, std::ratio_divide<years::period, std::ratio<12>>>; |
| 56 | #endif // __cpp_lib_chrono >= 201907L |
| 57 | } // namespace chrono |
| 58 | } // namespace q20 |
| 59 | |
| 60 | QT_END_NAMESPACE |
| 61 | |
| 62 | #endif /* Q20CHRONO_H */ |
| 63 | |