@@ -159,6 +159,15 @@ impl PlainTime {
159
159
160
160
impl PlainTime {
161
161
/// Creates a new `PlainTime`, constraining any field into a valid range.
162
+ ///
163
+ /// ```rust
164
+ /// use temporal_rs::PlainTime;
165
+ ///
166
+ /// let time = PlainTime::new(23, 59, 59, 999, 999, 999).unwrap();
167
+ ///
168
+ /// let constrained_time = PlainTime::new(24, 59, 59, 999, 999, 999).unwrap();
169
+ /// assert_eq!(time, constrained_time);
170
+ /// ```
162
171
pub fn new (
163
172
hour : i32 ,
164
173
minute : i32 ,
@@ -179,6 +188,15 @@ impl PlainTime {
179
188
}
180
189
181
190
/// Creates a new `PlainTime`, rejecting any field that is not in a valid range.
191
+ ///
192
+ /// ```rust
193
+ /// use temporal_rs::PlainTime;
194
+ ///
195
+ /// let time = PlainTime::try_new(23, 59, 59, 999, 999, 999).unwrap();
196
+ ///
197
+ /// let invalid_time = PlainTime::try_new(24, 59, 59, 999, 999, 999);
198
+ /// assert!(invalid_time.is_err());
199
+ /// ```
182
200
pub fn try_new (
183
201
hour : i32 ,
184
202
minute : i32 ,
@@ -221,11 +239,68 @@ impl PlainTime {
221
239
Ok ( Self :: new_unchecked ( time) )
222
240
}
223
241
242
+ /// Creates a new `PlainTime` from a `PartialTime`.
243
+ ///
244
+ /// ```rust
245
+ /// use temporal_rs::{partial::PartialTime, PlainTime};
246
+ ///
247
+ /// let partial_time = PartialTime {
248
+ /// hour: Some(22),
249
+ /// ..Default::default()
250
+ /// };
251
+ ///
252
+ /// let time = PlainTime::from_partial(partial_time, None).unwrap();
253
+ ///
254
+ /// assert_eq!(time.hour(), 22);
255
+ /// assert_eq!(time.minute(), 0);
256
+ /// assert_eq!(time.second(), 0);
257
+ /// assert_eq!(time.millisecond(), 0);
258
+ /// assert_eq!(time.microsecond(), 0);
259
+ /// assert_eq!(time.nanosecond(), 0);
260
+ ///
261
+ /// ```
262
+ pub fn from_partial (
263
+ partial : PartialTime ,
264
+ overflow : Option < ArithmeticOverflow > ,
265
+ ) -> TemporalResult < Self > {
266
+ // NOTE: 4.5.12 ToTemporalTimeRecord requires one field to be set.
267
+ if partial. is_empty ( ) {
268
+ return Err ( TemporalError :: r#type ( ) . with_message ( "PartialTime cannot be empty." ) ) ;
269
+ }
270
+
271
+ let overflow = overflow. unwrap_or_default ( ) ;
272
+ let iso = IsoTime :: default ( ) . with ( partial, overflow) ?;
273
+ Ok ( Self :: new_unchecked ( iso) )
274
+ }
275
+
276
+ /// Creates a new `PlainTime` using the current `PlainTime` fields as a fallback.
277
+ ///
278
+ /// ```rust
279
+ /// use temporal_rs::{partial::PartialTime, PlainTime};
280
+ ///
281
+ /// let partial_time = PartialTime {
282
+ /// hour: Some(22),
283
+ /// ..Default::default()
284
+ /// };
285
+ ///
286
+ /// let initial = PlainTime::try_new(15, 30, 12, 123, 456, 789).unwrap();
287
+ ///
288
+ /// let time = initial.with(partial_time, None).unwrap();
289
+ ///
290
+ /// assert_eq!(time.hour(), 22);
291
+ /// assert_eq!(time.minute(), 30);
292
+ /// assert_eq!(time.second(), 12);
293
+ /// assert_eq!(time.millisecond(), 123);
294
+ /// assert_eq!(time.microsecond(), 456);
295
+ /// assert_eq!(time.nanosecond(), 789);
296
+ ///
297
+ /// ```
224
298
pub fn with (
225
299
& self ,
226
300
partial : PartialTime ,
227
301
overflow : Option < ArithmeticOverflow > ,
228
302
) -> TemporalResult < Self > {
303
+ // NOTE: 4.5.12 ToTemporalTimeRecord requires one field to be set.
229
304
if partial. is_empty ( ) {
230
305
return Err ( TemporalError :: r#type ( ) . with_message ( "PartialTime cannot be empty." ) ) ;
231
306
}
0 commit comments