Skip to content

Commit

Permalink
cxx-qt-lib: Add binding for QDateTime::fromString
Browse files Browse the repository at this point in the history
This gives cxx-qt users a way to parse QDateTime from a QString. This
is using one of the QDateTime::fromString overloads.
  • Loading branch information
redstrate committed Dec 27, 2024
1 parent f5afefe commit e848d67
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/KDAB/cxx-qt/compare/v0.7.0...HEAD)

### Added

- `QDateTime::from_string` to parse `QDateTime` from a `QString`.

### Fixed

- Build warnings due to unused unsafe blocks since CXX 1.0.130
Expand Down
2 changes: 2 additions & 0 deletions crates/cxx-qt-lib/include/core/qdatetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,7 @@ ::std::int64_t
qdatetimeToSecsSinceEpoch(const QDateTime& datetime);
void
qdatetimeSetTimeZone(QDateTime& datetime, const QTimeZone& timeZone);
QDateTime
qdatetimeFromQString(const QString& string, Qt::DateFormat format);
}
}
6 changes: 6 additions & 0 deletions crates/cxx-qt-lib/src/core/qdatetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,11 @@ qdatetimeSetTimeZone(QDateTime& datetime, const QTimeZone& timeZone)
#endif
}

QDateTime
qdatetimeFromQString(const QString& string, const Qt::DateFormat format)
{
return QDateTime::fromString(string, format);
}

}
}
13 changes: 13 additions & 0 deletions crates/cxx-qt-lib/src/core/qdatetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod ffi {
unsafe extern "C++" {
include!("cxx-qt-lib/qt.h");
type TimeSpec = crate::TimeSpec;
type DateFormat = crate::DateFormat;
}

unsafe extern "C++" {
Expand Down Expand Up @@ -169,6 +170,8 @@ mod ffi {
fn qdatetimeToSecsSinceEpoch(datetime: &QDateTime) -> i64;
#[rust_name = "qdatetime_settimezone"]
fn qdatetimeSetTimeZone(datetime: &mut QDateTime, time_zone: &QTimeZone);
#[rust_name = "qdatetime_from_string"]
fn qdatetimeFromQString(string: &QString, format: DateFormat) -> QDateTime;
}

#[namespace = "rust::cxxqtlib1"]
Expand Down Expand Up @@ -298,6 +301,16 @@ impl QDateTime {
ffi::qdatetime_from_secs_since_epoch(secs, time_zone)
}

/// Returns the datetime represented in the string as a QDateTime using the format given, or None if this is not possible.
pub fn from_string(string: &ffi::QString, format: ffi::DateFormat) -> Option<Self> {
let date = ffi::qdatetime_from_string(string, format);
if date.is_valid() {
Some(date)
} else {
None
}
}

/// Returns the number of milliseconds from this datetime to the other datetime.
/// If the other datetime is earlier than this datetime, the value returned is negative.
pub fn msecs_to(&self, other: &Self) -> i64 {
Expand Down

0 comments on commit e848d67

Please sign in to comment.