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
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/qstring.h");
type QString = crate::QString;
include!("cxx-qt-lib/qdate.h");
type QDate = super::QDate;
#[rust_name = "add_months"]
fn addMonths(self: &QDate, nmonths: i32) -> QDate;
#[rust_name = "add_years"]
fn addYears(self: &QDate, nyears: i32) -> QDate;
fn day(self: &QDate) -> i32;
#[rust_name = "day_of_week"]
fn dayOfWeek(self: &QDate) -> i32;
#[rust_name = "day_of_year"]
fn dayOfYear(self: &QDate) -> i32;
#[rust_name = "days_in_monyth"]
fn daysInMonth(self: &QDate) -> i32;
#[rust_name = "days_in_year"]
fn daysInYear(self: &QDate) -> i32;
#[rust_name = "is_null"]
fn isNull(self: &QDate) -> bool;
#[rust_name = "is_valid"]
fn isValid(self: &QDate) -> bool;
fn month(self: &QDate) -> i32;
#[rust_name = "set_date"]
fn setDate(self: &mut QDate, y: i32, m: i32, d: i32) -> bool;
#[rust_name = "format_enum"]
fn toString(self: &QDate, format: DateFormat) -> QString;
fn year(self: &QDate) -> i32;
}
#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++" {
#[doc(hidden)]
#[rust_name = "qdate_add_days"]
fn qdateAddDays(date: &QDate, ndays: i64) -> QDate;
#[doc(hidden)]
#[rust_name = "qdate_current_date"]
fn qdateCurrentDate() -> QDate;
#[doc(hidden)]
#[rust_name = "qdate_days_to"]
fn qdateDaysTo(date: &QDate, d: QDate) -> i64;
#[doc(hidden)]
#[rust_name = "qdate_from_string"]
fn qdateFromString(string: &QString, format: &QString) -> QDate;
#[doc(hidden)]
#[rust_name = "qdate_from_string_enum"]
fn qdateFromString(string: &QString, format: DateFormat) -> QDate;
#[doc(hidden)]
#[rust_name = "qdate_is_leap_year"]
fn qdateIsLeapYear(year: i32) -> bool;
#[doc(hidden)]
#[rust_name = "qdate_to_format"]
fn qdateToFormat(date: &QDate, format: &QString) -> QString;
}
#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++" {
include!("cxx-qt-lib/common.h");
#[doc(hidden)]
#[rust_name = "qdate_init_default"]
fn construct() -> QDate;
#[doc(hidden)]
#[rust_name = "qdate_init"]
fn construct(y: i32, m: i32, d: i32) -> QDate;
#[doc(hidden)]
#[rust_name = "qdate_to_qstring"]
fn toQString(value: &QDate) -> QString;
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(C)]
pub struct QDate {
jd: i64,
}
impl Default for QDate {
fn default() -> Self {
ffi::qdate_init_default()
}
}
impl fmt::Display for QDate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ffi::qdate_to_qstring(self))
}
}
impl fmt::Debug for QDate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}")
}
}
impl QDate {
pub fn add_days(&self, ndays: i64) -> Self {
ffi::qdate_add_days(self, ndays)
}
pub fn current_date() -> Self {
ffi::qdate_current_date()
}
pub fn days_to(&self, date: Self) -> i64 {
ffi::qdate_days_to(self, date)
}
pub fn format(&self, format: &ffi::QString) -> ffi::QString {
ffi::qdate_to_format(self, format)
}
pub fn from_julian_day(jd: i64) -> Self {
Self { jd }
}
pub fn from_string(string: &ffi::QString, format: &ffi::QString) -> Option<Self> {
let date = ffi::qdate_from_string(string, format);
if date.is_valid() {
Some(date)
} else {
None
}
}
pub fn from_string_enum(string: &ffi::QString, format: ffi::DateFormat) -> Option<Self> {
let date = ffi::qdate_from_string_enum(string, format);
if date.is_valid() {
Some(date)
} else {
None
}
}
pub fn is_leap_year(year: i32) -> bool {
ffi::qdate_is_leap_year(year)
}
pub fn new(y: i32, m: i32, d: i32) -> Self {
ffi::qdate_init(y, m, d)
}
pub fn to_julian_day(&self) -> i64 {
self.jd
}
}
unsafe impl ExternType for QDate {
type Id = type_id!("QDate");
type Kind = cxx::kind::Trivial;
}
#[cfg(feature = "chrono")]
use chrono::Datelike;
#[cfg(feature = "chrono")]
impl From<chrono::NaiveDate> for QDate {
fn from(value: chrono::NaiveDate) -> Self {
QDate::new(value.year(), value.month() as i32, value.day() as i32)
}
}
#[cfg(feature = "chrono")]
impl TryFrom<QDate> for chrono::NaiveDate {
type Error = &'static str;
fn try_from(value: QDate) -> Result<Self, Self::Error> {
chrono::NaiveDate::from_ymd_opt(value.year(), value.month() as u32, value.day() as u32)
.ok_or("out-of-range date, invalid month and/or day")
}
}
#[cfg(feature = "time")]
impl From<time::Date> for QDate {
fn from(value: time::Date) -> Self {
QDate::new(
value.year(),
Into::<u8>::into(value.month()) as i32,
value.day() as i32,
)
}
}
#[cfg(feature = "time")]
impl TryFrom<QDate> for time::Date {
type Error = time::error::ComponentRange;
fn try_from(value: QDate) -> Result<Self, Self::Error> {
time::Date::from_calendar_date(
value.year(),
time::Month::try_from(value.month() as u8)?,
value.day() as u8,
)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn qdate_current_date() {
let date_a = QDate::current_date();
let date_b = date_a.add_days(100);
assert_eq!(date_a.days_to(date_b), 100);
}
#[test]
fn qdate_julian_day() {
let date_a = QDate::from_julian_day(1000);
let date_b = QDate::from_julian_day(1010);
assert_eq!(date_a.days_to(date_b), 10);
}
#[cfg(feature = "chrono")]
#[test]
fn qdate_from_chrono_naive() {
let naive = chrono::NaiveDate::from_ymd_opt(2023, 1, 1).unwrap();
let qdate = QDate::new(2023, 1, 1);
assert_eq!(QDate::from(naive), qdate);
}
#[cfg(feature = "chrono")]
#[test]
fn qdate_to_chrono_naive() {
let naive = chrono::NaiveDate::from_ymd_opt(2023, 1, 1).unwrap();
let qdate = QDate::new(2023, 1, 1);
assert_eq!(chrono::NaiveDate::try_from(qdate).unwrap(), naive);
}
#[cfg(feature = "time")]
#[test]
fn qdate_from_time_date() {
let time_date = time::Date::from_calendar_date(2023, time::Month::January, 1).unwrap();
let qdate = QDate::new(2023, 1, 1);
assert_eq!(QDate::from(time_date), qdate);
}
#[cfg(feature = "time")]
#[test]
fn qdate_to_time_date() {
let time_date = time::Date::from_calendar_date(2023, time::Month::January, 1).unwrap();
let qdate = QDate::new(2023, 1, 1);
assert_eq!(time::Date::try_from(qdate).unwrap(), time_date);
}
}