-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
TimeOnly.cs
960 lines (834 loc) · 59.8 KB
/
TimeOnly.cs
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Globalization;
namespace System
{
/// <summary>
/// Represents a time of day, as would be read from a clock, within the range 00:00:00 to 23:59:59.9999999.
/// </summary>
public readonly struct TimeOnly
: IComparable,
IComparable<TimeOnly>,
IEquatable<TimeOnly>,
ISpanFormattable,
IComparisonOperators<TimeOnly, TimeOnly>,
IMinMaxValue<TimeOnly>,
ISpanParsable<TimeOnly>,
ISubtractionOperators<TimeOnly, TimeOnly, TimeSpan>
{
// represent the number of ticks map to the time of the day. 1 ticks = 100-nanosecond in time measurements.
private readonly long _ticks;
// MinTimeTicks is the ticks for the midnight time 00:00:00.000 AM
private const long MinTimeTicks = 0;
// MaxTimeTicks is the max tick value for the time in the day. It is calculated using DateTime.Today.AddTicks(-1).TimeOfDay.Ticks.
private const long MaxTimeTicks = 863_999_999_999;
/// <summary>
/// Represents the smallest possible value of TimeOnly.
/// </summary>
public static TimeOnly MinValue => new TimeOnly((ulong)MinTimeTicks);
/// <summary>
/// Represents the largest possible value of TimeOnly.
/// </summary>
public static TimeOnly MaxValue => new TimeOnly((ulong)MaxTimeTicks);
/// <summary>
/// Initializes a new instance of the timeOnly structure to the specified hour and the minute.
/// </summary>
/// <param name="hour">The hours (0 through 23).</param>
/// <param name="minute">The minutes (0 through 59).</param>
public TimeOnly(int hour, int minute) : this(DateTime.TimeToTicks(hour, minute, 0, 0)) {}
/// <summary>
/// Initializes a new instance of the timeOnly structure to the specified hour, minute, and second.
/// </summary>
/// <param name="hour">The hours (0 through 23).</param>
/// <param name="minute">The minutes (0 through 59).</param>
/// <param name="second">The seconds (0 through 59).</param>
public TimeOnly(int hour, int minute, int second) : this(DateTime.TimeToTicks(hour, minute, second, 0)) {}
/// <summary>
/// Initializes a new instance of the timeOnly structure to the specified hour, minute, second, and millisecond.
/// </summary>
/// <param name="hour">The hours (0 through 23).</param>
/// <param name="minute">The minutes (0 through 59).</param>
/// <param name="second">The seconds (0 through 59).</param>
/// <param name="millisecond">The millisecond (0 through 999).</param>
public TimeOnly(int hour, int minute, int second, int millisecond) : this(DateTime.TimeToTicks(hour, minute, second, millisecond)) { }
/// <summary>
/// Initializes a new instance of the <see cref="TimeOnly"/> structure to the specified hour, minute, second, and millisecond.
/// </summary>
/// <param name="hour">The hours (0 through 23).</param>
/// <param name="minute">The minutes (0 through 59).</param>
/// <param name="second">The seconds (0 through 59).</param>
/// <param name="millisecond">The millisecond (0 through 999).</param>
/// <param name="microsecond">The microsecond (0 through 999).</param>
public TimeOnly(int hour, int minute, int second, int millisecond, int microsecond) : this(DateTime.TimeToTicks(hour, minute, second, millisecond, microsecond)) { }
/// <summary>
/// Initializes a new instance of the TimeOnly structure using a specified number of ticks.
/// </summary>
/// <param name="ticks">A time of day expressed in the number of 100-nanosecond units since 00:00:00.0000000.</param>
public TimeOnly(long ticks)
{
if ((ulong)ticks > MaxTimeTicks)
{
throw new ArgumentOutOfRangeException(nameof(ticks), SR.ArgumentOutOfRange_TimeOnlyBadTicks);
}
_ticks = ticks;
}
// exist to bypass the check in the public constructor.
internal TimeOnly(ulong ticks) => _ticks = (long)ticks;
/// <summary>
/// Gets the hour component of the time represented by this instance.
/// </summary>
public int Hour => new TimeSpan(_ticks).Hours;
/// <summary>
/// Gets the minute component of the time represented by this instance.
/// </summary>
public int Minute => new TimeSpan(_ticks).Minutes;
/// <summary>
/// Gets the second component of the time represented by this instance.
/// </summary>
public int Second => new TimeSpan(_ticks).Seconds;
/// <summary>
/// Gets the millisecond component of the time represented by this instance.
/// </summary>
public int Millisecond => new TimeSpan(_ticks).Milliseconds;
/// <summary>
/// Gets the microsecond component of the time represented by this instance.
/// </summary>
public int Microsecond => new TimeSpan(_ticks).Microseconds;
/// <summary>
/// Gets the nanosecond component of the time represented by this instance.
/// </summary>
public int Nanosecond => new TimeSpan(_ticks).Nanoseconds;
/// <summary>
/// Gets the number of ticks that represent the time of this instance.
/// </summary>
public long Ticks => _ticks;
private TimeOnly AddTicks(long ticks) => new TimeOnly((_ticks + TimeSpan.TicksPerDay + (ticks % TimeSpan.TicksPerDay)) % TimeSpan.TicksPerDay);
private TimeOnly AddTicks(long ticks, out int wrappedDays)
{
wrappedDays = (int)(ticks / TimeSpan.TicksPerDay);
long newTicks = _ticks + ticks % TimeSpan.TicksPerDay;
if (newTicks < 0)
{
wrappedDays--;
newTicks += TimeSpan.TicksPerDay;
}
else
{
if (newTicks >= TimeSpan.TicksPerDay)
{
wrappedDays++;
newTicks -= TimeSpan.TicksPerDay;
}
}
return new TimeOnly(newTicks);
}
/// <summary>
/// Returns a new TimeOnly that adds the value of the specified TimeSpan to the value of this instance.
/// </summary>
/// <param name="value">A positive or negative time interval.</param>
/// <returns>An object whose value is the sum of the time represented by this instance and the time interval represented by value.</returns>
public TimeOnly Add(TimeSpan value) => AddTicks(value.Ticks);
/// <summary>
/// Returns a new TimeOnly that adds the value of the specified TimeSpan to the value of this instance.
/// If the result wraps past the end of the day, this method will return the number of excess days as an out parameter.
/// </summary>
/// <param name="value">A positive or negative time interval.</param>
/// <param name="wrappedDays">When this method returns, contains the number of excess days if any that resulted from wrapping during this addition operation.</param>
/// <returns>An object whose value is the sum of the time represented by this instance and the time interval represented by value.</returns>
public TimeOnly Add(TimeSpan value, out int wrappedDays) => AddTicks(value.Ticks, out wrappedDays);
/// <summary>
/// Returns a new TimeOnly that adds the specified number of hours to the value of this instance.
/// </summary>
/// <param name="value">A number of whole and fractional hours. The value parameter can be negative or positive.</param>
/// <returns>An object whose value is the sum of the time represented by this instance and the number of hours represented by value.</returns>
public TimeOnly AddHours(double value) => AddTicks((long)(value * TimeSpan.TicksPerHour));
/// <summary>
/// Returns a new TimeOnly that adds the specified number of hours to the value of this instance.
/// If the result wraps past the end of the day, this method will return the number of excess days as an out parameter.
/// </summary>
/// <param name="value">A number of whole and fractional hours. The value parameter can be negative or positive.</param>
/// <param name="wrappedDays">When this method returns, contains the number of excess days if any that resulted from wrapping during this addition operation.</param>
/// <returns>An object whose value is the sum of the time represented by this instance and the number of hours represented by value.</returns>
public TimeOnly AddHours(double value, out int wrappedDays) => AddTicks((long)(value * TimeSpan.TicksPerHour), out wrappedDays);
/// <summary>
/// Returns a new TimeOnly that adds the specified number of minutes to the value of this instance.
/// </summary>
/// <param name="value">A number of whole and fractional minutes. The value parameter can be negative or positive.</param>
/// <returns>An object whose value is the sum of the time represented by this instance and the number of minutes represented by value.</returns>
public TimeOnly AddMinutes(double value) => AddTicks((long)(value * TimeSpan.TicksPerMinute));
/// <summary>
/// Returns a new TimeOnly that adds the specified number of minutes to the value of this instance.
/// If the result wraps past the end of the day, this method will return the number of excess days as an out parameter.
/// </summary>
/// <param name="value">A number of whole and fractional minutes. The value parameter can be negative or positive.</param>
/// <param name="wrappedDays">When this method returns, contains the number of excess days if any that resulted from wrapping during this addition operation.</param>
/// <returns>An object whose value is the sum of the time represented by this instance and the number of minutes represented by value.</returns>
public TimeOnly AddMinutes(double value, out int wrappedDays) => AddTicks((long)(value * TimeSpan.TicksPerMinute), out wrappedDays);
/// <summary>
/// Determines if a time falls within the range provided.
/// Supports both "normal" ranges such as 10:00-12:00, and ranges that span midnight such as 23:00-01:00.
/// </summary>
/// <param name="start">The starting time of day, inclusive.</param>
/// <param name="end">The ending time of day, exclusive.</param>
/// <returns>True, if the time falls within the range, false otherwise.</returns>
/// <remarks>
/// If <paramref name="start"/> and <paramref name="end"/> are equal, this method returns false, meaning there is zero elapsed time between the two values.
/// If you wish to treat such cases as representing one or more whole days, then first check for equality before calling this method.
/// </remarks>
public bool IsBetween(TimeOnly start, TimeOnly end)
{
long startTicks = start._ticks;
long endTicks = end._ticks;
return startTicks <= endTicks
? (startTicks <= _ticks && endTicks > _ticks)
: (startTicks <= _ticks || endTicks > _ticks);
}
/// <summary>
/// Determines whether two specified instances of TimeOnly are equal.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns>true if left and right represent the same time; otherwise, false.</returns>
/// <inheritdoc cref="IEqualityOperators{TSelf, TOther}.op_Equality(TSelf, TOther)" />
public static bool operator ==(TimeOnly left, TimeOnly right) => left._ticks == right._ticks;
/// <summary>
/// Determines whether two specified instances of TimeOnly are not equal.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns>true if left and right do not represent the same time; otherwise, false.</returns>
/// <inheritdoc cref="IEqualityOperators{TSelf, TOther}.op_Inequality(TSelf, TOther)" />
public static bool operator !=(TimeOnly left, TimeOnly right) => left._ticks != right._ticks;
/// <summary>
/// Determines whether one specified TimeOnly is later than another specified TimeOnly.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns>true if left is later than right; otherwise, false.</returns>
/// <inheritdoc cref="IComparisonOperators{TSelf, TOther}.op_GreaterThan(TSelf, TOther)" />
public static bool operator >(TimeOnly left, TimeOnly right) => left._ticks > right._ticks;
/// <summary>
/// Determines whether one specified TimeOnly represents a time that is the same as or later than another specified TimeOnly.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns>true if left is the same as or later than right; otherwise, false.</returns>
/// <inheritdoc cref="IComparisonOperators{TSelf, TOther}.op_GreaterThanOrEqual(TSelf, TOther)" />
public static bool operator >=(TimeOnly left, TimeOnly right) => left._ticks >= right._ticks;
/// <summary>
/// Determines whether one specified TimeOnly is earlier than another specified TimeOnly.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns>true if left is earlier than right; otherwise, false.</returns>
/// <inheritdoc cref="IComparisonOperators{TSelf, TOther}.op_LessThan(TSelf, TOther)" />
public static bool operator <(TimeOnly left, TimeOnly right) => left._ticks < right._ticks;
/// <summary>
/// Determines whether one specified TimeOnly represents a time that is the same as or earlier than another specified TimeOnly.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns>true if left is the same as or earlier than right; otherwise, false.</returns>
/// <inheritdoc cref="IComparisonOperators{TSelf, TOther}.op_LessThanOrEqual(TSelf, TOther)" />
public static bool operator <=(TimeOnly left, TimeOnly right) => left._ticks <= right._ticks;
/// <summary>
/// Gives the elapsed time between two points on a circular clock, which will always be a positive value.
/// </summary>
/// <param name="t1">The first TimeOnly instance.</param>
/// <param name="t2">The second TimeOnly instance..</param>
/// <returns>The elapsed time between t1 and t2.</returns>
public static TimeSpan operator -(TimeOnly t1, TimeOnly t2) => new TimeSpan((t1._ticks - t2._ticks + TimeSpan.TicksPerDay) % TimeSpan.TicksPerDay);
/// <summary>
/// Constructs a TimeOnly object from a TimeSpan representing the time elapsed since midnight.
/// </summary>
/// <param name="timeSpan">The time interval measured since midnight. This value has to be positive and not exceeding the time of the day.</param>
/// <returns>A TimeOnly object representing the time elapsed since midnight using the timeSpan value.</returns>
public static TimeOnly FromTimeSpan(TimeSpan timeSpan) => new TimeOnly(timeSpan._ticks);
/// <summary>
/// Constructs a TimeOnly object from a DateTime representing the time of the day in this DateTime object.
/// </summary>
/// <param name="dateTime">The time DateTime object to extract the time of the day from.</param>
/// <returns>A TimeOnly object representing time of the day specified in the DateTime object.</returns>
public static TimeOnly FromDateTime(DateTime dateTime) => new TimeOnly(dateTime.TimeOfDay.Ticks);
/// <summary>
/// Convert the current TimeOnly instance to a TimeSpan object.
/// </summary>
/// <returns>A TimeSpan object spanning to the time specified in the current TimeOnly object.</returns>
public TimeSpan ToTimeSpan() => new TimeSpan(_ticks);
internal DateTime ToDateTime() => new DateTime(_ticks);
/// <summary>
/// Compares the value of this instance to a specified TimeOnly value and indicates whether this instance is earlier than, the same as, or later than the specified TimeOnly value.
/// </summary>
/// <param name="value">The object to compare to the current instance.</param>
/// <returns>
/// A signed number indicating the relative values of this instance and the value parameter.
/// Less than zero if this instance is earlier than value.
/// Zero if this instance is the same as value.
/// Greater than zero if this instance is later than value.
/// </returns>
public int CompareTo(TimeOnly value) => _ticks.CompareTo(value._ticks);
/// <summary>
/// Compares the value of this instance to a specified object that contains a specified TimeOnly value, and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified TimeOnly value.
/// </summary>
/// <param name="value">A boxed object to compare, or null.</param>
/// <returns>
/// A signed number indicating the relative values of this instance and the value parameter.
/// Less than zero if this instance is earlier than value.
/// Zero if this instance is the same as value.
/// Greater than zero if this instance is later than value.
/// </returns>
public int CompareTo(object? value)
{
if (value == null) return 1;
if (value is not TimeOnly timeOnly)
{
throw new ArgumentException(SR.Arg_MustBeTimeOnly);
}
return CompareTo(timeOnly);
}
/// <summary>
/// Returns a value indicating whether the value of this instance is equal to the value of the specified TimeOnly instance.
/// </summary>
/// <param name="value">The object to compare to this instance.</param>
/// <returns>true if the value parameter equals the value of this instance; otherwise, false.</returns>
public bool Equals(TimeOnly value) => _ticks == value._ticks;
/// <summary>
/// Returns a value indicating whether this instance is equal to a specified object.
/// </summary>
/// <param name="value">The object to compare to this instance.</param>
/// <returns>true if value is an instance of TimeOnly and equals the value of this instance; otherwise, false.</returns>
public override bool Equals([NotNullWhen(true)] object? value) => value is TimeOnly timeOnly && _ticks == timeOnly._ticks;
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>A 32-bit signed integer hash code.</returns>
public override int GetHashCode()
{
long ticks = _ticks;
return unchecked((int)ticks) ^ (int)(ticks >> 32);
}
private const ParseFlags ParseFlagsTimeMask = ParseFlags.HaveYear | ParseFlags.HaveMonth | ParseFlags.HaveDay | ParseFlags.HaveDate | ParseFlags.TimeZoneUsed |
ParseFlags.TimeZoneUtc | ParseFlags.ParsedMonthName | ParseFlags.CaptureOffset | ParseFlags.UtcSortPattern;
/// <summary>
/// Converts a memory span that contains string representation of a time to its TimeOnly equivalent by using culture-specific format information and a formatting style.
/// </summary>
/// <param name="s">The memory span that contains the string to parse.</param>
/// <param name="provider">An object that supplies culture-specific format information about s.</param>
/// <param name="style">A bitwise combination of enumeration values that indicates the permitted format of s. A typical value to specify is None.</param>
/// <returns>An object that is equivalent to the time contained in s, as specified by provider and styles.</returns>
/// <inheritdoc cref="ISpanParsable{TSelf}.Parse(ReadOnlySpan{char}, IFormatProvider?)" />
public static TimeOnly Parse(ReadOnlySpan<char> s, IFormatProvider? provider = default, DateTimeStyles style = DateTimeStyles.None)
{
ParseFailureKind result = TryParseInternal(s, provider, style, out TimeOnly timeOnly);
if (result != ParseFailureKind.None)
{
ThrowOnError(result, s);
}
return timeOnly;
}
private const string OFormat = "HH':'mm':'ss'.'fffffff";
private const string RFormat = "HH':'mm':'ss";
/// <summary>
/// Converts the specified span representation of a time to its TimeOnly equivalent using the specified format, culture-specific format information, and style.
/// The format of the string representation must match the specified format exactly or an exception is thrown.
/// </summary>
/// <param name="s">A span containing the characters that represent a time to convert.</param>
/// <param name="format">A span containing the characters that represent a format specifier that defines the required format of s.</param>
/// <param name="provider">An object that supplies culture-specific formatting information about s.</param>
/// <param name="style">A bitwise combination of enumeration values that indicates the permitted format of s. A typical value to specify is None.</param>
/// <returns>An object that is equivalent to the time contained in s, as specified by format, provider, and style.</returns>
public static TimeOnly ParseExact(ReadOnlySpan<char> s, [StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] ReadOnlySpan<char> format, IFormatProvider? provider = default, DateTimeStyles style = DateTimeStyles.None)
{
ParseFailureKind result = TryParseExactInternal(s, format, provider, style, out TimeOnly timeOnly);
if (result != ParseFailureKind.None)
{
ThrowOnError(result, s);
}
return timeOnly;
}
/// <summary>
/// Converts the specified span representation of a time to its TimeOnly equivalent using the specified array of formats.
/// The format of the string representation must match at least one of the specified formats exactly or an exception is thrown.
/// </summary>
/// <param name="s">A span containing the characters that represent a time to convert.</param>
/// <param name="formats">An array of allowable formats of s.</param>
/// <returns>An object that is equivalent to the time contained in s, as specified by format, provider, and style.</returns>
public static TimeOnly ParseExact(ReadOnlySpan<char> s, [StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] string[] formats) => ParseExact(s, formats, null, DateTimeStyles.None);
/// <summary>
/// Converts the specified span representation of a time to its TimeOnly equivalent using the specified array of formats, culture-specific format information, and style.
/// The format of the string representation must match at least one of the specified formats exactly or an exception is thrown.
/// </summary>
/// <param name="s">A span containing the characters that represent a time to convert.</param>
/// <param name="formats">An array of allowable formats of s.</param>
/// <param name="provider">An object that supplies culture-specific formatting information about s.</param>
/// <param name="style">A bitwise combination of enumeration values that indicates the permitted format of s. A typical value to specify is None.</param>
/// <returns>An object that is equivalent to the time contained in s, as specified by format, provider, and style.</returns>
public static TimeOnly ParseExact(ReadOnlySpan<char> s, [StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] string[] formats, IFormatProvider? provider, DateTimeStyles style = DateTimeStyles.None)
{
ParseFailureKind result = TryParseExactInternal(s, formats, provider, style, out TimeOnly timeOnly);
if (result != ParseFailureKind.None)
{
ThrowOnError(result, s);
}
return timeOnly;
}
/// <summary>
/// Converts a string that contains string representation of a time to its TimeOnly equivalent by using the conventions of the current culture.
/// </summary>
/// <param name="s">The string that contains the string to parse.</param>
/// <returns>An object that is equivalent to the time contained in s.</returns>
public static TimeOnly Parse(string s) => Parse(s, null, DateTimeStyles.None);
/// <summary>
/// Converts a string that contains string representation of a time to its TimeOnly equivalent by using culture-specific format information and a formatting style.
/// </summary>
/// <param name="s">The string that contains the string to parse.</param>
/// <param name="provider">An object that supplies culture-specific format information about s.</param>
/// <param name="style">A bitwise combination of the enumeration values that indicates the style elements that can be present in s for the parse operation to succeed, and that defines how to interpret the parsed date. A typical value to specify is None.</param>
/// <returns>An object that is equivalent to the time contained in s, as specified by provider and styles.</returns>
public static TimeOnly Parse(string s, IFormatProvider? provider, DateTimeStyles style = DateTimeStyles.None)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
return Parse(s.AsSpan(), provider, style);
}
/// <summary>
/// Converts the specified string representation of a time to its TimeOnly equivalent using the specified format.
/// The format of the string representation must match the specified format exactly or an exception is thrown.
/// </summary>
/// <param name="s">A string containing the characters that represent a time to convert.</param>
/// <param name="format">A string that represent a format specifier that defines the required format of s.</param>
/// <returns>An object that is equivalent to the time contained in s, as specified by format.</returns>
public static TimeOnly ParseExact(string s, [StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] string format) => ParseExact(s, format, null, DateTimeStyles.None);
/// <summary>
/// Converts the specified string representation of a time to its TimeOnly equivalent using the specified format, culture-specific format information, and style.
/// The format of the string representation must match the specified format exactly or an exception is thrown.
/// </summary>
/// <param name="s">A string containing the characters that represent a time to convert.</param>
/// <param name="format">A string containing the characters that represent a format specifier that defines the required format of s.</param>
/// <param name="provider">An object that supplies culture-specific formatting information about s.</param>
/// <param name="style">A bitwise combination of the enumeration values that provides additional information about s, about style elements that may be present in s, or about the conversion from s to a TimeOnly value. A typical value to specify is None.</param>
/// <returns>An object that is equivalent to the time contained in s, as specified by format, provider, and style.</returns>
public static TimeOnly ParseExact(string s, [StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] string format, IFormatProvider? provider, DateTimeStyles style = DateTimeStyles.None)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
if (format == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.format);
return ParseExact(s.AsSpan(), format.AsSpan(), provider, style);
}
/// <summary>
/// Converts the specified span representation of a time to its TimeOnly equivalent using the specified array of formats.
/// The format of the string representation must match at least one of the specified formats exactly or an exception is thrown.
/// </summary>
/// <param name="s">A span containing the characters that represent a time to convert.</param>
/// <param name="formats">An array of allowable formats of s.</param>
/// <returns>An object that is equivalent to the time contained in s, as specified by format, provider, and style.</returns>
public static TimeOnly ParseExact(string s, [StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] string[] formats) => ParseExact(s, formats, null, DateTimeStyles.None);
/// <summary>
/// Converts the specified string representation of a time to its TimeOnly equivalent using the specified array of formats, culture-specific format information, and style.
/// The format of the string representation must match at least one of the specified formats exactly or an exception is thrown.
/// </summary>
/// <param name="s">A string containing the characters that represent a time to convert.</param>
/// <param name="formats">An array of allowable formats of s.</param>
/// <param name="provider">An object that supplies culture-specific formatting information about s.</param>
/// <param name="style">A bitwise combination of enumeration values that indicates the permitted format of s. A typical value to specify is None.</param>
/// <returns>An object that is equivalent to the time contained in s, as specified by format, provider, and style.</returns>
public static TimeOnly ParseExact(string s, [StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] string[] formats, IFormatProvider? provider, DateTimeStyles style = DateTimeStyles.None)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
return ParseExact(s.AsSpan(), formats, provider, style);
}
/// <summary>
/// Converts the specified span representation of a time to its TimeOnly equivalent and returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">A span containing the characters representing the time to convert.</param>
/// <param name="result">When this method returns, contains the TimeOnly value equivalent to the time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is empty string, or does not contain a valid string representation of a time. This parameter is passed uninitialized.</param>
/// <returns>true if the s parameter was converted successfully; otherwise, false.</returns>
public static bool TryParse(ReadOnlySpan<char> s, out TimeOnly result) => TryParse(s, null, DateTimeStyles.None, out result);
/// <summary>
/// Converts the specified span representation of a time to its TimeOnly equivalent using the specified array of formats, culture-specific format information, and style. And returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">A string containing the characters that represent a time to convert.</param>
/// <param name="provider">An object that supplies culture-specific formatting information about s.</param>
/// <param name="style">A bitwise combination of enumeration values that indicates the permitted format of s. A typical value to specify is None.</param>
/// <param name="result">When this method returns, contains the TimeOnly value equivalent to the time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is empty string, or does not contain a valid string representation of a date. This parameter is passed uninitialized.</param>
/// <returns>true if the s parameter was converted successfully; otherwise, false.</returns>
/// <inheritdoc cref="ISpanParsable{TSelf}.TryParse(ReadOnlySpan{char}, IFormatProvider?, out TSelf)" />
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result) =>
TryParseInternal(s, provider, style, out result) == ParseFailureKind.None;
private static ParseFailureKind TryParseInternal(ReadOnlySpan<char> s, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result)
{
if ((style & ~DateTimeStyles.AllowWhiteSpaces) != 0)
{
result = default;
return ParseFailureKind.FormatWithParameter;
}
DateTimeResult dtResult = default;
dtResult.Init(s);
if (!DateTimeParse.TryParse(s, DateTimeFormatInfo.GetInstance(provider), style, ref dtResult))
{
result = default;
return ParseFailureKind.FormatWithOriginalDateTime;
}
if ((dtResult.flags & ParseFlagsTimeMask) != 0)
{
result = default;
return ParseFailureKind.WrongParts;
}
result = new TimeOnly(dtResult.parsedDate.TimeOfDay.Ticks);
return ParseFailureKind.None;
}
/// <summary>
/// Converts the specified span representation of a time to its TimeOnly equivalent using the specified format and style.
/// The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">A span containing the characters representing a time to convert.</param>
/// <param name="format">The required format of s.</param>
/// <param name="result">When this method returns, contains the TimeOnly value equivalent to the time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s is empty string, or does not contain a time that correspond to the pattern specified in format. This parameter is passed uninitialized.</param>
/// <returns>true if s was converted successfully; otherwise, false.</returns>
public static bool TryParseExact(ReadOnlySpan<char> s, [StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] ReadOnlySpan<char> format, out TimeOnly result) => TryParseExact(s, format, null, DateTimeStyles.None, out result);
/// <summary>
/// Converts the specified span representation of a time to its TimeOnly equivalent using the specified format, culture-specific format information, and style.
/// The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">A span containing the characters representing a time to convert.</param>
/// <param name="format">The required format of s.</param>
/// <param name="provider">An object that supplies culture-specific formatting information about s.</param>
/// <param name="style">A bitwise combination of one or more enumeration values that indicate the permitted format of s.</param>
/// <param name="result">When this method returns, contains the TimeOnly value equivalent to the time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s is empty string, or does not contain a time that correspond to the pattern specified in format. This parameter is passed uninitialized.</param>
/// <returns>true if s was converted successfully; otherwise, false.</returns>
public static bool TryParseExact(ReadOnlySpan<char> s, [StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] ReadOnlySpan<char> format, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result) =>
TryParseExactInternal(s, format, provider, style, out result) == ParseFailureKind.None;
private static ParseFailureKind TryParseExactInternal(ReadOnlySpan<char> s, ReadOnlySpan<char> format, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result)
{
if ((style & ~DateTimeStyles.AllowWhiteSpaces) != 0)
{
result = default;
return ParseFailureKind.FormatWithParameter;
}
if (format.Length == 1)
{
switch (format[0])
{
case 'o':
case 'O':
format = OFormat;
provider = CultureInfo.InvariantCulture.DateTimeFormat;
break;
case 'r':
case 'R':
format = RFormat;
provider = CultureInfo.InvariantCulture.DateTimeFormat;
break;
}
}
DateTimeResult dtResult = default;
dtResult.Init(s);
if (!DateTimeParse.TryParseExact(s, format, DateTimeFormatInfo.GetInstance(provider), style, ref dtResult))
{
result = default;
return ParseFailureKind.FormatWithOriginalDateTime;
}
if ((dtResult.flags & ParseFlagsTimeMask) != 0)
{
result = default;
return ParseFailureKind.WrongParts;
}
result = new TimeOnly(dtResult.parsedDate.TimeOfDay.Ticks);
return ParseFailureKind.None;
}
/// <summary>
/// Converts the specified char span of a time to its TimeOnly equivalent and returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">The span containing the string to parse.</param>
/// <param name="formats">An array of allowable formats of s.</param>
/// <param name="result">When this method returns, contains the TimeOnly value equivalent to the time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is Empty, or does not contain a valid string representation of a time. This parameter is passed uninitialized.</param>
/// <returns>true if the s parameter was converted successfully; otherwise, false.</returns>
public static bool TryParseExact(ReadOnlySpan<char> s, [NotNullWhen(true), StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] string?[]? formats, out TimeOnly result) => TryParseExact(s, formats, null, DateTimeStyles.None, out result);
/// <summary>
/// Converts the specified char span of a time to its TimeOnly equivalent and returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">The span containing the string to parse.</param>
/// <param name="formats">An array of allowable formats of s.</param>
/// <param name="provider">An object that supplies culture-specific formatting information about s.</param>
/// <param name="style">A bitwise combination of enumeration values that defines how to interpret the parsed time. A typical value to specify is None.</param>
/// <param name="result">When this method returns, contains the TimeOnly value equivalent to the time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is Empty, or does not contain a valid string representation of a time. This parameter is passed uninitialized.</param>
/// <returns>true if the s parameter was converted successfully; otherwise, false.</returns>
public static bool TryParseExact(ReadOnlySpan<char> s, [NotNullWhen(true), StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] string?[]? formats, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result) =>
TryParseExactInternal(s, formats, provider, style, out result) == ParseFailureKind.None;
private static ParseFailureKind TryParseExactInternal(ReadOnlySpan<char> s, string?[]? formats, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result)
{
if ((style & ~DateTimeStyles.AllowWhiteSpaces) != 0 || formats == null)
{
result = default;
return ParseFailureKind.FormatWithParameter;
}
DateTimeFormatInfo dtfi = DateTimeFormatInfo.GetInstance(provider);
for (int i = 0; i < formats.Length; i++)
{
DateTimeFormatInfo dtfiToUse = dtfi;
string? format = formats[i];
if (string.IsNullOrEmpty(format))
{
result = default;
return ParseFailureKind.FormatWithFormatSpecifier;
}
if (format.Length == 1)
{
switch (format[0])
{
case 'o':
case 'O':
format = OFormat;
dtfiToUse = CultureInfo.InvariantCulture.DateTimeFormat;
break;
case 'r':
case 'R':
format = RFormat;
dtfiToUse = CultureInfo.InvariantCulture.DateTimeFormat;
break;
}
}
// Create a new result each time to ensure the runs are independent. Carry through
// flags from the caller and return the result.
DateTimeResult dtResult = default;
dtResult.Init(s);
if (DateTimeParse.TryParseExact(s, format, dtfiToUse, style, ref dtResult) && ((dtResult.flags & ParseFlagsTimeMask) == 0))
{
result = new TimeOnly(dtResult.parsedDate.TimeOfDay.Ticks);
return ParseFailureKind.None;
}
}
result = default;
return ParseFailureKind.FormatWithOriginalDateTime;
}
/// <summary>
/// Converts the specified string representation of a time to its TimeOnly equivalent and returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">A string containing the characters representing the time to convert.</param>
/// <param name="result">When this method returns, contains the TimeOnly value equivalent to the time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is empty string, or does not contain a valid string representation of a time. This parameter is passed uninitialized.</param>
/// <returns>true if the s parameter was converted successfully; otherwise, false.</returns>
public static bool TryParse([NotNullWhen(true)] string? s, out TimeOnly result) => TryParse(s, null, DateTimeStyles.None, out result);
/// <summary>
/// Converts the specified string representation of a time to its TimeOnly equivalent using the specified array of formats, culture-specific format information, and style. And returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">A string containing the characters that represent a time to convert.</param>
/// <param name="provider">An object that supplies culture-specific formatting information about s.</param>
/// <param name="style">A bitwise combination of enumeration values that indicates the permitted format of s. A typical value to specify is None.</param>
/// <param name="result">When this method returns, contains the TimeOnly value equivalent to the time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is empty string, or does not contain a valid string representation of a time. This parameter is passed uninitialized.</param>
/// <returns>true if the s parameter was converted successfully; otherwise, false.</returns>
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result)
{
if (s == null)
{
result = default;
return false;
}
return TryParse(s.AsSpan(), provider, style, out result);
}
/// <summary>
/// Converts the specified string representation of a time to its TimeOnly equivalent using the specified format and style.
/// The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">A string containing the characters representing a time to convert.</param>
/// <param name="format">The required format of s.</param>
/// <param name="result">When this method returns, contains the TimeOnly value equivalent to the time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s is empty string, or does not contain a time that correspond to the pattern specified in format. This parameter is passed uninitialized.</param>
/// <returns>true if s was converted successfully; otherwise, false.</returns>
public static bool TryParseExact([NotNullWhen(true)] string? s, [NotNullWhen(true), StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] string? format, out TimeOnly result) => TryParseExact(s, format, null, DateTimeStyles.None, out result);
/// <summary>
/// Converts the specified span representation of a time to its TimeOnly equivalent using the specified format, culture-specific format information, and style.
/// The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">A span containing the characters representing a time to convert.</param>
/// <param name="format">The required format of s.</param>
/// <param name="provider">An object that supplies culture-specific formatting information about s.</param>
/// <param name="style">A bitwise combination of one or more enumeration values that indicate the permitted format of s.</param>
/// <param name="result">When this method returns, contains the TimeOnly value equivalent to the time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s is empty string, or does not contain a time that correspond to the pattern specified in format. This parameter is passed uninitialized.</param>
/// <returns>true if s was converted successfully; otherwise, false.</returns>
public static bool TryParseExact([NotNullWhen(true)] string? s, [NotNullWhen(true), StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] string? format, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result)
{
if (s == null || format == null)
{
result = default;
return false;
}
return TryParseExact(s.AsSpan(), format.AsSpan(), provider, style, out result);
}
/// <summary>
/// Converts the specified string of a time to its TimeOnly equivalent and returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">The string containing time to parse.</param>
/// <param name="formats">An array of allowable formats of s.</param>
/// <param name="result">When this method returns, contains the timeOnly value equivalent to the time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is Empty, or does not contain a valid string representation of a time. This parameter is passed uninitialized.</param>
/// <returns>true if the s parameter was converted successfully; otherwise, false.</returns>
public static bool TryParseExact([NotNullWhen(true)] string? s, [NotNullWhen(true), StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] string?[]? formats, out TimeOnly result) => TryParseExact(s, formats, null, DateTimeStyles.None, out result);
/// <summary>
/// Converts the specified string of a time to its TimeOnly equivalent and returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">The string containing the time to parse.</param>
/// <param name="formats">An array of allowable formats of s.</param>
/// <param name="provider">An object that supplies culture-specific formatting information about s.</param>
/// <param name="style">A bitwise combination of enumeration values that defines how to interpret the parsed date. A typical value to specify is None.</param>
/// <param name="result">When this method returns, contains the TimeOnly value equivalent to the time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is Empty, or does not contain a valid string representation of a time. This parameter is passed uninitialized.</param>
/// <returns>true if the s parameter was converted successfully; otherwise, false.</returns>
public static bool TryParseExact([NotNullWhen(true)] string? s, [NotNullWhen(true), StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] string?[]? formats, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result)
{
if (s == null)
{
result = default;
return false;
}
return TryParseExact(s.AsSpan(), formats, provider, style, out result);
}
private static void ThrowOnError(ParseFailureKind result, ReadOnlySpan<char> s)
{
Debug.Assert(result != ParseFailureKind.None);
switch (result)
{
case ParseFailureKind.FormatWithParameter: throw new ArgumentException(SR.Argument_InvalidDateStyles, "style");
case ParseFailureKind.FormatWithOriginalDateTime: throw new FormatException(SR.Format(SR.Format_BadTimeOnly, s.ToString()));
case ParseFailureKind.FormatWithFormatSpecifier: throw new FormatException(SR.Argument_BadFormatSpecifier);
default:
Debug.Assert(result == ParseFailureKind.WrongParts);
throw new FormatException(SR.Format(SR.Format_DateTimeOnlyContainsNoneDateParts, s.ToString(), nameof(TimeOnly)));
}
}
/// <summary>
/// Converts the value of the current TimeOnly object to its equivalent long date string representation.
/// </summary>
/// <returns>A string that contains the long time string representation of the current TimeOnly object.</returns>
public string ToLongTimeString() => ToString("T");
/// <summary>
/// Converts the value of the current TimeOnly object to its equivalent short time string representation.
/// </summary>
/// <returns>A string that contains the short time string representation of the current TimeOnly object.</returns>
public string ToShortTimeString() => ToString();
/// <summary>
/// Converts the value of the current TimeOnly object to its equivalent string representation using the formatting conventions of the current culture.
/// The TimeOnly object will be formatted in short form.
/// </summary>
/// <returns>A string that contains the short time string representation of the current TimeOnly object.</returns>
public override string ToString() => ToString("t");
/// <summary>
/// Converts the value of the current TimeOnly object to its equivalent string representation using the specified format and the formatting conventions of the current culture.
/// </summary>
/// <param name="format">A standard or custom time format string.</param>
/// <returns>A string representation of value of the current TimeOnly object as specified by format.</returns>
/// <remarks>The accepted standard formats are 'r', 'R', 'o', 'O', 't' and 'T'. </remarks>
public string ToString([StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] string? format) => ToString(format, null);
/// <summary>
/// Converts the value of the current TimeOnly object to its equivalent string representation using the specified culture-specific format information.
/// </summary>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>A string representation of value of the current TimeOnly object as specified by provider.</returns>
public string ToString(IFormatProvider? provider) => ToString("t", provider);
/// <summary>
/// Converts the value of the current TimeOnly object to its equivalent string representation using the specified culture-specific format information.
/// </summary>
/// <param name="format">A standard or custom time format string.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>A string representation of value of the current TimeOnly object as specified by format and provider.</returns>
/// <remarks>The accepted standard formats are 'r', 'R', 'o', 'O', 't' and 'T'. </remarks>
public string ToString([StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] string? format, IFormatProvider? provider)
{
if (format == null || format.Length == 0)
{
format = "t";
}
if (format.Length == 1)
{
switch (format[0])
{
case 'o':
case 'O':
return string.Create(16, this, (destination, value) =>
{
bool b = DateTimeFormat.TryFormatTimeOnlyO(value.Hour, value.Minute, value.Second, value._ticks % TimeSpan.TicksPerSecond, destination);
Debug.Assert(b);
});
case 'r':
case 'R':
return string.Create(8, this, (destination, value) =>
{
bool b = DateTimeFormat.TryFormatTimeOnlyR(value.Hour, value.Minute, value.Second, destination);
Debug.Assert(b);
});
case 't':
case 'T':
return DateTimeFormat.Format(ToDateTime(), format, provider);
default:
throw new FormatException(SR.Format_InvalidString);
}
}
DateTimeFormat.IsValidCustomTimeFormat(format.AsSpan(), throwOnError: true);
return DateTimeFormat.Format(ToDateTime(), format, provider);
}
/// <summary>
/// Tries to format the value of the current TimeOnly instance into the provided span of characters.
/// </summary>
/// <param name="destination">When this method returns, this instance's value formatted as a span of characters.</param>
/// <param name="charsWritten">When this method returns, the number of characters that were written in destination.</param>
/// <param name="format">A span containing the characters that represent a standard or custom format string that defines the acceptable format for destination.</param>
/// <param name="provider">An optional object that supplies culture-specific formatting information for destination.</param>
/// <returns>true if the formatting was successful; otherwise, false.</returns>
/// <remarks>The accepted standard formats are 'r', 'R', 'o', 'O', 't' and 'T'. </remarks>
public bool TryFormat(Span<char> destination, out int charsWritten, [StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] ReadOnlySpan<char> format = default(ReadOnlySpan<char>), IFormatProvider? provider = null)
{
if (format.Length == 0)
{
format = "t";
}
if (format.Length == 1)
{
switch (format[0])
{
case 'o':
case 'O':
if (!DateTimeFormat.TryFormatTimeOnlyO(Hour, Minute, Second, _ticks % TimeSpan.TicksPerSecond, destination))
{
charsWritten = 0;
return false;
}
charsWritten = 16;
return true;
case 'r':
case 'R':
if (!DateTimeFormat.TryFormatTimeOnlyR(Hour, Minute, Second, destination))
{
charsWritten = 0;
return false;
}
charsWritten = 8;
return true;
case 't':
case 'T':
return DateTimeFormat.TryFormat(ToDateTime(), destination, out charsWritten, format, provider);
default:
throw new FormatException(SR.Argument_BadFormatSpecifier);
}
}
if (!DateTimeFormat.IsValidCustomTimeFormat(format, throwOnError: false))
{
throw new FormatException(SR.Format(SR.Format_DateTimeOnlyContainsNoneDateParts, format.ToString(), nameof(TimeOnly)));
}
return DateTimeFormat.TryFormat(ToDateTime(), destination, out charsWritten, format, provider);
}
//
// IParsable
//
public static TimeOnly Parse(string s, IFormatProvider? provider) => Parse(s, provider, DateTimeStyles.None);
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, out TimeOnly result) => TryParse(s, provider, DateTimeStyles.None, out result);
//
// ISpanParsable
//
/// <inheritdoc cref="ISpanParsable{TSelf}.Parse(ReadOnlySpan{char}, IFormatProvider?)" />
public static TimeOnly Parse(ReadOnlySpan<char> s, IFormatProvider? provider) => Parse(s, provider, DateTimeStyles.None);
/// <inheritdoc cref="ISpanParsable{TSelf}.TryParse(ReadOnlySpan{char}, IFormatProvider?, out TSelf)" />
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out TimeOnly result) => TryParse(s, provider, DateTimeStyles.None, out result);
//
// ISubtractionOperators
//
/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
static TimeSpan ISubtractionOperators<TimeOnly, TimeOnly, TimeSpan>.operator checked -(TimeOnly left, TimeOnly right) => left - right;
}
}