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
use std::{
cmp,
ffi::{c_void, CStr, CString},
fmt::Debug,
hash::Hash,
ops::{BitAnd, Range, RangeInclusive},
ptr,
};
use chrono::{Datelike, NaiveDate, TimeDelta};
use crate::{
collections::{base::{Collection, Span, impl_collection}, datetime::DAYS_UNTIL_2000},
errors::ParseError,
utils::from_interval,
};
pub struct DateSpan {
_inner: ptr::NonNull<meos_sys::Span>,
}
impl Drop for DateSpan {
fn drop(&mut self) {
unsafe {
libc::free(self._inner.as_ptr().cast::<c_void>());
}
}
}
impl Collection for DateSpan {
impl_collection!(span, NaiveDate);
fn contains(&self, content: &NaiveDate) -> bool {
unsafe { meos_sys::contains_span_date(self.inner(), content.num_days_from_ce()) }
}
}
impl Span for DateSpan {
type SubsetType = TimeDelta;
fn inner(&self) -> *const meos_sys::Span {
self._inner.as_ptr()
}
/// Creates a new `DateSpan` from an inner podateer to a `meos_sys::Span`.
///
/// # Arguments
/// * `inner` - A podateer to the inner `meos_sys::Span`.
///
/// ## Returns
/// * A new `DateSpan` instance.
fn from_inner(inner: *mut meos_sys::Span) -> Self {
Self {
_inner: ptr::NonNull::new(inner).expect("No null pointers allowed"),
}
}
/// Returns the lower bound of the span.
///
/// ## Returns
/// * The lower bound as a `NaiveDate`.
///
/// ## Example
/// ```
/// # use meos::DateSpan;
/// # use meos::Span;
/// # use chrono::naive::NaiveDate;
///
/// let from_ymd_opt = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
///
/// let span: DateSpan = (from_ymd_opt(2023, 1, 1)..from_ymd_opt(2023, 1, 15)).into();
/// let lower = span.lower();
/// assert_eq!(lower, from_ymd_opt(2023, 1, 1));
/// ```
fn lower(&self) -> Self::Type {
let num_of_days = unsafe { meos_sys::datespan_lower(self.inner()) };
NaiveDate::from_num_days_from_ce_opt(num_of_days)
.expect("Wrong date returned from meos")
.checked_add_days(DAYS_UNTIL_2000)
.unwrap()
}
/// Returns the upper bound of the span.
///
/// ## Returns
/// * The upper bound as a `NaiveDate`.
///
/// ## Example
/// ```
/// # use meos::DateSpan;
/// # use meos::Span;
/// # use chrono::naive::NaiveDate;
///
/// let from_ymd_opt = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
///
/// let span: DateSpan = (from_ymd_opt(2023, 1, 1)..from_ymd_opt(2023, 1, 15)).into();
/// let upper = span.upper();
/// assert_eq!(upper, from_ymd_opt(2023, 1, 15));
/// ```
fn upper(&self) -> Self::Type {
let num_of_days = unsafe { meos_sys::datespan_upper(self.inner()) };
NaiveDate::from_num_days_from_ce_opt(num_of_days)
.expect("Wrong date returned from meos")
.checked_add_days(DAYS_UNTIL_2000)
.unwrap()
}
/// Return a new `DateSpan` with the lower and upper bounds shifted by `delta`.
///
/// # Arguments
/// * `delta` - The value to shift by, as a `NaiveDate`.
///
/// # Returns
/// A new `DateSpan` instance.
///
/// # Example
/// ```
/// # use meos::DateSpan;
/// # use meos::Span;
/// use chrono::naive::NaiveDate;
/// use chrono::TimeDelta;
///
/// let from_ymd_opt = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
///
/// let span: DateSpan = (from_ymd_opt(2023, 1, 1)..from_ymd_opt(2023, 1, 15)).into();
/// let shifted_span = span.shift(TimeDelta::days(5));
/// let expected_span: DateSpan = (from_ymd_opt(2023, 1, 6)..from_ymd_opt(2023, 1, 20)).into();
/// assert_eq!(shifted_span, expected_span);
/// ```
fn shift(&self, delta: TimeDelta) -> DateSpan {
self.shift_scale(Some(delta), None)
}
/// Return a new `DateSpan` with the lower and upper bounds scaled so that the width is `width`.
///
/// # Arguments
/// * `width` - The new width, as a `NaiveDate`.
///
/// # Returns
/// A new `DateSpan` instance.
///
/// # Example
/// ```
/// # use meos::DateSpan;
/// # use meos::Span;
/// use chrono::naive::NaiveDate;
/// use chrono::TimeDelta;
///
/// let from_ymd_opt = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
///
/// let span: DateSpan = (from_ymd_opt(2023, 1, 1)..from_ymd_opt(2023, 1, 15)).into();
/// let scaled_span = span.scale(TimeDelta::days(5));
/// let expected_span: DateSpan = (from_ymd_opt(2023, 1, 1)..from_ymd_opt(2023, 1, 07)).into();
/// assert_eq!(scaled_span, expected_span);
/// ```
fn scale(&self, width: TimeDelta) -> DateSpan {
self.shift_scale(None, Some(width))
}
/// Return a new `DateSpan` with the lower and upper bounds shifted by `delta` and scaled so that the width is `width`.
///
/// # Arguments
/// * `delta` - The value to shift by, as a `NaiveDate`.
/// * `width` - The new width, as a `NaiveDate`.
///
/// # Returns
/// A new `DateSpan` instance.
///
/// # Example
/// ```
/// # use meos::DateSpan;
/// # use meos::Span;
/// use chrono::naive::NaiveDate;
/// use chrono::TimeDelta;
///
/// let from_ymd_opt = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
///
/// let span: DateSpan = (from_ymd_opt(2023, 1, 1)..from_ymd_opt(2023, 1, 15)).into();
/// let shifted_scaled_span = span.shift_scale(Some(TimeDelta::days(5)), Some(TimeDelta::days(10)));
/// let expected_span: DateSpan = (from_ymd_opt(2023, 1, 6)..from_ymd_opt(2023, 1, 17)).into();
/// assert_eq!(shifted_scaled_span, expected_span);
/// ```
fn shift_scale(&self, delta: Option<TimeDelta>, width: Option<TimeDelta>) -> DateSpan {
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::datespan_shift_scale(
self._inner.as_ptr(),
d,
w,
delta.is_some(),
width.is_some(),
)
};
DateSpan::from_inner(modified)
}
/// Calculates the distance between this `DateSpan` and a specific timestamp (`value`).
///
/// ## Arguments
/// * `value` - Anvalue `DateSpan` to calculate the distance to.
///
/// ## Returns
/// A `TimeDelta` representing the distance in seconds between the two spans.
///
/// ## Example
/// ```
/// # use meos::DateSpan;
/// # use meos::meos_initialize;
/// use std::str::FromStr;
/// # use meos::Span;
/// use chrono::TimeDelta;
/// # meos_initialize();
/// let span1 = DateSpan::from_str("[2019-09-08, 2019-09-10]").unwrap();
/// let span2 = DateSpan::from_str("[2019-09-12, 2019-09-14]").unwrap();
/// let distance = span1.distance_to_span(&span2);
/// assert_eq!(distance, TimeDelta::days(2));
/// ```
fn distance_to_value(&self, other: &Self::Type) -> TimeDelta {
unsafe {
TimeDelta::days(
meos_sys::distance_span_date(
self.inner(),
other
.checked_sub_days(DAYS_UNTIL_2000)
.unwrap()
.num_days_from_ce(),
)
.into(),
)
}
}
/// Calculates the distance between this `DateSpan` and another `DateSpan`.
///
/// ## Arguments
/// * `other` - Another `DateSpan` to calculate the distance to.
///
/// ## Returns
/// A `TimeDelta` representing the distance in seconds between the two spans.
///
/// ## Example
/// ```
/// # use meos::DateSpan;
/// # use meos::Span;
/// # use chrono::{TimeDelta, TimeZone, Utc};
/// # use meos::meos_initialize;
/// use std::str::FromStr;
/// # meos_initialize();
/// let span_set1 = DateSpan::from_str("[2019-09-08, 2019-09-10]").unwrap();
/// let span_set2 = DateSpan::from_str("[2018-08-07, 2018-08-17]").unwrap();
/// let distance = span_set1.distance_to_span(&span_set2);
/// assert_eq!(distance, TimeDelta::days(387));
/// ```
fn distance_to_span(&self, other: &Self) -> TimeDelta {
unsafe {
TimeDelta::days(
meos_sys::distance_datespan_datespan(self.inner(), other.inner()).into(),
)
}
}
}
impl DateSpan {
pub fn duration(&self) -> TimeDelta {
from_interval(unsafe { meos_sys::datespan_duration(self._inner.as_ptr()).read() })
}
}
impl Clone for DateSpan {
fn clone(&self) -> Self {
unsafe { Self::from_inner(meos_sys::span_copy(self._inner.as_ptr())) }
}
}
impl Hash for DateSpan {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let hash = unsafe { meos_sys::span_hash(self._inner.as_ptr()) };
state.write_u32(hash);
let _ = state.finish();
}
}
impl std::str::FromStr for DateSpan {
type Err = ParseError;
/// Parses a `DateSpan` from a string representation.
///
/// ## Arguments
/// * `string` - A string slice containing the representation.
///
/// ## Returns
/// * A `DateSpan` instance.
///
/// ## Errors
/// * Returns `ParseSpanError` if the string cannot be parsed.
///
/// ## Example
/// ```
/// # use meos::DateSpan;
/// # use meos::Span;
/// # use std::str::FromStr;
/// # use meos::meos_initialize;
/// use chrono::NaiveDate;
/// # meos_initialize();
/// let from_ymd_opt = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
///
/// let span: DateSpan = "(2019-09-08, 2019-09-10)".parse().expect("Failed to parse span");
/// assert_eq!(span.lower(), from_ymd_opt(2019, 9, 9));
/// assert_eq!(span.upper(), from_ymd_opt(2019, 9, 10));
/// ```
fn from_str(string: &str) -> Result<Self, Self::Err> {
CString::new(string).map_err(|_| ParseError).map(|string| {
let inner = unsafe { meos_sys::datespan_in(string.as_ptr()) };
Self::from_inner(inner)
})
}
}
impl cmp::PartialEq for DateSpan {
/// Checks if two `DateSpan` instances are equal.
///
/// # Arguments
/// * `other` - Another `DateSpan` instance.
///
/// ## Returns
/// * `true` if the spans are equal, `false` otherwise.
///
/// ## Example
/// ```
/// # use meos::DateSpan;
/// # use meos::Span;
/// use chrono::naive::NaiveDate;
///
/// let from_ymd_opt = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
///
/// let span1: DateSpan = (from_ymd_opt(1, 1, 1)..from_ymd_opt(2, 2, 2)).into();
/// let span2: DateSpan = (from_ymd_opt(1, 1, 1)..from_ymd_opt(2, 2, 2)).into();
/// assert_eq!(span1, span2);
/// ```
fn eq(&self, other: &Self) -> bool {
unsafe { meos_sys::span_eq(self._inner.as_ptr(), other._inner.as_ptr()) }
}
}
impl cmp::Eq for DateSpan {}
impl From<Range<NaiveDate>> for DateSpan {
fn from(Range { start, end }: Range<NaiveDate>) -> Self {
let inner = unsafe {
meos_sys::datespan_make(
start
.checked_sub_days(DAYS_UNTIL_2000)
.unwrap()
.num_days_from_ce(),
end.checked_sub_days(DAYS_UNTIL_2000)
.unwrap()
.num_days_from_ce(),
true,
false,
)
};
Self::from_inner(inner)
}
}
impl From<RangeInclusive<NaiveDate>> for DateSpan {
fn from(range: RangeInclusive<NaiveDate>) -> Self {
let inner = unsafe {
meos_sys::datespan_make(
range
.start()
.checked_sub_days(DAYS_UNTIL_2000)
.unwrap()
.num_days_from_ce(),
range
.end()
.checked_sub_days(DAYS_UNTIL_2000)
.unwrap()
.num_days_from_ce(),
true,
true,
)
};
Self::from_inner(inner)
}
}
impl Debug for DateSpan {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let out_str = unsafe { meos_sys::datespan_out(self._inner.as_ptr()) };
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
}
}
// Implement BitAnd for dateersection with DateSpan
impl BitAnd for DateSpan {
type Output = Option<DateSpan>;
/// Computes the dateersection of two `DateSpan` instances.
///
/// # Arguments
/// * `other` - Another `DateSpan` instance.
///
/// ## Returns
/// * An `Option<DateSpan>` containing the dateersection, or `None` if there is no dateersection.
///
/// ## Example
/// ```
/// # use meos::DateSpan;
/// # use meos::Span;
/// # use std::str::FromStr;
/// use chrono::naive::NaiveDate;
///
/// let from_ymd_opt = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
///
/// let span1: DateSpan = (from_ymd_opt(1, 1, 1)..from_ymd_opt(1, 1, 11)).into();
/// let span2: DateSpan = (from_ymd_opt(1, 1, 9)..from_ymd_opt(2, 1, 11)).into();
/// let date_intersection = (span1 & span2).unwrap();
///
/// assert_eq!(date_intersection, (from_ymd_opt(1, 1, 9)..from_ymd_opt(1, 1, 11)).into())
/// ```
fn bitand(self, other: Self) -> Self::Output {
// Replace with actual function call or logic
let result = unsafe {
meos_sys::intersection_span_span(self._inner.as_ptr(), other._inner.as_ptr())
};
if result.is_null() {
None
} else {
Some(DateSpan::from_inner(result))
}
}
}
impl PartialOrd for DateSpan {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
let cmp = unsafe { meos_sys::span_cmp(self._inner.as_ptr(), other._inner.as_ptr()) };
match cmp {
-1 => Some(cmp::Ordering::Less),
0 => Some(cmp::Ordering::Equal),
1 => Some(cmp::Ordering::Greater),
_ => None,
}
}
}
impl Ord for DateSpan {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.partial_cmp(other).expect(
"Unreachable since for non-null and same types spans, we only return -1, 0, or 1",
)
}
}