From bde5eeecaf35f7b7a94b7878289a8e7fcd057afa Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sat, 8 Apr 2023 23:24:17 +0200 Subject: [PATCH] q20: add C++20 chrono days/weeks/months/years Change-Id: Ie2a167cbe6672694e15b7810daf7fad3fe0656a1 Reviewed-by: Thiago Macieira --- src/corelib/CMakeLists.txt | 1 + src/corelib/global/q20chrono.h | 62 ++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/corelib/global/q20chrono.h diff --git a/src/corelib/CMakeLists.txt b/src/corelib/CMakeLists.txt index 489647d1a4..d229a9dbc0 100644 --- a/src/corelib/CMakeLists.txt +++ b/src/corelib/CMakeLists.txt @@ -99,6 +99,7 @@ qt_internal_add_module(Core ${core_version_tagging_files} global/qvolatile_p.h global/q20algorithm.h + global/q20chrono.h global/q20functional.h global/q20iterator.h global/q20memory.h diff --git a/src/corelib/global/q20chrono.h b/src/corelib/global/q20chrono.h new file mode 100644 index 0000000000..177d307e21 --- /dev/null +++ b/src/corelib/global/q20chrono.h @@ -0,0 +1,62 @@ +// Copyright (C) 2023 Ahmad Samir +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef Q20CHRONO_H +#define Q20CHRONO_H + +#include + +#include + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. Types and functions defined in this +// file can reliably be replaced by their std counterparts, once available. +// You may use these definitions in your own code, but be aware that we +// will remove them once Qt depends on the C++ version that supports +// them in namespace std. There will be NO deprecation warning, the +// definitions will JUST go away. +// +// If you can't agree to these terms, don't use these definitions! +// +// We mean it. +// + +QT_BEGIN_NAMESPACE + +namespace q20 { +namespace chrono { + +#if defined(__GLIBCXX__) +// https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/include/bits/chrono.h +using IntRep = int64_t; +#else +// https://github.com/llvm/llvm-project/blob/main/libcxx/include/__chrono/duration.h +// https://github.com/microsoft/STL/blob/main/stl/inc/__msvc_chrono.hpp +using IntRep = int; +#endif + +#if __cpp_lib_chrono >= 201611L && __cplusplus > 201703L +using std::chrono::days; +using std::chrono::weeks; +using std::chrono::years; +using std::chrono::months; + +static_assert(std::is_same_v); +static_assert(std::is_same_v); +static_assert(std::is_same_v); +static_assert(std::is_same_v); +#else // __cpp_lib_chrono >= 201907L +using days = std::chrono::duration>; +using weeks = std::chrono::duration, days::period>>; +using years = std::chrono::duration, days::period>>; +using months = std::chrono::duration>>; +#endif // __cpp_lib_chrono >= 201611L +} // namespace chrono +} // namespace q20 + +QT_END_NAMESPACE + +#endif /* Q20CHRONO_H */