Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cxx-qt-lib: qdatetime put deprecated behind cfg flag #1109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions crates/cxx-qt-lib/src/core/qdatetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ mod ffi {
fn offsetFromUtc(self: &QDateTime) -> i32;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remaining issue appears to be with Qt 6.8 the linking order is incorrect with Qt6QuickControls2 being linked before our staticlib


/// Sets the timeSpec() to Qt::OffsetFromUTC and the offset to offsetSeconds.
#[cfg(not(cxxqt_qt_version_at_least_6_8))]
#[rust_name = "set_offset_from_utc"]
fn setOffsetFromUtc(self: &mut QDateTime, offset_seconds: i32);

/// Sets the time specification used in this datetime to spec. The datetime will refer to a different point in time.
#[cfg(not(cxxqt_qt_version_at_least_6_8))]
#[rust_name = "set_time_spec"]
fn setTimeSpec(self: &mut QDateTime, spec: TimeSpec);

Expand All @@ -83,6 +85,7 @@ mod ffi {
fn toOffsetFromUtc(self: &QDateTime, offset_seconds: i32) -> QDateTime;

/// Returns a copy of this datetime converted to the given time spec.
#[cfg(not(cxxqt_qt_version_at_least_6_8))]
#[rust_name = "to_time_spec"]
fn toTimeSpec(self: &QDateTime, spec: TimeSpec) -> QDateTime;

Expand Down Expand Up @@ -175,6 +178,7 @@ mod ffi {
#[doc(hidden)]
#[rust_name = "qdatetime_init_from_date_and_time_time_zone"]
fn construct(date: &QDate, time: &QTime, time_zone: &QTimeZone) -> QDateTime;
#[cfg(not(cxxqt_qt_version_at_least_6_8))]
#[doc(hidden)]
#[rust_name = "qdatetime_init_from_date_and_time_time_spec"]
fn construct(
Expand Down Expand Up @@ -264,6 +268,7 @@ impl QDateTime {
}

/// Construct a Rust QDateTime from a given QDate, QTime, Qt::TimeSpec, and offset
#[cfg(not(cxxqt_qt_version_at_least_6_8))]
pub fn from_date_and_time_time_spec(
date: &QDate,
time: &QTime,
Expand Down Expand Up @@ -398,11 +403,11 @@ impl<Tz: chrono::TimeZone> TryFrom<chrono::DateTime<Tz>> for QDateTime {
type Error = &'static str;

fn try_from(value: chrono::DateTime<Tz>) -> Result<Self, Self::Error> {
Ok(QDateTime::from_date_and_time_time_spec(
let tz = crate::QTimeZone::from_offset_seconds(value.offset().fix().local_minus_utc());
Ok(QDateTime::from_date_and_time_time_zone(
&QDate::from(value.date_naive()),
&QTime::try_from(value.time())?,
ffi::TimeSpec::OffsetFromUTC,
value.offset().fix().local_minus_utc(),
tz.as_ref().ok_or("Could not construct timezone")?,
))
}
}
Expand Down Expand Up @@ -444,23 +449,23 @@ impl TryFrom<QDateTime> for chrono::DateTime<chrono::Utc> {
#[cfg(feature = "time")]
impl From<time::OffsetDateTime> for QDateTime {
fn from(value: time::OffsetDateTime) -> Self {
QDateTime::from_date_and_time_time_spec(
let tz = crate::QTimeZone::from_offset_seconds(value.offset().whole_seconds());
QDateTime::from_date_and_time_time_zone(
&QDate::from(value.date()),
&QTime::from(value.time()),
ffi::TimeSpec::OffsetFromUTC,
value.offset().whole_seconds(),
tz.as_ref().expect("Could not construct timezone"),
)
}
}

#[cfg(feature = "time")]
impl From<time::PrimitiveDateTime> for QDateTime {
fn from(value: time::PrimitiveDateTime) -> Self {
QDateTime::from_date_and_time_time_spec(
let tz = crate::QTimeZone::utc();
QDateTime::from_date_and_time_time_zone(
&QDate::from(value.date()),
&QTime::from(value.time()),
ffi::TimeSpec::UTC,
0,
tz.as_ref().expect("Could not construct timezone"),
)
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/qt_types_standalone/cpp/qdatetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ private Q_SLOTS:

void read()
{
QVERIFY(
read_qdatetime(QDateTime(QDate(2022, 1, 1), QTime(1, 2, 3, 4), Qt::UTC),
QDate(2022, 1, 1),
QTime(1, 2, 3, 4)));
QVERIFY(read_qdatetime(
QDateTime(QDate(2022, 1, 1), QTime(1, 2, 3, 4), QTimeZone::utc()),
QDate(2022, 1, 1),
QTime(1, 2, 3, 4)));
}

void clone()
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/cpp/qvariant.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private Q_SLOTS:
<< VariantTest::QDate;
QTest::newRow("QDateTime")
<< QVariant::fromValue<QDateTime>(
QDateTime(QDate(2021, 12, 31), QTime(4, 3, 2, 1), Qt::UTC))
QDateTime(QDate(2021, 12, 31), QTime(4, 3, 2, 1), QTimeZone::utc()))
<< VariantTest::QDateTime;
QTest::newRow("QPoint")
<< QVariant::fromValue<QPoint>(QPoint(8, 9)) << VariantTest::QPoint;
Expand Down
Loading