-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdate.rs
More file actions
108 lines (100 loc) · 2.96 KB
/
date.rs
File metadata and controls
108 lines (100 loc) · 2.96 KB
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
//! Implementation of a `DateDuration`
use crate::{primitive::FiniteF64, Sign, TemporalError, TemporalResult};
use alloc::vec::Vec;
/// `DateDuration` represents the [date duration record][spec] of the `Duration.`
///
/// These fields are laid out in the [Temporal Proposal][field spec] as 64-bit floating point numbers.
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal-date-duration-records
/// [field spec]: https://tc39.es/proposal-temporal/#sec-properties-of-temporal-duration-instances
#[non_exhaustive]
#[derive(Debug, Default, Clone, Copy)]
pub struct DateDuration {
/// `DateDuration`'s internal year value.
pub years: FiniteF64,
/// `DateDuration`'s internal month value.
pub months: FiniteF64,
/// `DateDuration`'s internal week value.
pub weeks: FiniteF64,
/// `DateDuration`'s internal day value.
pub days: FiniteF64,
}
impl DateDuration {
/// Creates a new, non-validated `DateDuration`.
#[inline]
#[must_use]
pub(crate) const fn new_unchecked(
years: FiniteF64,
months: FiniteF64,
weeks: FiniteF64,
days: FiniteF64,
) -> Self {
Self {
years,
months,
weeks,
days,
}
}
/// Returns the iterator for `DateDuration`
#[inline]
#[must_use]
pub(crate) fn fields(&self) -> Vec<FiniteF64> {
Vec::from(&[self.years, self.months, self.weeks, self.days])
}
}
impl DateDuration {
/// Creates a new `DateDuration` with provided values.
#[inline]
pub fn new(
years: FiniteF64,
months: FiniteF64,
weeks: FiniteF64,
days: FiniteF64,
) -> TemporalResult<Self> {
let result = Self::new_unchecked(years, months, weeks, days);
if !super::is_valid_duration(
years,
months,
weeks,
days,
FiniteF64::default(),
FiniteF64::default(),
FiniteF64::default(),
FiniteF64::default(),
FiniteF64::default(),
FiniteF64::default(),
) {
return Err(TemporalError::range().with_message("Invalid DateDuration."));
}
Ok(result)
}
/// Returns a negated `DateDuration`.
#[inline]
#[must_use]
pub fn negated(&self) -> Self {
Self {
years: self.years.negate(),
months: self.months.negate(),
weeks: self.weeks.negate(),
days: self.days.negate(),
}
}
/// Returns a new `DateDuration` representing the absolute value of the current.
#[inline]
#[must_use]
pub fn abs(&self) -> Self {
Self {
years: self.years.abs(),
months: self.months.abs(),
weeks: self.weeks.abs(),
days: self.days.abs(),
}
}
/// Returns the sign for the current `DateDuration`.
#[inline]
#[must_use]
pub fn sign(&self) -> Sign {
super::duration_sign(&self.fields())
}
}