Skip to content

Commit f5627f8

Browse files
redstrateLeonMatthesKDAB
authored andcommitted
cxx-qt-lib: Add binding for QDateTime::fromString
This gives cxx-qt users a way to parse QDateTime from a QString. This is using one of the QDateTime::fromString overloads.
1 parent 61ea381 commit f5627f8

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717

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

20+
### Added
21+
22+
- `QDateTime::from_string` to parse `QDateTime` from a `QString`.
23+
2024
### Fixed
2125

2226
- Build warnings due to unused unsafe blocks since CXX 1.0.130

crates/cxx-qt-lib/include/core/qdatetime.h

+2
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,7 @@ ::std::int64_t
6868
qdatetimeToSecsSinceEpoch(const QDateTime& datetime);
6969
void
7070
qdatetimeSetTimeZone(QDateTime& datetime, const QTimeZone& timeZone);
71+
QDateTime
72+
qdatetimeFromQString(const QString& string, Qt::DateFormat format);
7173
}
7274
}

crates/cxx-qt-lib/src/core/qdatetime.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,11 @@ qdatetimeSetTimeZone(QDateTime& datetime, const QTimeZone& timeZone)
154154
#endif
155155
}
156156

157+
QDateTime
158+
qdatetimeFromQString(const QString& string, const Qt::DateFormat format)
159+
{
160+
return QDateTime::fromString(string, format);
161+
}
162+
157163
}
158164
}

crates/cxx-qt-lib/src/core/qdatetime.rs

+13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod ffi {
1414
unsafe extern "C++" {
1515
include!("cxx-qt-lib/qt.h");
1616
type TimeSpec = crate::TimeSpec;
17+
type DateFormat = crate::DateFormat;
1718
}
1819

1920
unsafe extern "C++" {
@@ -169,6 +170,8 @@ mod ffi {
169170
fn qdatetimeToSecsSinceEpoch(datetime: &QDateTime) -> i64;
170171
#[rust_name = "qdatetime_settimezone"]
171172
fn qdatetimeSetTimeZone(datetime: &mut QDateTime, time_zone: &QTimeZone);
173+
#[rust_name = "qdatetime_from_string"]
174+
fn qdatetimeFromQString(string: &QString, format: DateFormat) -> QDateTime;
172175
}
173176

174177
#[namespace = "rust::cxxqtlib1"]
@@ -298,6 +301,16 @@ impl QDateTime {
298301
ffi::qdatetime_from_secs_since_epoch(secs, time_zone)
299302
}
300303

304+
/// Returns the datetime represented in the string as a QDateTime using the format given, or None if this is not possible.
305+
pub fn from_string(string: &ffi::QString, format: ffi::DateFormat) -> Option<Self> {
306+
let date = ffi::qdatetime_from_string(string, format);
307+
if date.is_valid() {
308+
Some(date)
309+
} else {
310+
None
311+
}
312+
}
313+
301314
/// Returns the number of milliseconds from this datetime to the other datetime.
302315
/// If the other datetime is earlier than this datetime, the value returned is negative.
303316
pub fn msecs_to(&self, other: &Self) -> i64 {

0 commit comments

Comments
 (0)