@@ -7,13 +7,13 @@ use tinystr::TinyAsciiStr;
7
7
8
8
use crate :: {
9
9
iso:: IsoDate ,
10
- options:: { ArithmeticOverflow , DisplayCalendar } ,
10
+ options:: { ArithmeticOverflow , DifferenceOperation , DifferenceSettings , DisplayCalendar } ,
11
11
parsers:: { FormattableCalendar , FormattableDate , FormattableYearMonth } ,
12
12
utils:: pad_iso_year,
13
13
Calendar , TemporalError , TemporalResult , TemporalUnwrap ,
14
14
} ;
15
15
16
- use super :: { Duration , PartialDate } ;
16
+ use super :: { Duration , PartialDate , PlainDate } ;
17
17
18
18
/// The native Rust implementation of `Temporal.YearMonth`.
19
19
#[ non_exhaustive]
@@ -37,6 +37,40 @@ impl PlainYearMonth {
37
37
Self { iso, calendar }
38
38
}
39
39
40
+ /// Internal addition method for adding `Duration` to a `PlainYearMonth`
41
+ pub ( crate ) fn add_or_subtract_duration (
42
+ & self ,
43
+ duration : & Duration ,
44
+ overflow : ArithmeticOverflow ,
45
+ ) -> TemporalResult < Self > {
46
+ // Potential TODO: update to current Temporal specification
47
+ let partial = PartialDate :: try_from_year_month ( self ) ?;
48
+
49
+ let mut intermediate_date = self . calendar ( ) . date_from_partial ( & partial, overflow) ?;
50
+
51
+ intermediate_date = intermediate_date. add_date ( duration, Some ( overflow) ) ?;
52
+
53
+ let result_fields = PartialDate :: default ( ) . with_fallback_date ( & intermediate_date) ?;
54
+
55
+ self . calendar ( )
56
+ . year_month_from_partial ( & result_fields, overflow)
57
+ }
58
+
59
+ /// The internal difference operation of `PlainYearMonth`.
60
+ pub ( crate ) fn diff (
61
+ & self ,
62
+ _op : DifferenceOperation ,
63
+ _other : & Self ,
64
+ _settings : DifferenceSettings ,
65
+ ) -> TemporalResult < Duration > {
66
+ // TODO: implement
67
+ Err ( TemporalError :: general ( "Not yet implemented" ) )
68
+ }
69
+ }
70
+
71
+ // ==== Public method implementations ====
72
+
73
+ impl PlainYearMonth {
40
74
/// Creates a new valid `YearMonth`.
41
75
#[ inline]
42
76
pub fn new_with_overflow (
@@ -137,6 +171,15 @@ impl PlainYearMonth {
137
171
self . calendar . identifier ( )
138
172
}
139
173
174
+ /// Creates a `PlainYearMonth` using the fields provided from a [`PartialDate`]
175
+ pub fn with (
176
+ & self ,
177
+ _partial : PartialDate ,
178
+ _overflow : ArithmeticOverflow ,
179
+ ) -> TemporalResult < Self > {
180
+ Err ( TemporalError :: general ( "Not yet implemented." ) )
181
+ }
182
+
140
183
/// Compares one `PlainYearMonth` to another `PlainYearMonth` using their
141
184
/// `IsoDate` representation.
142
185
///
@@ -151,39 +194,40 @@ impl PlainYearMonth {
151
194
self . iso . cmp ( & other. iso )
152
195
}
153
196
154
- pub fn add_duration (
155
- & self ,
156
- duration : & Duration ,
157
- overflow : ArithmeticOverflow ,
158
- ) -> TemporalResult < Self > {
197
+ /// Adds a [`Duration`] from the current `PlainYearMonth`.
198
+ #[ inline]
199
+ pub fn add ( & self , duration : & Duration , overflow : ArithmeticOverflow ) -> TemporalResult < Self > {
159
200
self . add_or_subtract_duration ( duration, overflow)
160
201
}
161
202
162
- pub fn subtract_duration (
203
+ /// Subtracts a [`Duration`] from the current `PlainYearMonth`.
204
+ #[ inline]
205
+ pub fn subtract (
163
206
& self ,
164
207
duration : & Duration ,
165
208
overflow : ArithmeticOverflow ,
166
209
) -> TemporalResult < Self > {
167
210
self . add_or_subtract_duration ( & duration. negated ( ) , overflow)
168
211
}
169
212
170
- pub ( crate ) fn add_or_subtract_duration (
171
- & self ,
172
- duration : & Duration ,
173
- overflow : ArithmeticOverflow ,
174
- ) -> TemporalResult < Self > {
175
- let partial = PartialDate :: try_from_year_month ( self ) ?;
176
-
177
- let mut intermediate_date = self . calendar ( ) . date_from_partial ( & partial, overflow) ?;
178
-
179
- intermediate_date = intermediate_date. add_date ( duration, Some ( overflow) ) ?;
213
+ /// Returns a `Duration` representing the period of time from this `PlainYearMonth` until the other `PlainYearMonth`.
214
+ #[ inline]
215
+ pub fn until ( & self , other : & Self , settings : DifferenceSettings ) -> TemporalResult < Duration > {
216
+ self . diff ( DifferenceOperation :: Until , other, settings)
217
+ }
180
218
181
- let result_fields = PartialDate :: default ( ) . with_fallback_date ( & intermediate_date) ?;
219
+ /// Returns a `Duration` representing the period of time from this `PlainYearMonth` since the other `PlainYearMonth`.
220
+ #[ inline]
221
+ pub fn since ( & self , other : & Self , settings : DifferenceSettings ) -> TemporalResult < Duration > {
222
+ self . diff ( DifferenceOperation :: Since , other, settings)
223
+ }
182
224
183
- self . calendar ( )
184
- . year_month_from_partial ( & result_fields , overflow )
225
+ pub fn to_plain_date ( & self ) -> TemporalResult < PlainDate > {
226
+ Err ( TemporalError :: general ( "Not yet iimplemented." ) )
185
227
}
186
228
229
+ /// Returns a RFC9557 IXDTF string for the current `PlainYearMonth`
230
+ #[ inline]
187
231
pub fn to_ixdtf_string ( & self , display_calendar : DisplayCalendar ) -> String {
188
232
let ixdtf = FormattableYearMonth {
189
233
date : FormattableDate ( self . iso_year ( ) , self . iso_month ( ) , self . iso . day ) ,
0 commit comments