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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
// 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::mem::MaybeUninit;
use std::{cmp::Ordering, fmt};
#[allow(unused)]
use crate::TimeSpec;
use crate::{DateFormat, QDate, QString, QTime, QTimeZone};
#[cxx::bridge]
mod ffi {
#[namespace = "Qt"]
unsafe extern "C++" {
include!("cxx-qt-lib/qt.h");
type TimeSpec = crate::TimeSpec;
type DateFormat = crate::DateFormat;
}
unsafe extern "C++" {
include!("cxx-qt-lib/qtypes.h");
type qint64 = crate::qint64;
}
unsafe extern "C++" {
include!("cxx-qt-lib/qdate.h");
type QDate = crate::QDate;
include!("cxx-qt-lib/qtime.h");
type QTime = crate::QTime;
include!("cxx-qt-lib/qstring.h");
type QString = crate::QString;
include!("cxx-qt-lib/qtimezone.h");
type QTimeZone = crate::QTimeZone;
}
unsafe extern "C++" {
include!("cxx-qt-lib/qdatetime.h");
type QDateTime = super::QDateTime;
/// Returns the system clock's current datetime, expressed in terms of UTC.
#[Self = "QDateTime"]
#[rust_name = "current_date_time_utc"]
fn currentDateTimeUtc() -> QDateTime;
/// Returns the system clock's current datetime, using local time.
#[Self = "QDateTime"]
#[rust_name = "current_date_time"]
fn currentDateTime() -> QDateTime;
#[doc(hidden)]
#[Self = "QDateTime"]
#[rust_name = "current_msecs_since_epoch_qint64"]
fn currentMSecsSinceEpoch() -> qint64;
#[doc(hidden)]
#[Self = "QDateTime"]
#[rust_name = "current_secs_since_epoch_qint64"]
fn currentSecsSinceEpoch() -> qint64;
#[doc(hidden)]
#[Self = "QDateTime"]
#[rust_name = "from_msecs_since_epoch_qint64"]
fn fromMSecsSinceEpoch(msecs: qint64, time_zone: &QTimeZone) -> QDateTime;
#[doc(hidden)]
#[Self = "QDateTime"]
#[rust_name = "from_secs_since_epoch_qint64"]
fn fromSecsSinceEpoch(secs: qint64, time_zone: &QTimeZone) -> QDateTime;
#[doc(hidden)]
#[rust_name = "add_days_qint64"]
fn addDays(self: &QDateTime, ndays: qint64) -> QDateTime;
/// Returns a QDateTime object containing a datetime `nmonths` months later than the datetime of this object (or earlier if `nmonths` is negative).
///
/// If [`time_spec`](Self::time_spec) is [`TimeSpec::LocalTime`] or [`TimeSpec::TimeZone`] and the resulting date and time fall in the Standard Time to Daylight-Saving Time transition hour then the result will be just beyond this gap, in the direction of change. If the transition is at 2am and the clock goes forward to 3am, the result of aiming between 2am and 3am will be adjusted to fall before 2am (if `nmonths` is negative) or after 3am (otherwise).
#[rust_name = "add_months"]
fn addMonths(self: &QDateTime, nmonths: i32) -> QDateTime;
#[doc(hidden)]
#[rust_name = "add_msecs_qint64"]
fn addMSecs(self: &QDateTime, msecs: qint64) -> QDateTime;
#[doc(hidden)]
#[rust_name = "add_secs_qint64"]
fn addSecs(self: &QDateTime, secs: qint64) -> QDateTime;
/// Returns a QDateTime object containing a datetime `nyears` years later than the datetime of this object (or earlier if `nyears` is negative).
///
/// If [`time_spec`](Self::time_spec) is [`TimeSpec::LocalTime`] or [`TimeSpec::TimeZone`] and the resulting date and time fall in the Standard Time to Daylight-Saving Time transition hour then the result will be just beyond this gap, in the direction of change. If the transition is at 2am and the clock goes forward to 3am, the result of aiming between 2am and 3am will be adjusted to fall before 2am (if `nyears` is negative) or after 3am (otherwise).
#[rust_name = "add_years"]
fn addYears(self: &QDateTime, nyears: i32) -> QDateTime;
/// Returns the date part of the datetime.
fn date(self: &QDateTime) -> QDate;
#[doc(hidden)]
#[rust_name = "days_to_qint64"]
fn daysTo(self: &QDateTime, other: &QDateTime) -> qint64;
/// Returns `true` if this datetime falls in Daylight-Saving Time, otherwise `false`.
///
/// If [`time_spec`](Self::time_spec) is not [`TimeSpec::LocalTime`] or [`TimeSpec::TimeZone`] then this will always return `false`.
#[rust_name = "is_daylight_time"]
fn isDaylightTime(self: &QDateTime) -> bool;
/// Returns `true` if both the date and the time are null; otherwise returns `false`. A null datetime is invalid.
#[rust_name = "is_null"]
fn isNull(self: &QDateTime) -> bool;
/// Returns `true` if both the `date` and the `time` are valid and they are valid in the current [`TimeSpec`](TimeSpec), otherwise returns `false`.
#[rust_name = "is_valid"]
fn isValid(self: &QDateTime) -> bool;
/// Returns this date-time's offset from UTC in seconds.
#[rust_name = "offset_from_utc"]
fn offsetFromUtc(self: &QDateTime) -> i32;
#[doc(hidden)]
#[rust_name = "msecs_to_qint64"]
fn msecsTo(self: &QDateTime, other: &QDateTime) -> qint64;
#[doc(hidden)]
#[rust_name = "secs_to_qint64"]
fn secsTo(self: &QDateTime, other: &QDateTime) -> qint64;
#[doc(hidden)]
#[rust_name = "set_msecs_since_epoch_qint64"]
fn setMSecsSinceEpoch(self: &mut QDateTime, msecs: qint64);
/// Sets the [`time_spec`](Self::time_spec) to [`TimeSpec::OffsetFromUTC`] and the offset to `offset_seconds`.
///
/// **Note:** This method is only available with Qt < 6.8.
#[cfg(not(cxxqt_qt_version_at_least_6_8))]
#[rust_name = "set_offset_from_utc"]
fn setOffsetFromUtc(self: &mut QDateTime, offset_seconds: i32);
#[doc(hidden)]
#[rust_name = "set_secs_since_epoch_qint64"]
fn setSecsSinceEpoch(self: &mut QDateTime, secs: qint64);
/// Sets the time specification used in this datetime to `spec`. The datetime will refer to a different point in time.
///
/// **Note:** This method is only available with Qt < 6.8.
#[cfg(not(cxxqt_qt_version_at_least_6_8))]
#[rust_name = "set_time_spec"]
fn setTimeSpec(self: &mut QDateTime, spec: TimeSpec);
/// Returns the time part of the datetime.
fn time(self: &QDateTime) -> QTime;
/// Returns the time specification of the datetime.
#[rust_name = "time_spec"]
fn timeSpec(self: &QDateTime) -> TimeSpec;
/// Returns the Time Zone Abbreviation for this datetime.
#[rust_name = "time_zone_abbreviation"]
fn timeZoneAbbreviation(self: &QDateTime) -> QString;
/// Returns a copy of this datetime converted to local time.
///
/// The result represents the same moment in time as, and is equal to, this datetime.
#[rust_name = "to_local_time"]
fn toLocalTime(self: &QDateTime) -> QDateTime;
#[doc(hidden)]
#[rust_name = "to_msecs_since_epoch_qint64"]
fn toMSecsSinceEpoch(self: &QDateTime) -> qint64;
/// Returns a copy of this datetime converted to a spec of [`TimeSpec::OffsetFromUTC`] with the given `offset_seconds`.
///
/// If the `offset_seconds` equals 0 then a UTC datetime will be returned.
///
/// The result represents the same moment in time as, and is equal to, this datetime.
#[rust_name = "to_offset_from_utc"]
fn toOffsetFromUtc(self: &QDateTime, offset_seconds: i32) -> QDateTime;
#[doc(hidden)]
#[rust_name = "to_secs_since_epoch_qint64"]
fn toSecsSinceEpoch(self: &QDateTime) -> qint64;
/// Returns the time as a string in the `format` given.
#[rust_name = "format_enum"]
fn toString(self: &QDateTime, format: DateFormat) -> QString;
/// Returns a copy of this datetime converted to the given time `spec`.
///
/// Note this method is only available with Qt < 6.8
#[cfg(not(cxxqt_qt_version_at_least_6_8))]
#[rust_name = "to_time_spec"]
fn toTimeSpec(self: &QDateTime, spec: TimeSpec) -> QDateTime;
/// Returns a copy of this datetime converted to the given `time_zone`.
///
/// The result represents the same moment in time as, and is equal to, this datetime.
///
/// If `time_zone` is invalid then the datetime will be invalid.
#[rust_name = "to_time_zone"]
fn toTimeZone(self: &QDateTime, time_zone: &QTimeZone) -> QDateTime;
/// Returns a copy of this datetime converted to UTC.
///
/// The result represents the same moment in time as, and is equal to, this datetime.
#[rust_name = "to_utc"]
fn toUTC(self: &QDateTime) -> QDateTime;
}
#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++" {
// Note that Qt 5 takes const-ref and Qt 6 takes by-value
// for QDateTime::setDate and QDateTime::setTime
//
// We want by-value, as that is Rust-idiomatic, so for Qt 5 we create a proxy
#[doc(hidden)]
#[rust_name = "qdatetime_set_date"]
fn qdatetimeSetDate(datetime: &mut QDateTime, date: QDate);
#[doc(hidden)]
#[rust_name = "qdatetime_set_time"]
fn qdatetimeSetTime(datetime: &mut QDateTime, time: QTime);
#[doc(hidden)]
#[rust_name = "qdatetime_time_zone"]
fn qdatetimeTimeZone(datetime: &QDateTime) -> UniquePtr<QTimeZone>;
#[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"]
unsafe extern "C++" {
include!("cxx-qt-lib/common.h");
#[doc(hidden)]
#[rust_name = "qdatetime_drop"]
fn drop(datetime: &mut QDateTime);
#[doc(hidden)]
#[rust_name = "qdatetime_init_default"]
fn construct() -> QDateTime;
#[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(
date: &QDate,
time: &QTime,
time_spec: TimeSpec,
offset_seconds: i32,
) -> QDateTime;
#[doc(hidden)]
#[rust_name = "qdatetime_init_from_qdatetime"]
fn construct(datetime: &QDateTime) -> QDateTime;
#[doc(hidden)]
#[rust_name = "qdatetime_eq"]
fn operatorEq(a: &QDateTime, b: &QDateTime) -> bool;
#[doc(hidden)]
#[rust_name = "qdatetime_cmp"]
fn operatorCmp(a: &QDateTime, b: &QDateTime) -> i8;
#[doc(hidden)]
#[rust_name = "qdatetime_to_debug_qstring"]
fn toDebugQString(value: &QDateTime) -> QString;
}
}
/// The `QDateTime` class provides date and time functions.
///
/// Qt Documentation: [QDateTime](https://doc.qt.io/qt/qdatetime.html#details)
#[repr(C)]
pub struct QDateTime {
_space: MaybeUninit<usize>,
}
impl QDateTime {
/// Returns the current number of milliseconds since the start, in UTC, of the year 1970.
///
/// This number is like the POSIX `time_t` variable, but expressed in milliseconds instead.
pub fn current_msecs_since_epoch() -> i64 {
Self::current_msecs_since_epoch_qint64().into()
}
/// Returns the number of seconds since the start, in UTC, of the year 1970.
///
/// This number is like the POSIX `time_t` variable.
pub fn current_secs_since_epoch() -> i64 {
Self::current_secs_since_epoch_qint64().into()
}
/// Returns a datetime representing a moment the given number `msecs` of milliseconds after the start, in UTC, of the year 1970, described as specified by `time_zone`.
///
/// Note that there are possible values for `msecs` that lie outside the valid range of `QDateTime`, both negative and positive. The behavior of this function is undefined for those values.
pub fn from_msecs_since_epoch(msecs: i64, time_zone: &QTimeZone) -> QDateTime {
Self::from_msecs_since_epoch_qint64(msecs.into(), time_zone)
}
/// Returns a datetime representing a moment the given number `secs` of seconds after the start, in UTC, of the year 1970, described as specified by `time_zone`.
///
/// Note that there are possible values for `secs` that lie outside the valid range of `QDateTime`, both negative and positive. The behavior of this function is undefined for those values.
pub fn from_secs_since_epoch(secs: i64, time_zone: &QTimeZone) -> QDateTime {
Self::from_secs_since_epoch_qint64(secs.into(), time_zone)
}
/// Sets the time zone used in this datetime to `time_zone`.
///
/// The datetime may refer to a different point in time. It uses the time representation of `time_zone`, which may change the meaning of its unchanged [`date`](Self::date) and [`time`](Self::time).
///
/// If `time_zone` is invalid then the datetime will be invalid.
pub fn set_time_zone(&mut self, time_zone: &QTimeZone) {
ffi::qdatetime_settimezone(self, time_zone)
}
/// Returns a `QDateTime` object containing a datetime `ndays` days later than the datetime of this object (or earlier if `ndays` is negative).
///
/// If [`time_spec`](Self::time_spec) is [`TimeSpec::LocalTime`] or [`TimeSpec::TimeZone`] and the resulting date and time fall in the Standard Time to Daylight-Saving Time transition hour then the result will be just beyond this gap, in the direction of change. If the transition is at 2am and the clock goes forward to 3am, the result of aiming between 2am and 3am will be adjusted to fall before 2am (if `ndays` is negative) or after 3am (otherwise).
pub fn add_days(self: &QDateTime, ndays: i64) -> QDateTime {
self.add_days_qint64(ndays.into())
}
/// Returns a `QDateTime` object containing a datetime `msecs` milliseconds later than the datetime of this object (or earlier if `msecs` is negative).
///
/// If this datetime is invalid, an invalid datetime will be returned.
pub fn add_msecs(self: &QDateTime, msecs: i64) -> QDateTime {
self.add_msecs_qint64(msecs.into())
}
/// Returns a `QDateTime` object containing a datetime `secs` seconds later than the datetime of this object (or earlier if `secs` is negative).
///
/// If this datetime is invalid, an invalid datetime will be returned.
pub fn add_secs(self: &QDateTime, secs: i64) -> QDateTime {
self.add_secs_qint64(secs.into())
}
/// Returns the number of days from this datetime to the `other` datetime.
/// The number of days is counted as the number of times midnight is reached between this datetime to the other datetime.
/// This means that a 10 minute difference from 23:55 to 0:05 the next day counts as one day.
///
/// If the `other` datetime is earlier than this datetime, the value returned is negative.
pub fn days_to(&self, other: &Self) -> i64 {
self.days_to_qint64(other).into()
}
/// Constructs a datetime with the given `date` and `time`, using the time representation described by `time_zone`.
///
/// If `date` is valid and `time` is not, the time will be set to midnight. If `time_zone` is invalid then the datetime will be invalid.
pub fn from_date_and_time_time_zone(date: &QDate, time: &QTime, time_zone: &QTimeZone) -> Self {
ffi::qdatetime_init_from_date_and_time_time_zone(date, time, time_zone)
}
/// Constructs a datetime with the given `date` and `time`, using the time specification defined by `spec` and `offset_seconds` seconds.
///
/// If `date` is valid and `time` is not, the time will be set to midnight.
///
/// If the spec is not [`TimeSpec::OffsetFromUTC`] then `offset_seconds` will be ignored.
///
/// If the spec is [`TimeSpec::OffsetFromUTC`] and `offset_seconds` is 0 then the time spec will be set to [`TimeSpec::UTC`], i.e. an offset of 0 seconds.
///
/// If spec is [`TimeSpec::TimeZone`] then the spec will be set to [`TimeSpec::LocalTime`], i.e. the current system time zone.
///
/// **Note:** This method is only available with Qt < 6.8.
#[cfg(not(cxxqt_qt_version_at_least_6_8))]
pub fn from_date_and_time_time_spec(
date: &QDate,
time: &QTime,
time_spec: TimeSpec,
offset_seconds: i32,
) -> Self {
ffi::qdatetime_init_from_date_and_time_time_spec(date, time, time_spec, offset_seconds)
}
/// Returns the datetime represented by the `string`, using the `format` given, or `None` if this is not possible.
///
/// Note for [`DateFormat::TextDate`]: only English month names (e.g. "Jan" in short form or "January" in long form) are recognized.
pub fn from_string(string: &QString, format: 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.
///
/// Returns 0 if either datetime is invalid.
pub fn msecs_to(self: &QDateTime, other: &QDateTime) -> i64 {
self.msecs_to_qint64(other).into()
}
/// Returns the number of seconds from this datetime to the `other` datetime.
/// If the `other` datetime is earlier than this datetime, the value returned is negative.
///
/// Returns 0 if either datetime is invalid.
pub fn secs_to(self: &QDateTime, other: &QDateTime) -> i64 {
self.secs_to_qint64(other).into()
}
/// Sets the date part of this datetime to `date`. If no time is set yet, it is set to midnight.
/// If `date` is invalid, this `QDateTime` becomes invalid.
pub fn set_date(&mut self, date: QDate) {
ffi::qdatetime_set_date(self, date);
}
/// Sets the datetime to represent a moment a given number, `msecs`, of milliseconds after the start, in UTC, of the year 1970.
///
/// On systems that do not support time zones, this function will behave as if local time were UTC.
///
/// Note that passing `i64::MIN` to `msecs` will result in undefined behavior.
pub fn set_msecs_since_epoch(self: &mut QDateTime, msecs: i64) {
self.set_msecs_since_epoch_qint64(msecs.into());
}
/// Sets the datetime to represent a moment a given number, `secs`, of seconds after the start, in UTC, of the year 1970.
///
/// On systems that do not support time zones, this function will behave as if local time were UTC.
pub fn set_secs_since_epoch(self: &mut QDateTime, secs: i64) {
self.set_secs_since_epoch_qint64(secs.into());
}
/// Sets the time part of this datetime to `time`. If `time` is not valid, this function sets it to midnight.
/// Therefore, it's possible to clear any set time in a `QDateTime` by setting it to [`QTime::default()`].
pub fn set_time(&mut self, time: QTime) {
ffi::qdatetime_set_time(self, time);
}
/// Returns the time zone of the datetime.
pub fn time_zone(&self) -> cxx::UniquePtr<QTimeZone> {
ffi::qdatetime_time_zone(self)
}
/// Returns the datetime as a number of milliseconds after the start, in UTC, of the year 1970.
///
/// On systems that do not support time zones, this function will behave as if local time were UTC.
///
/// The behavior for this function is undefined if the datetime stored in this object is not valid. However, for all valid dates, this function returns a unique value.
pub fn to_msecs_since_epoch(self: &QDateTime) -> i64 {
self.to_msecs_since_epoch_qint64().into()
}
/// Returns the datetime as a number of seconds after the start, in UTC, of the year 1970.
///
/// On systems that do not support time zones, this function will behave as if local time were UTC.
///
/// The behavior for this function is undefined if the datetime stored in this object is not valid. However, for all valid dates, this function returns a unique value.
pub fn to_secs_since_epoch(self: &QDateTime) -> i64 {
self.to_secs_since_epoch_qint64().into()
}
}
impl Clone for QDateTime {
/// Constructs a copy of the other datetime.
fn clone(&self) -> Self {
ffi::qdatetime_init_from_qdatetime(self)
}
}
impl Default for QDateTime {
/// Construct a default null QDateTime
fn default() -> Self {
ffi::qdatetime_init_default()
}
}
impl PartialEq for QDateTime {
fn eq(&self, other: &Self) -> bool {
ffi::qdatetime_eq(self, other)
}
}
impl Eq for QDateTime {}
impl PartialOrd for QDateTime {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for QDateTime {
fn cmp(&self, other: &Self) -> Ordering {
ffi::qdatetime_cmp(self, other).cmp(&0)
}
}
impl fmt::Display for QDateTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.format_enum(DateFormat::TextDate).fmt(f)
}
}
impl fmt::Debug for QDateTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ffi::qdatetime_to_debug_qstring(self).fmt(f)
}
}
impl Drop for QDateTime {
/// Destroys the datetime.
fn drop(&mut self) {
ffi::qdatetime_drop(self);
}
}
#[cfg(feature = "chrono")]
use chrono::Offset;
#[cfg(feature = "chrono")]
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> {
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())?,
tz.as_ref().ok_or("Could not construct timezone")?,
))
}
}
#[cfg(feature = "chrono")]
impl TryFrom<QDateTime> for chrono::DateTime<chrono::FixedOffset> {
type Error = &'static str;
fn try_from(value: QDateTime) -> Result<Self, Self::Error> {
let timezone_east = chrono::FixedOffset::east_opt(value.offset_from_utc())
.expect("out-of-bound offset secs");
let value_utc = value.to_utc();
let naivedatetime_east = chrono::NaiveDate::try_from(value_utc.date())?
.and_time(chrono::NaiveTime::try_from(value_utc.time())?);
Ok(
chrono::DateTime::<chrono::FixedOffset>::from_naive_utc_and_offset(
naivedatetime_east,
timezone_east,
),
)
}
}
#[cfg(feature = "chrono")]
impl TryFrom<QDateTime> for chrono::DateTime<chrono::Utc> {
type Error = &'static str;
fn try_from(value: QDateTime) -> Result<Self, Self::Error> {
let value_utc = value.to_utc();
let naivedatetime_utc = chrono::NaiveDate::try_from(value_utc.date())?
.and_time(chrono::NaiveTime::try_from(value_utc.time())?);
Ok(chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(
naivedatetime_utc,
chrono::Utc,
))
}
}
#[cfg(feature = "time")]
impl From<time::OffsetDateTime> for QDateTime {
fn from(value: time::OffsetDateTime) -> Self {
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()),
tz.as_ref().expect("Could not construct timezone"),
)
}
}
#[cfg(feature = "time")]
impl From<time::PrimitiveDateTime> for QDateTime {
fn from(value: time::PrimitiveDateTime) -> Self {
let tz = crate::QTimeZone::utc();
QDateTime::from_date_and_time_time_zone(
&QDate::from(value.date()),
&QTime::from(value.time()),
tz.as_ref().expect("Could not construct timezone"),
)
}
}
#[cfg(feature = "time")]
impl TryFrom<QDateTime> for time::OffsetDateTime {
type Error = time::error::ComponentRange;
fn try_from(value: QDateTime) -> Result<Self, Self::Error> {
Ok(time::Date::try_from(value.date())?
.with_time(time::Time::try_from(value.time())?)
.assume_offset(time::UtcOffset::from_whole_seconds(
value.offset_from_utc(),
)?))
}
}
#[cfg(feature = "time")]
impl TryFrom<QDateTime> for time::PrimitiveDateTime {
type Error = time::error::ComponentRange;
fn try_from(value: QDateTime) -> Result<Self, Self::Error> {
let value_utc = value.to_utc();
Ok(time::Date::try_from(value_utc.date())?
.with_time(time::Time::try_from(value_utc.time())?))
}
}
// Safety:
//
// Static checks on the C++ side to ensure the size is the same.
unsafe impl ExternType for QDateTime {
type Id = type_id!("QDateTime");
type Kind = cxx::kind::Trivial;
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_ordering() {
let qdatetime_a = QDateTime::from_date_and_time_time_zone(
&QDate::new(2023, 1, 1),
&QTime::new(1, 1, 1, 1),
&QTimeZone::utc(),
);
let qdatetime_b = QDateTime::from_date_and_time_time_zone(
&QDate::new(2023, 2, 2),
&QTime::new(2, 2, 2, 2),
&QTimeZone::utc(),
);
assert!(qdatetime_a < qdatetime_b);
assert_eq!(qdatetime_a.cmp(&qdatetime_b), Ordering::Less);
assert_eq!(qdatetime_b.cmp(&qdatetime_a), Ordering::Greater);
assert_eq!(qdatetime_a.cmp(&qdatetime_a), Ordering::Equal);
}
}
#[cfg(test)]
#[cfg(feature = "chrono")]
mod test_chrono {
use super::*;
#[test]
fn qdatetime_from_chrono() {
let datetime_east = {
let timezone_east = chrono::FixedOffset::east_opt(60 * 60).unwrap();
let naivedatetime_east = chrono::NaiveDate::from_ymd_opt(2023, 1, 1)
.unwrap()
.and_hms_milli_opt(1, 2, 3, 4)
.unwrap();
chrono::DateTime::<chrono::FixedOffset>::from_naive_utc_and_offset(
naivedatetime_east,
timezone_east,
)
};
let qdatetime = QDateTime::from_date_and_time_time_zone(
&QDate::new(2023, 1, 1),
// Chrono adds the offset to the given time, so add the offset here to match Chrono
&QTime::new(1 + 1 /* plus the offset */, 2, 3, 4),
&QTimeZone::from_offset_seconds(60 * 60),
);
assert_eq!(QDateTime::try_from(datetime_east).unwrap(), qdatetime);
}
#[test]
fn qdatetime_to_chrono_fixed_offset() {
let datetime_east = {
let timezone_east = chrono::FixedOffset::east_opt(60 * 60).unwrap();
let naivedatetime_east = chrono::NaiveDate::from_ymd_opt(2023, 1, 1)
.unwrap()
// Chrono adds the offset to the given time, so minus the offset here to match Qt
.and_hms_milli_opt(1 - 1 /* minus the offset */, 2, 3, 4)
.unwrap();
chrono::DateTime::<chrono::FixedOffset>::from_naive_utc_and_offset(
naivedatetime_east,
timezone_east,
)
};
let qdatetime = QDateTime::from_date_and_time_time_zone(
&QDate::new(2023, 1, 1),
&QTime::new(1, 2, 3, 4),
&QTimeZone::from_offset_seconds(60 * 60),
);
assert_eq!(
chrono::DateTime::<chrono::FixedOffset>::try_from(qdatetime).unwrap(),
datetime_east
);
}
#[test]
fn qdatetime_to_chrono_utc() {
let datetime_utc = {
let naivedatetime_utc = chrono::NaiveDate::from_ymd_opt(2023, 1, 1)
.unwrap()
.and_hms_milli_opt(1, 2, 3, 4)
.unwrap();
chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(
naivedatetime_utc,
chrono::Utc,
)
};
let qdatetime = QDateTime::from_date_and_time_time_zone(
&QDate::new(2023, 1, 1),
&QTime::new(1, 2, 3, 4),
&QTimeZone::utc(),
);
assert_eq!(
chrono::DateTime::<chrono::Utc>::try_from(qdatetime).unwrap(),
datetime_utc
);
}
#[test]
fn qdatetime_to_chrono_utc_with_offset() {
let datetime_utc = {
let naivedatetime_utc = chrono::NaiveDate::from_ymd_opt(2023, 1, 1)
.unwrap()
.and_hms_milli_opt(0, 2, 3, 4)
.unwrap();
chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(
naivedatetime_utc,
chrono::Utc,
)
};
let qdatetime = QDateTime::from_date_and_time_time_zone(
&QDate::new(2023, 1, 1),
&QTime::new(1, 2, 3, 4),
// Should cause one hour offset when in chrono::DateTime
&QTimeZone::from_offset_seconds(60 * 60),
);
assert_eq!(
chrono::DateTime::<chrono::Utc>::try_from(qdatetime).unwrap(),
datetime_utc
);
}
}
#[cfg(test)]
#[cfg(feature = "time")]
mod test_time {
use super::*;
#[test]
fn qdatetime_to_time_offsetdatetime() {
let time_offsetdatetime = time::Date::from_calendar_date(2023, time::Month::January, 1)
.unwrap()
.with_hms_milli(1, 2, 3, 4)
.unwrap()
.assume_offset(time::UtcOffset::from_whole_seconds(60 * 60).unwrap());
let qdatetime = QDateTime::from_date_and_time_time_zone(
&QDate::new(2023, 1, 1),
&QTime::new(1, 2, 3, 4),
&QTimeZone::from_offset_seconds(60 * 60),
);
assert_eq!(
time::OffsetDateTime::try_from(qdatetime).unwrap(),
time_offsetdatetime
);
}
#[test]
fn qdatetime_to_time_primitivedatetime() {
let time_offsetdatetime = time::Date::from_calendar_date(2023, time::Month::January, 1)
.unwrap()
.with_hms_milli(1, 2, 3, 4)
.unwrap();
let qdatetime = QDateTime::from_date_and_time_time_zone(
&QDate::new(2023, 1, 1),
&QTime::new(1, 2, 3, 4),
&QTimeZone::utc(),
);
assert_eq!(
time::PrimitiveDateTime::try_from(qdatetime).unwrap(),
time_offsetdatetime
);
}
#[test]
fn qdatetime_from_time_offsetdatetime() {
let time_offsetdatetime = time::Date::from_calendar_date(2023, time::Month::January, 1)
.unwrap()
.with_hms_milli(1, 2, 3, 4)
.unwrap()
.assume_offset(time::UtcOffset::from_whole_seconds(60 * 60).unwrap());
let qdatetime = QDateTime::from_date_and_time_time_zone(
&QDate::new(2023, 1, 1),
&QTime::new(1, 2, 3, 4),
&QTimeZone::from_offset_seconds(60 * 60),
);
assert_eq!(QDateTime::from(time_offsetdatetime), qdatetime);
}
#[test]
fn qdatetime_from_time_primitivedatetime() {
let time_offsetdatetime = time::Date::from_calendar_date(2023, time::Month::January, 1)
.unwrap()
.with_hms_milli(1, 2, 3, 4)
.unwrap();
let qdatetime = QDateTime::from_date_and_time_time_zone(
&QDate::new(2023, 1, 1),
&QTime::new(1, 2, 3, 4),
&QTimeZone::utc(),
);
assert_eq!(QDateTime::from(time_offsetdatetime), qdatetime);
}
}