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
use std::ffi::{c_void, CStr, CString};
use std::ptr;
use chrono::Datelike;
use chrono::NaiveDate;
use chrono::TimeDelta;
use std::fmt::Debug;
use std::hash::Hash;
use std::ops::{BitAnd, BitOr};
use crate::collections::base::SpanSet;
use crate::collections::base::{Collection, Span, impl_collection, impl_iterator};
use crate::errors::ParseError;
use crate::utils::from_interval;
use super::date_span::DateSpan;
use super::DAYS_UNTIL_2000;
pub struct DateSpanSet {
_inner: ptr::NonNull<meos_sys::SpanSet>,
}
impl Drop for DateSpanSet {
fn drop(&mut self) {
unsafe {
libc::free(self._inner.as_ptr().cast::<c_void>());
}
}
}
impl Collection for DateSpanSet {
impl_collection!(spanset, NaiveDate);
fn contains(&self, content: &NaiveDate) -> bool {
unsafe { meos_sys::contains_spanset_date(self.inner(), content.num_days_from_ce()) }
}
}
impl SpanSet for DateSpanSet {
type SpanType = DateSpan;
type SubsetType = TimeDelta;
fn inner(&self) -> *const meos_sys::SpanSet {
self._inner.as_ptr()
}
fn from_inner(inner: *mut meos_sys::SpanSet) -> Self {
Self {
_inner: ptr::NonNull::new(inner).expect("No null pointers allowed"),
}
}
fn width(&self, _ignore_gaps: bool) -> Self::Type {
unimplemented!("Not implemented for date")
}
/// Return a new `DateSpanSet` with the lower and upper bounds shifted by `delta`.
///
/// ## Arguments
/// * `delta` - The value to shift by.
///
/// ## Returns
/// A new `DateSpanSet` instance.
///
/// ## Example
/// ```
/// # use meos::DateSpanSet;
/// # use meos::meos_initialize;
/// # use std::str::FromStr;
/// # use meos::SpanSet;
/// use chrono::TimeDelta;
/// # meos_initialize();
/// let span_set = DateSpanSet::from_str("{[2019-09-08, 2019-09-10], [2019-09-16, 2019-09-20]}").unwrap();
/// let shifted_span_set = span_set.shift(TimeDelta::days(5));
///
/// let expected_shifted_span_set =
/// DateSpanSet::from_str("{[2019-09-13, 2019-09-16), [2019-09-21, 2019-09-26)}").unwrap();
/// assert_eq!(shifted_span_set, expected_shifted_span_set);
/// ```
fn shift(&self, delta: TimeDelta) -> DateSpanSet {
self.shift_scale(Some(delta), None)
}
/// Return a new `DateSpanSet` with the lower and upper bounds scaled so that the width is `width`.
///
/// ## Arguments
/// * `width` - The new width.
///
/// ## Returns
/// A new `DateSpanSet` instance.
///
/// ## Example
/// ```
/// # use meos::DateSpanSet;
/// # use meos::meos_initialize;
/// # use std::str::FromStr;
/// # use meos::SpanSet;
/// use chrono::TimeDelta;
/// # meos_initialize();
/// let span_set = DateSpanSet::from_str("{[2019-09-08, 2019-09-10], [2019-09-13, 2019-09-15]}").unwrap();
/// let scaled_span_set = span_set.scale(TimeDelta::days(5));
///
/// let expected_scaled_span_set =
/// DateSpanSet::from_str("{[2019-09-08, 2019-09-10), [2019-09-11, 2019-09-14)}").unwrap();
/// assert_eq!(scaled_span_set, expected_scaled_span_set);
/// ```
fn scale(&self, width: TimeDelta) -> DateSpanSet {
self.shift_scale(None, Some(width))
}
/// Return a new `DateSpanSet` with the lower and upper bounds shifted by `delta` and scaled so that the width is `width`.
///
/// ## Arguments
/// * `delta` - The value to shift by.
/// * `width` - The new width.
///
/// ## Returns
/// A new `DateSpanSet` instance.
///
/// ## Example
/// ```
/// # use meos::DateSpanSet;
/// # use meos::meos_initialize;
/// # use std::str::FromStr;
/// # use meos::SpanSet;
/// use chrono::TimeDelta;
/// # meos_initialize();
/// let span_set = DateSpanSet::from_str("{[2019-09-08, 2019-09-10], [2019-09-11, 2019-09-12]}").unwrap();
/// let shifted_scaled_span_set = span_set.shift_scale(Some(TimeDelta::days(5)), Some(TimeDelta::days(10)));
///
/// let expected_shifted_scaled_span_set =
/// DateSpanSet::from_str("{[2019-09-13, 2019-09-24)}").unwrap();
/// assert_eq!(shifted_scaled_span_set, expected_shifted_scaled_span_set);
/// ```
fn shift_scale(&self, delta: Option<TimeDelta>, width: Option<TimeDelta>) -> DateSpanSet {
let d = delta
.unwrap_or_default()
.num_days()
.try_into()
.expect("Number too big");
let w = width
.unwrap_or_default()
.num_days()
.try_into()
.expect("Number too big");
let modified = unsafe {
meos_sys::datespanset_shift_scale(self.inner(), d, w, delta.is_some(), width.is_some())
};
DateSpanSet::from_inner(modified)
}
/// Calculates the distance between this `DateSpanSet` and a specific timestamp (`value`).
///
/// ## Arguments
/// * `value` - A timestamp represented by `TimeDelta` from the Unix epoch.
///
/// ## Returns
/// A `TimeDelta` representing the distance in seconds between this `DateSpanSet` and the given timestamp.
///
/// ## Example
/// ```
/// # use meos::DateSpanSet;
/// # use meos::SpanSet;
/// # use chrono::{TimeDelta, TimeZone, NaiveDate};
/// # use meos::meos_initialize;
/// use std::str::FromStr;
/// # meos_initialize();
/// let span_set = DateSpanSet::from_str("{[2019-09-08, 2019-09-10], [2019-09-11, 2019-09-12]}").unwrap();
/// let timestamp = NaiveDate::from_ymd_opt(2019, 9, 5).unwrap();
/// let distance = span_set.distance_to_value(×tamp);
/// assert_eq!(distance, TimeDelta::days(3));
/// ```
fn distance_to_value(&self, other: &Self::Type) -> TimeDelta {
unsafe {
TimeDelta::days(
meos_sys::distance_spanset_date(
self.inner(),
other
.checked_sub_days(DAYS_UNTIL_2000)
.unwrap()
.num_days_from_ce(),
)
.into(),
)
}
}
/// Calculates the distance between this `DateSpanSet` and another `DateSpanSet`.
///
/// ## Arguments
/// * `other` - Another `DateSpanSet` to calculate the distance to.
///
/// ## Returns
/// A `TimeDelta` representing the distance in seconds between the two span sets.
///
/// ## Example
/// ```
/// # use meos::DateSpanSet;
/// # use meos::SpanSet;
/// # use chrono::{TimeDelta, TimeZone, Utc};
/// # use meos::meos_initialize;
/// use std::str::FromStr;
/// # meos_initialize();
/// let span_set1 = DateSpanSet::from_str("{[2019-09-08, 2019-09-10], [2019-09-11, 2019-09-12]}").unwrap();
/// let span_set2 = DateSpanSet::from_str("{[2018-08-07, 2018-08-17], [2018-10-17, 2018-10-20]}").unwrap();
/// let distance = span_set1.distance_to_span_set(&span_set2);
/// assert_eq!(distance, TimeDelta::days(323));
/// ```
fn distance_to_span_set(&self, other: &Self) -> TimeDelta {
unsafe {
TimeDelta::days(
meos_sys::distance_datespanset_datespanset(self.inner(), other.inner()).into(),
)
}
}
/// Calculates the distance between this `DateSpanSet` and a `DateSpan`.
///
/// ## Arguments
/// * `other` - A `DateSpan` to calculate the distance to.
///
/// ## Returns
/// A `TimeDelta` representing the distance in seconds between the span set and the span.
///
/// ## Example
/// ```
/// # use meos::DateSpanSet;
/// # use meos::SpanSet;
/// # use meos::DateSpan;
/// # use meos::Span;
/// # use chrono::{TimeDelta, TimeZone, Utc};
/// # use meos::meos_initialize;
/// use std::str::FromStr;
/// # meos_initialize();
/// let span_set = DateSpanSet::from_str("{[2019-09-08, 2019-09-10], [2019-09-11, 2019-09-12]}").unwrap();
/// let span = DateSpan::from_str("[2018-08-07, 2018-08-17]").unwrap();
/// let distance = span_set.distance_to_span(&span);
/// assert_eq!(distance, TimeDelta::days(387));
/// ```
fn distance_to_span(&self, span: &Self::SpanType) -> TimeDelta {
unsafe {
TimeDelta::days(
meos_sys::distance_datespanset_datespan(self.inner(), span.inner()).into(),
)
}
}
}
impl DateSpanSet {
pub fn duration(&self, ignore_gaps: bool) -> TimeDelta {
from_interval(unsafe {
meos_sys::datespanset_duration(self._inner.as_ptr(), ignore_gaps).read()
})
}
}
impl Clone for DateSpanSet {
fn clone(&self) -> DateSpanSet {
self.copy()
}
}
impl_iterator!(DateSpanSet);
impl Hash for DateSpanSet {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let hash = unsafe { meos_sys::spanset_hash(self.inner()) };
state.write_u32(hash);
let _ = state.finish();
}
}
impl std::str::FromStr for DateSpanSet {
type Err = ParseError;
fn from_str(string: &str) -> Result<Self, Self::Err> {
CString::new(string).map_err(|_| ParseError).map(|string| {
let inner = unsafe { meos_sys::datespanset_in(string.as_ptr()) };
Self::from_inner(inner)
})
}
}
impl std::cmp::PartialEq for DateSpanSet {
fn eq(&self, other: &Self) -> bool {
unsafe { meos_sys::spanset_eq(self.inner(), other.inner()) }
}
}
impl Debug for DateSpanSet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let out_str = unsafe { meos_sys::datespanset_out(self.inner()) };
let c_str = unsafe { CStr::from_ptr(out_str) };
let str = c_str.to_str().map_err(|_| std::fmt::Error)?;
let result = f.write_str(str);
unsafe { libc::free(out_str.cast::<c_void>()) };
result
}
}
impl BitAnd<DateSpanSet> for DateSpanSet {
type Output = Option<DateSpanSet>;
/// Computes the dateersection of two `DateSpanSet`s.
///
/// ## Arguments
///
/// * `other` - Another `DateSpanSet` to dateersect with.
///
/// ## Returns
///
/// * `Some(DateSpanSet)` - A new `DateSpanSet` containing the dateersection, if it exists.
/// * `None` - If the dateersection is empty.
///
/// ## Example
///
/// ```
/// # use meos::DateSpanSet;
/// # use meos::meos_initialize;
/// # use std::str::FromStr;
/// # use meos::SpanSet;
/// # meos_initialize();
/// let span_set1 = DateSpanSet::from_str("{[2019-09-08, 2019-09-10], [2019-09-15, 2019-09-20]}").unwrap();
/// let span_set2 = DateSpanSet::from_str("{[2019-09-15, 2019-09-30], [2019-11-11, 2019-11-12]}").unwrap();
///
/// let expected_result = DateSpanSet::from_str("{[2019-09-15, 2019-09-21)}").unwrap();
/// assert_eq!((span_set1 & span_set2).unwrap(), expected_result);
/// ```
fn bitand(self, other: DateSpanSet) -> Self::Output {
self.intersection(&other)
}
}
impl BitOr for DateSpanSet {
type Output = Option<DateSpanSet>;
/// Computes the union of two `DateSpanSet`s.
///
/// ## Arguments
///
/// * `other` - Another `DateSpanSet` to union with.
///
/// ## Returns
///
/// * `Some(DateSpanSet)` - A new `DateSpanSet` containing the union.
/// * `None` - If the union is empty.
///
/// ## Example
///
/// ```
/// # use meos::DateSpanSet;
/// # use meos::meos_initialize;
/// # use std::str::FromStr;
/// # use meos::SpanSet;
/// # meos_initialize();
/// let span_set1 = DateSpanSet::from_str("{[2019-09-08, 2019-09-10], [2019-09-15, 2019-09-20]}").unwrap();
/// let span_set2 = DateSpanSet::from_str("{[2019-09-15, 2019-09-30], [2019-11-11, 2019-11-12]}").unwrap();
///
/// let expected_result = DateSpanSet::from_str("{[2019-09-08, 2019-09-11), [2019-09-15, 2019-10-01), [2019-11-11, 2019-11-13)}").unwrap();
/// assert_eq!((span_set1 | span_set2).unwrap(), expected_result)
/// ```
fn bitor(self, other: Self) -> Self::Output {
self.union(&other)
}
}