Skip to content

Commit

Permalink
removed deprecated up to Qt v6.9 (Qt::TimeSpec) πŸŽ‰
Browse files Browse the repository at this point in the history
 - removed using of QDateTime methods using plain Qt::TimeSpec
 - set QT_DISABLE_DEPRECATED_UP_TO to 0x060900 (also 0x061000 works)
 - maintained Qt v5 and <v6.5 code still 😬 (next step will be to drop
   Qt v5 πŸŽ‰)
 - updated auto tests

See:
qt/qtbase@8c8d6ff?diff=unified&w=0
  • Loading branch information
silverqx committed Jun 19, 2024
1 parent 318012f commit 11156a6
Show file tree
Hide file tree
Showing 30 changed files with 479 additions and 330 deletions.
7 changes: 5 additions & 2 deletions cmake/CommonModules/TinyCommon.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ ${TINY_UNPARSED_ARGUMENTS}")
# Disable deprecated APIs up to the given Qt version
# TODO qt5 remove silverqx
if(QT_VERSION_MAJOR GREATER_EQUAL 6)
# Disable all the APIs deprecated up to Qt v6.7.0 (including)
target_compile_definitions(${target} INTERFACE QT_DISABLE_DEPRECATED_UP_TO=0x060700)
# Disable all the APIs deprecated up to Qt v6.9.0 (including)
target_compile_definitions(${target} INTERFACE QT_DISABLE_DEPRECATED_UP_TO=0x060900)
else()
# Disable all the APIs deprecated up to Qt v6.0.0 (including)
target_compile_definitions(${target} INTERFACE QT_DISABLE_DEPRECATED_BEFORE=0x060000)
endif()

target_compile_definitions(${target}
INTERFACE
#QT_NO_DEPRECATED_WARNINGS
#QT_WARN_DEPRECATED_UP_TO=0x060900

#QT_ASCII_CAST_WARNINGS
#QT_NO_CAST_FROM_ASCII
#QT_RESTRICTED_CAST_FROM_ASCII
Expand Down
9 changes: 5 additions & 4 deletions examples/tom/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

using Orm::DatabaseManager;
using Orm::DB;
using Orm::QtTimeZoneConfig;

using ConfigUtils = Orm::Utils::Configuration;

Expand Down Expand Up @@ -86,7 +87,7 @@ std::shared_ptr<DatabaseManager> setupDatabaseManager()
/* Specifies what time zone all QDateTime-s will have, the overridden default
is the Qt::UTC, set to the Qt::LocalTime or QtTimeZoneType::DontConvert to
use the system local time. */
{qt_timezone, QVariant::fromValue(Qt::UTC)},
{qt_timezone, QVariant::fromValue(QtTimeZoneConfig::utc())},
{prefix_, EMPTY},
{prefix_indexes, false},
{strict_, true},
Expand All @@ -112,7 +113,7 @@ std::shared_ptr<DatabaseManager> setupDatabaseManager()
/* Specifies what time zone all QDateTime-s will have, the overridden default
is the Qt::UTC, set to the Qt::LocalTime or QtTimeZoneType::DontConvert to
use the system local time. */
{qt_timezone, QVariant::fromValue(Qt::UTC)},
{qt_timezone, QVariant::fromValue(QtTimeZoneConfig::utc())},
{prefix_, EMPTY},
{prefix_indexes, false},
{strict_, true},
Expand All @@ -139,7 +140,7 @@ std::shared_ptr<DatabaseManager> setupDatabaseManager()
/* Specifies what time zone all QDateTime-s will have, the overridden default
is the Qt::UTC, set to the Qt::LocalTime or QtTimeZoneType::DontConvert to
use the system local time. */
{qt_timezone, QVariant::fromValue(Qt::UTC)},
{qt_timezone, QVariant::fromValue(QtTimeZoneConfig::utc())},
// Examples of qt_timezone
// {qt_timezone, QVariant::fromValue(QTimeZone("Europe/Bratislava"))},
// {qt_timezone, "Europe/Prague"}, // Will be converted to QTimeZone("Europe/Prague")
Expand Down Expand Up @@ -167,7 +168,7 @@ std::shared_ptr<DatabaseManager> setupDatabaseManager()
/* Specifies what time zone all QDateTime-s will have, the overridden default
is the Qt::UTC, set to the Qt::LocalTime or QtTimeZoneType::DontConvert to
use the system local time. */
{qt_timezone, QVariant::fromValue(Qt::UTC)},
{qt_timezone, QVariant::fromValue(QtTimeZoneConfig::utc())},
/* Return a QDateTime/QDate with the correct time zone instead of the QString,
only works when the qt_timezone isn't set to the DontConvert. */
{return_qdatetime, true},
Expand Down
2 changes: 1 addition & 1 deletion include/orm/connectors/connectionfactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace Connectors
createConnection(
const QString &driver, std::function<ConnectionName()> &&connection,
QString &&database, QString &&tablePrefix = "",
QtTimeZoneConfig &&qtTimeZone = {QtTimeZoneType::QtTimeSpec, Qt::UTC},
QtTimeZoneConfig &&qtTimeZone = QtTimeZoneConfig::utc(),
QVariantHash &&config = {},
std::optional<bool> returnQDateTime = std::nullopt);

Expand Down
4 changes: 2 additions & 2 deletions include/orm/databaseconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ namespace Orm
explicit DatabaseConnection(
std::function<Connectors::ConnectionName()> &&connection,
QString &&database = "", QString &&tablePrefix = "",
QtTimeZoneConfig &&qtTimeZone = {QtTimeZoneType::QtTimeSpec, Qt::UTC},
QtTimeZoneConfig &&qtTimeZone = QtTimeZoneConfig::utc(),
QVariantHash &&config = {});
/*! Protected constructor for SQLite connection. */
explicit DatabaseConnection(
std::function<Connectors::ConnectionName()> &&connection,
QString &&database = "", QString &&tablePrefix = "",
QtTimeZoneConfig &&qtTimeZone = {QtTimeZoneType::QtTimeSpec, Qt::UTC},
QtTimeZoneConfig &&qtTimeZone = QtTimeZoneConfig::utc(),
std::optional<bool> returnQDateTime = true,
QVariantHash &&config = {});

Expand Down
4 changes: 2 additions & 2 deletions include/orm/mysqlconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Orm
explicit MySqlConnection(
std::function<Connectors::ConnectionName()> &&connection,
QString &&database = "", QString &&tablePrefix = "",
QtTimeZoneConfig &&qtTimeZone = {QtTimeZoneType::QtTimeSpec, Qt::UTC},
QtTimeZoneConfig &&qtTimeZone = QtTimeZoneConfig::utc(),
QVariantHash &&config = {});

public:
Expand All @@ -33,7 +33,7 @@ namespace Orm
std::shared_ptr<MySqlConnection>
create(std::function<Connectors::ConnectionName()> &&connection,
QString &&database = "", QString &&tablePrefix = "",
QtTimeZoneConfig &&qtTimeZone = {QtTimeZoneType::QtTimeSpec, Qt::UTC},
QtTimeZoneConfig &&qtTimeZone = QtTimeZoneConfig::utc(),
QVariantHash &&config = {});

/* Getters / Setters */
Expand Down
55 changes: 52 additions & 3 deletions include/orm/ormtypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,25 +177,74 @@ namespace Query
{
/*! Don't convert time zone. */
DontConvert,
/*! QtTimeZoneConfig contains Qt::TimeSpec, use toTimeSpec() for conversion. */
QtTimeSpec,
/*! QtTimeZoneConfig contains the QTimeZone, use toTimeZone() for conversion. */
QTimeZone,
/*! QtTimeZoneConfig contains seconds, use toOffsetFromUtc() for conversion. */
OffsetFromUtc,
#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
/*! QtTimeZoneConfig contains Qt::TimeSpec, use toTimeSpec() for conversion. */
QtTimeSpec,
#endif
};

/* The TTimeZone alias can be used on Qt v5, Qt <v6.5, and also Qt >=v6.5.
It helps to support all Qt versions still. */
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
/*! Alias for QTimeZone::Initialization. */
using TTimeZone = QTimeZone::Initialization;
#else
/*! Alias for Qt::TimeSpec. */
using TTimeZone = Qt::TimeSpec;
#endif

/*! Determine how the QDateTime time zone will be converted, it's saved
in the qt_timezone database connection's configuration option. */
struct QtTimeZoneConfig
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
/*! Time zone type saved in the value data member. */
QtTimeZoneType type {QtTimeZoneType::QTimeZone};
/*! Time zone value (UTC by default). */
QVariant value {QVariant::fromValue(QTimeZone(QTimeZone::UTC))}; // UTC is the default timezone, not the QTimeZone::LocalTime (it overrides this default)
#else
/*! Time zone type saved in the value data member. */
QtTimeZoneType type {QtTimeZoneType::QtTimeSpec};
/*! Time zone value. */
/*! Time zone value (UTC by default). */
QVariant value {Qt::UTC}; // UTC is the default timezone, not the Qt::LocalTime (it overrides this default)
#endif

/*! Equality comparison operator for the QtTimeZoneConfig. */
bool operator==(const QtTimeZoneConfig &) const = default;

/*! Factory method to create QtTimeZoneConfig as QTimeZone::UTC instance. */
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
inline static QtTimeZoneConfig utc() noexcept
#else
inline static QtTimeZoneConfig utc()
#endif
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
return {QtTimeZoneType::QTimeZone,
QVariant::fromValue(QTimeZone(QTimeZone::UTC))};
#else
return {QtTimeZoneType::QtTimeSpec, Qt::UTC};
#endif
}

/*! Factory method to create QtTimeZoneConfig as QTimeZone::LocalTime instance. */
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
inline static QtTimeZoneConfig localTime() noexcept
#else
inline static QtTimeZoneConfig localTime()
#endif
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
return {QtTimeZoneType::QTimeZone,
QVariant::fromValue(QTimeZone(QTimeZone::LocalTime))};
#else
return {QtTimeZoneType::QtTimeSpec, Qt::LocalTime};
#endif
}
};

} // namespace Orm
Expand Down
4 changes: 2 additions & 2 deletions include/orm/postgresconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Orm
explicit PostgresConnection(
std::function<Connectors::ConnectionName()> &&connection,
QString &&database = "", QString &&tablePrefix = "",
QtTimeZoneConfig &&qtTimeZone = {QtTimeZoneType::QtTimeSpec, Qt::UTC},
QtTimeZoneConfig &&qtTimeZone = QtTimeZoneConfig::utc(),
QVariantHash &&config = {});

public:
Expand All @@ -35,7 +35,7 @@ namespace Orm
std::shared_ptr<PostgresConnection>
create(std::function<Connectors::ConnectionName()> &&connection,
QString &&database = "", QString &&tablePrefix = "",
QtTimeZoneConfig &&qtTimeZone = {QtTimeZoneType::QtTimeSpec, Qt::UTC},
QtTimeZoneConfig &&qtTimeZone = QtTimeZoneConfig::utc(),
QVariantHash &&config = {});

/* Getters */
Expand Down
4 changes: 2 additions & 2 deletions include/orm/sqliteconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Orm
explicit SQLiteConnection(
std::function<Connectors::ConnectionName()> &&connection,
QString &&database = "", QString &&tablePrefix = "",
QtTimeZoneConfig &&qtTimeZone = {QtTimeZoneType::QtTimeSpec, Qt::UTC},
QtTimeZoneConfig &&qtTimeZone = QtTimeZoneConfig::utc(),
std::optional<bool> returnQDateTime = true,
QVariantHash &&config = {});

Expand All @@ -34,7 +34,7 @@ namespace Orm
std::shared_ptr<SQLiteConnection>
create(std::function<Connectors::ConnectionName()> &&connection,
QString &&database = "", QString &&tablePrefix = "",
QtTimeZoneConfig &&qtTimeZone = {QtTimeZoneType::QtTimeSpec, Qt::UTC},
QtTimeZoneConfig &&qtTimeZone = QtTimeZoneConfig::utc(),
std::optional<bool> returnQDateTime = true,
QVariantHash &&config = {});

Expand Down
7 changes: 5 additions & 2 deletions qmake/common/common.pri
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ CONFIG -= c++11 app_bundle
# ---

# Disable deprecated APIs up to the given Qt version
# Disable all the APIs deprecated up to Qt v6.7.0 (including)
# Disable all the APIs deprecated up to Qt v6.9.0 (including)
# TODO qt5 remove silverqx
versionAtLeast(QT_VERSION, 6): \
DEFINES *= QT_DISABLE_DEPRECATED_UP_TO=0x060800
DEFINES *= QT_DISABLE_DEPRECATED_UP_TO=0x060900
# Disable all the APIs deprecated up to Qt v6.0.0 (including)
else: \
DEFINES *= QT_DISABLE_DEPRECATED_BEFORE=0x060000

#DEFINES *= QT_NO_DEPRECATED_WARNINGS
#DEFINES *= QT_WARN_DEPRECATED_UP_TO=0x060900

#DEFINES *= QT_ASCII_CAST_WARNINGS
#DEFINES *= QT_NO_CAST_FROM_ASCII
#DEFINES *= QT_RESTRICTED_CAST_FROM_ASCII
Expand Down
19 changes: 13 additions & 6 deletions src/orm/utils/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ QtTimeZoneConfig
Configuration::prepareQtTimeZone(const QVariantHash &config, const QString &connection)
{
if (!config.contains(qt_timezone))
return {QtTimeZoneType::QtTimeSpec, Qt::UTC};
return QtTimeZoneConfig::utc();

return prepareQtTimeZone(config[qt_timezone], connection);
}
Expand All @@ -52,12 +52,23 @@ Configuration::prepareQtTimeZone(const QVariant &qtTimeZone, const QString &conn
{
// Nothing to do
if (!qtTimeZone.isValid() || qtTimeZone.isNull())
return {QtTimeZoneType::QtTimeSpec, Qt::UTC};
return QtTimeZoneConfig::utc();

const auto typeId = Helpers::qVariantTypeId(qtTimeZone);

#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
if (typeId == QMetaType::fromType<QTimeZone::Initialization>().id())
return {QtTimeZoneType::QTimeZone,
QVariant::fromValue(
QTimeZone(qtTimeZone.value<QTimeZone::Initialization>()))};
#else
if (typeId == QMetaType::fromType<Qt::TimeSpec>().id())
return {QtTimeZoneType::QtTimeSpec, qtTimeZone.value<Qt::TimeSpec>()};
#endif

if (typeId == QMetaType::fromType<QTimeZone>().id())
return {QtTimeZoneType::QTimeZone,
QVariant::fromValue(qtTimeZone.value<QTimeZone>())};

// Allow to define the QtTimeZoneType::DontConvert for the qt_timezone config. option
if (typeId == QMetaType::fromType<QtTimeZoneType>().id())
Expand All @@ -66,10 +77,6 @@ Configuration::prepareQtTimeZone(const QVariant &qtTimeZone, const QString &conn
)
return {QtTimeZoneType::DontConvert, {}};

if (typeId == QMetaType::fromType<QTimeZone>().id())
return {QtTimeZoneType::QTimeZone,
QVariant::fromValue(qtTimeZone.value<QTimeZone>())};

/* Must be a valid IANA timezone ID and must be available on a current system.
Also if a configuration option is the QString then return the QTimeZone
right away, to avoid re-computations in the Connection::prepareBinding(). */
Expand Down
42 changes: 34 additions & 8 deletions src/orm/utils/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,25 @@ Helpers::convertTimeZone(const QDateTime &datetime, const QtTimeZoneConfig &time

switch (timezoneType) {
T_LIKELY
case QtTimeZoneType::QtTimeSpec:
return datetime.toTimeSpec(timezoneValue.value<Qt::TimeSpec>());
case QtTimeZoneType::QTimeZone:
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
if (const auto typeId = Helpers::qVariantTypeId(timezoneValue);
typeId == QMetaType::fromType<QTimeZone::Initialization>().id()
)
return datetime.toTimeZone(timezoneValue.value<QTimeZone::Initialization>());
else if (typeId == QMetaType::fromType<QTimeZone>().id())
#endif
return datetime.toTimeZone(timezoneValue.value<QTimeZone>());
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
else
Q_UNREACHABLE();
#endif

#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
T_LIKELY
case QtTimeZoneType::QTimeZone:
return datetime.toTimeZone(timezoneValue.value<QTimeZone>());
case QtTimeZoneType::QtTimeSpec:
return datetime.toTimeSpec(timezoneValue.value<Qt::TimeSpec>());
#endif

T_UNLIKELY
case QtTimeZoneType::OffsetFromUtc:
Expand All @@ -110,19 +123,32 @@ Helpers::setTimeZone(QDateTime &datetime, const QtTimeZoneConfig &timezone)

switch (timezoneType) {
T_LIKELY
case QtTimeZoneType::QtTimeSpec:
datetime.setTimeSpec(timezoneValue.value<Qt::TimeSpec>());
case QtTimeZoneType::QTimeZone:
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
if (const auto typeId = Helpers::qVariantTypeId(timezoneValue);
typeId == QMetaType::fromType<QTimeZone::Initialization>().id()
)
datetime.setTimeZone(timezoneValue.value<QTimeZone::Initialization>());
else if (typeId == QMetaType::fromType<QTimeZone>().id())
#endif
datetime.setTimeZone(timezoneValue.value<QTimeZone>());
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
else
Q_UNREACHABLE();
#endif
break;

#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
T_LIKELY
case QtTimeZoneType::QTimeZone:
datetime.setTimeZone(timezoneValue.value<QTimeZone>());
case QtTimeZoneType::QtTimeSpec:
datetime.setTimeSpec(timezoneValue.value<Qt::TimeSpec>());
break;

T_UNLIKELY
case QtTimeZoneType::OffsetFromUtc:
datetime.setOffsetFromUtc(timezoneValue.value<int>());
break;
#endif

T_UNLIKELY
default:
Expand Down
Loading

0 comments on commit 11156a6

Please sign in to comment.