1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
use cxx::{type_id, ExternType};
use std::fmt;
#[cxx::bridge]
mod ffi {
#[namespace = "Qt"]
unsafe extern "C++" {
include!("cxx-qt-lib/qt.h");
type DateFormat = crate::DateFormat;
}
unsafe extern "C++" {
include!("cxx-qt-lib/qtime.h");
include!("cxx-qt-lib/qstring.h");
type QTime = super::QTime;
type QString = crate::QString;
/// Returns the current time as reported by the system clock.
///
/// Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.
///
/// Furthermore, the value only increases within each day; it shall drop by 24 hours each time midnight passes; and, beside this, changes in it may not correspond to elapsed time, if a daylight-saving transition intervenes.
#[Self = "QTime"]
#[rust_name = "current_time"]
fn currentTime() -> QTime;
/// Returns a new `QTime` instance with the time set to the number of `msecs`
/// since the start of the day, i.e. since 00:00:00.
///
/// If `msecs` falls outside the valid range an invalid `QTime` will be returned.
#[Self = "QTime"]
#[rust_name = "from_msecs_since_start_of_day"]
fn fromMSecsSinceStartOfDay(msecs: i32) -> QTime;
/// Returns `true` if the specified time is valid; otherwise returns `false`.
/// The time is valid if `h` is in the range 0 to 23, `m` and `s` are in the range 0 to 59, and `ms` is in the range 0 to 999.
#[Self = "QTime"]
#[rust_name = "is_valid_time"]
pub fn isValid(h: i32, m: i32, s: i32, ms: i32) -> bool;
/// Returns a `QTime` object containing a time `ms` milliseconds later
/// than the time of this object (or earlier if `ms` is negative).
///
/// Note that the time will wrap if it passes midnight.
///
/// Returns a null time if this time is invalid.
#[rust_name = "add_msecs"]
fn addMSecs(self: &QTime, ms: i32) -> QTime;
/// Returns a `QTime` object containing a time `s` seconds later than the
/// time of this object (or earlier if `s` is negative).
///
/// Note that the time will wrap if it passes midnight.
///
/// Returns a null time if this time is invalid.
#[rust_name = "add_secs"]
fn addSecs(self: &QTime, s: i32) -> QTime;
/// Returns the hour part (0 to 23) of the time.
///
/// Returns -1 if the time is invalid.
fn hour(self: &QTime) -> i32;
/// Returns `true` if the time is null (i.e., the `QTime` object was
/// constructed using the default constructor); otherwise returns `false`.
/// A null time is also an invalid time.
#[rust_name = "is_null"]
fn isNull(self: &QTime) -> bool;
/// Returns `true` if the time is valid; otherwise returns `false`. For example, the time 23:30:55.746 is valid, but 24:12:30 is invalid.
#[rust_name = "is_valid"]
fn isValid(self: &QTime) -> bool;
/// Returns the minute part (0 to 59) of the time.
///
/// Returns -1 if the time is invalid.
fn minute(self: &QTime) -> i32;
/// Returns the millisecond part (0 to 999) of the time.
///
/// Returns -1 if the time is invalid.
fn msec(self: &QTime) -> i32;
/// Returns the number of msecs since the start of the day, i.e. since 00:00:00.
#[rust_name = "msecs_since_start_of_day"]
fn msecsSinceStartOfDay(self: &QTime) -> i32;
/// Returns the second part (0 to 59) of the time.
///
/// Returns -1 if the time is invalid.
fn second(self: &QTime) -> i32;
/// Sets the time to hour `h`, minute `m`, seconds `s` and milliseconds `ms`.
#[rust_name = "set_hms"]
fn setHMS(self: &mut QTime, h: i32, m: i32, s: i32, ms: i32) -> bool;
/// Returns the time as a string. The `format` parameter determines the format of the string.
#[rust_name = "format"]
fn toString(self: &QTime, format: &QString) -> QString;
/// Returns the time as a string. The `format` parameter determines the format of the string.
#[rust_name = "format_enum"]
fn toString(self: &QTime, format: DateFormat) -> QString;
}
#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++" {
#[doc(hidden)]
#[rust_name = "qtime_from_string"]
fn qtimeFromString(string: &QString, format: &QString) -> QTime;
#[doc(hidden)]
#[rust_name = "qtime_from_string_enum"]
fn qtimeFromString(string: &QString, format: DateFormat) -> QTime;
#[doc(hidden)]
#[rust_name = "qtime_msecs_to"]
fn qtimeMSecsTo(time: &QTime, t: QTime) -> i32;
#[doc(hidden)]
#[rust_name = "qtime_secs_to"]
fn qtimeSecsTo(time: &QTime, t: QTime) -> i32;
}
#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++" {
include!("cxx-qt-lib/common.h");
#[doc(hidden)]
#[rust_name = "qtime_init_default"]
fn construct() -> QTime;
#[doc(hidden)]
#[rust_name = "qtime_init"]
fn construct(h: i32, m: i32, s: i32, ms: i32) -> QTime;
#[doc(hidden)]
#[rust_name = "qtime_to_debug_qstring"]
fn toDebugQString(value: &QTime) -> QString;
}
}
/// The `QTime` class provides clock time functions.
///
/// Qt Documentation: [QTime](https://doc.qt.io/qt/qtime.html#details)
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(C)]
pub struct QTime {
mds: i32,
}
impl QTime {
/// Returns the time represented in the `string` as a `QTime` using the `format` given, or an invalid time if the string cannot be parsed.
pub fn from_string(string: &QString, format: &QString) -> Self {
ffi::qtime_from_string(string, format)
}
/// Returns the time represented in the `string` as a `QTime` using the `format` given, or an invalid time if this is not possible.
pub fn from_string_enum(string: &QString, format: DateFormat) -> Self {
ffi::qtime_from_string_enum(string, format)
}
/// Returns the number of milliseconds from this time to `t`.
/// If `t` is earlier than this time, the number of milliseconds returned is negative.
///
/// Because `QTime` measures time within a day and there are 86400 seconds in a day, the result is always between -86400000 and 86400000 ms.
///
/// Returns 0 if either time is invalid.
pub fn msecs_to(&self, t: Self) -> i32 {
ffi::qtime_msecs_to(self, t)
}
/// Constructs a time with hour `h`, minute `m`, seconds `s` and milliseconds `ms`.
pub fn new(h: i32, m: i32, s: i32, ms: i32) -> Self {
ffi::qtime_init(h, m, s, ms)
}
/// Returns the number of seconds from this time to `t`.
/// If `t` is earlier than this time, the number of seconds returned is negative.
///
/// Because `QTime` measures time within a day and there are 86400 seconds in a day, the result is always between -86400 and 86400.
///
/// This function does not take into account any milliseconds.
///
/// Returns 0 if either time is invalid.
pub fn secs_to(&self, t: Self) -> i32 {
ffi::qtime_secs_to(self, t)
}
}
impl Default for QTime {
/// Constructs a null time object.
fn default() -> Self {
ffi::qtime_init_default()
}
}
impl fmt::Display for QTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.format_enum(DateFormat::TextDate).fmt(f)
}
}
impl fmt::Debug for QTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ffi::qtime_to_debug_qstring(self).fmt(f)
}
}
#[cfg(feature = "chrono")]
use chrono::Timelike;
use crate::{DateFormat, QString};
#[cfg(feature = "chrono")]
impl TryFrom<chrono::NaiveTime> for QTime {
type Error = &'static str;
/// Errors if [`NaiveTime`](chrono::NaiveTime) has milliseconds larger than 999,
/// as Qt does not support representing a leap second in this way.
fn try_from(value: chrono::NaiveTime) -> Result<Self, Self::Error> {
let ms = (value.nanosecond() / 1_000_000) as i32;
// NaiveTime can have a nanosecond larger than 1 second
// to represent a leap second.
//
// Qt has no way to represent this, we could add 1 second but
// when then happens if the time is 23:59:59 + 1 ?
if ms > 999 {
return Err("leap second is not supported in Qt");
}
Ok(QTime::new(
value.hour() as i32,
value.minute() as i32,
value.second() as i32,
ms,
))
}
}
#[cfg(feature = "chrono")]
impl TryFrom<QTime> for chrono::NaiveTime {
type Error = &'static str;
fn try_from(value: QTime) -> Result<Self, Self::Error> {
chrono::NaiveTime::from_hms_milli_opt(
value.hour() as u32,
value.minute() as u32,
value.second() as u32,
value.msec() as u32,
)
.ok_or("invalid hour, minute, second and/or millisecond")
}
}
#[cfg(feature = "time")]
impl From<time::Time> for QTime {
fn from(value: time::Time) -> Self {
QTime::new(
value.hour() as i32,
value.minute() as i32,
value.second() as i32,
value.millisecond() as i32,
)
}
}
#[cfg(feature = "time")]
impl TryFrom<QTime> for time::Time {
type Error = time::error::ComponentRange;
fn try_from(value: QTime) -> Result<Self, Self::Error> {
time::Time::from_hms_milli(
value.hour() as u8,
value.minute() as u8,
value.second() as u8,
value.msec() as u16,
)
}
}
// Safety:
//
// Static checks on the C++ side ensure that QTime is trivial.
unsafe impl ExternType for QTime {
type Id = type_id!("QTime");
type Kind = cxx::kind::Trivial;
}
#[cfg(test)]
#[cfg(feature = "chrono")]
mod test_chrono {
use super::*;
#[test]
fn qtime_from_chrono_naive() {
let naive = chrono::NaiveTime::from_hms_milli_opt(1, 2, 3, 4).unwrap();
let qtime = QTime::new(1, 2, 3, 4);
assert_eq!(QTime::try_from(naive).unwrap(), qtime);
}
#[test]
fn qtime_from_chrono_naive_leap_second() {
let naive = chrono::NaiveTime::from_hms_milli_opt(1, 2, 59, 1_999).unwrap();
assert!(QTime::try_from(naive).is_err());
}
#[test]
fn qtime_to_chrono_naive() {
let naive = chrono::NaiveTime::from_hms_milli_opt(1, 2, 3, 4).unwrap();
let qtime = QTime::new(1, 2, 3, 4);
assert_eq!(chrono::NaiveTime::try_from(qtime).unwrap(), naive);
}
}
#[cfg(test)]
#[cfg(feature = "time")]
mod test_time {
use super::*;
#[test]
fn qtime_from_time_time() {
let time_time = time::Time::from_hms_milli(1, 2, 3, 4).unwrap();
let qtime = QTime::new(1, 2, 3, 4);
assert_eq!(QTime::from(time_time), qtime);
}
#[test]
fn qtime_to_time_time() {
let time_time = time::Time::from_hms_milli(1, 2, 3, 4).unwrap();
let qtime = QTime::new(1, 2, 3, 4);
assert_eq!(time::Time::try_from(qtime).unwrap(), time_time);
}
}