/
DateOnly.cs
851 lines (736 loc) · 53.1 KB
/
DateOnly.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
// 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.Globalization;
using System.Numerics;
using System.Runtime.Versioning;
namespace System
{
/// <summary>
/// Represents dates with values ranging from January 1, 0001 Anno Domini (Common Era) through December 31, 9999 A.D. (C.E.) in the Gregorian calendar.
/// </summary>
public readonly struct DateOnly
: IComparable,
IComparable<DateOnly>,
IEquatable<DateOnly>,
ISpanFormattable,
ISpanParsable<DateOnly>
{
private readonly int _dayNumber;
// Maps to Jan 1st year 1
private const int MinDayNumber = 0;
// Maps to December 31 year 9999. The value calculated from "new DateTime(9999, 12, 31).Ticks / TimeSpan.TicksPerDay"
private const int MaxDayNumber = 3_652_058;
private static int DayNumberFromDateTime(DateTime dt) => (int)((ulong)dt.Ticks / TimeSpan.TicksPerDay);
private DateTime GetEquivalentDateTime() => DateTime.UnsafeCreate(_dayNumber * TimeSpan.TicksPerDay);
private DateOnly(int dayNumber)
{
Debug.Assert((uint)dayNumber <= MaxDayNumber);
_dayNumber = dayNumber;
}
/// <summary>
/// Gets the earliest possible date that can be created.
/// </summary>
public static DateOnly MinValue => new DateOnly(MinDayNumber);
/// <summary>
/// Gets the latest possible date that can be created.
/// </summary>
public static DateOnly MaxValue => new DateOnly(MaxDayNumber);
/// <summary>
/// Creates a new instance of the DateOnly structure to the specified year, month, and day.
/// </summary>
/// <param name="year">The year (1 through 9999).</param>
/// <param name="month">The month (1 through 12).</param>
/// <param name="day">The day (1 through the number of days in <paramref name="month" />).</param>
public DateOnly(int year, int month, int day) => _dayNumber = DayNumberFromDateTime(new DateTime(year, month, day));
/// <summary>
/// Creates a new instance of the DateOnly structure to the specified year, month, and day for the specified calendar.
/// </summary>
/// <param name="year">The year (1 through the number of years in calendar).</param>
/// <param name="month">The month (1 through the number of months in calendar).</param>
/// <param name="day">The day (1 through the number of days in <paramref name="month"/>).</param>
/// <param name="calendar">The calendar that is used to interpret year, month, and day.<paramref name="month"/>.</param>
public DateOnly(int year, int month, int day, Calendar calendar) => _dayNumber = DayNumberFromDateTime(new DateTime(year, month, day, calendar));
/// <summary>
/// Creates a new instance of the DateOnly structure to the specified number of days.
/// </summary>
/// <param name="dayNumber">The number of days since January 1, 0001 in the Proleptic Gregorian calendar.</param>
public static DateOnly FromDayNumber(int dayNumber)
{
if ((uint)dayNumber > MaxDayNumber)
{
ThrowHelper.ThrowArgumentOutOfRange_DayNumber(dayNumber);
}
return new DateOnly(dayNumber);
}
/// <summary>
/// Gets the year component of the date represented by this instance.
/// </summary>
public int Year => GetEquivalentDateTime().Year;
/// <summary>
/// Gets the month component of the date represented by this instance.
/// </summary>
public int Month => GetEquivalentDateTime().Month;
/// <summary>
/// Gets the day component of the date represented by this instance.
/// </summary>
public int Day => GetEquivalentDateTime().Day;
/// <summary>
/// Gets the day of the week represented by this instance.
/// </summary>
public DayOfWeek DayOfWeek => (DayOfWeek)(((uint)_dayNumber + 1) % 7);
/// <summary>
/// Gets the day of the year represented by this instance.
/// </summary>
public int DayOfYear => GetEquivalentDateTime().DayOfYear;
/// <summary>
/// Gets the number of days since January 1, 0001 in the Proleptic Gregorian calendar represented by this instance.
/// </summary>
public int DayNumber => _dayNumber;
/// <summary>
/// Adds the specified number of days to the value of this instance.
/// </summary>
/// <param name="value">The number of days to add. To subtract days, specify a negative number.</param>
/// <returns>An instance whose value is the sum of the date represented by this instance and the number of days represented by value.</returns>
public DateOnly AddDays(int value)
{
int newDayNumber = _dayNumber + value;
if ((uint)newDayNumber > MaxDayNumber)
{
ThrowOutOfRange();
}
return new DateOnly(newDayNumber);
static void ThrowOutOfRange() => throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_AddValue);
}
/// <summary>
/// Adds the specified number of months to the value of this instance.
/// </summary>
/// <param name="value">A number of months. The months parameter can be negative or positive.</param>
/// <returns>An object whose value is the sum of the date represented by this instance and months.</returns>
public DateOnly AddMonths(int value) => new DateOnly(DayNumberFromDateTime(GetEquivalentDateTime().AddMonths(value)));
/// <summary>
/// Adds the specified number of years to the value of this instance.
/// </summary>
/// <param name="value">A number of years. The value parameter can be negative or positive.</param>
/// <returns>An object whose value is the sum of the date represented by this instance and the number of years represented by value.</returns>
public DateOnly AddYears(int value) => new DateOnly(DayNumberFromDateTime(GetEquivalentDateTime().AddYears(value)));
/// <summary>
/// Determines whether two specified instances of DateOnly 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 date; otherwise, false.</returns>
public static bool operator ==(DateOnly left, DateOnly right) => left._dayNumber == right._dayNumber;
/// <summary>
/// Determines whether two specified instances of DateOnly 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 date; otherwise, false.</returns>
public static bool operator !=(DateOnly left, DateOnly right) => left._dayNumber != right._dayNumber;
/// <summary>
/// Determines whether one specified DateOnly is later than another specified DateTime.
/// </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>
public static bool operator >(DateOnly left, DateOnly right) => left._dayNumber > right._dayNumber;
/// <summary>
/// Determines whether one specified DateOnly represents a date that is the same as or later than another specified DateOnly.
/// </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>
public static bool operator >=(DateOnly left, DateOnly right) => left._dayNumber >= right._dayNumber;
/// <summary>
/// Determines whether one specified DateOnly is earlier than another specified DateOnly.
/// </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>
public static bool operator <(DateOnly left, DateOnly right) => left._dayNumber < right._dayNumber;
/// <summary>
/// Determines whether one specified DateOnly represents a date that is the same as or earlier than another specified DateOnly.
/// </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>
public static bool operator <=(DateOnly left, DateOnly right) => left._dayNumber <= right._dayNumber;
/// <summary>
/// Returns a DateTime that is set to the date of this DateOnly instance and the time of specified input time.
/// </summary>
/// <param name="time">The time of the day.</param>
/// <returns>The DateTime instance composed of the date of the current DateOnly instance and the time specified by the input time.</returns>
public DateTime ToDateTime(TimeOnly time) => new DateTime(_dayNumber * TimeSpan.TicksPerDay + time.Ticks);
/// <summary>
/// Returns a DateTime instance with the specified input kind that is set to the date of this DateOnly instance and the time of specified input time.
/// </summary>
/// <param name="time">The time of the day.</param>
/// <param name="kind">One of the enumeration values that indicates whether ticks specifies a local time, Coordinated Universal Time (UTC), or neither.</param>
/// <returns>The DateTime instance composed of the date of the current DateOnly instance and the time specified by the input time.</returns>
public DateTime ToDateTime(TimeOnly time, DateTimeKind kind) => new DateTime(_dayNumber * TimeSpan.TicksPerDay + time.Ticks, kind);
/// <summary>
/// Returns a DateOnly instance that is set to the date part of the specified dateTime.
/// </summary>
/// <param name="dateTime">The DateTime instance.</param>
/// <returns>The DateOnly instance composed of the date part of the specified input time dateTime instance.</returns>
public static DateOnly FromDateTime(DateTime dateTime) => new DateOnly(DayNumberFromDateTime(dateTime));
/// <summary>
/// Compares the value of this instance to a specified DateOnly value and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.
/// </summary>
/// <param name="value">The object to compare to the current instance.</param>
/// <returns>Less than zero if this instance is earlier than value. Greater than zero if this instance is later than value. Zero if this instance is the same as value.</returns>
public int CompareTo(DateOnly value) => _dayNumber.CompareTo(value._dayNumber);
/// <summary>
/// Compares the value of this instance to a specified object that contains a specified DateOnly value, and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateOnly value.
/// </summary>
/// <param name="value">A boxed object to compare, or null.</param>
/// <returns>Less than zero if this instance is earlier than value. Greater than zero if this instance is later than value. Zero if this instance is the same as value.</returns>
public int CompareTo(object? value)
{
if (value == null) return 1;
if (value is not DateOnly dateOnly)
{
throw new ArgumentException(SR.Arg_MustBeDateOnly);
}
return CompareTo(dateOnly);
}
/// <summary>
/// Returns a value indicating whether the value of this instance is equal to the value of the specified DateOnly 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(DateOnly value) => _dayNumber == value._dayNumber;
/// <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 DateOnly and equals the value of this instance; otherwise, false.</returns>
public override bool Equals([NotNullWhen(true)] object? value) => value is DateOnly dateOnly && _dayNumber == dateOnly._dayNumber;
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>A 32-bit signed integer hash code.</returns>
public override int GetHashCode() => _dayNumber;
private const ParseFlags ParseFlagsDateMask = ParseFlags.HaveHour | ParseFlags.HaveMinute | ParseFlags.HaveSecond | ParseFlags.HaveTime | ParseFlags.TimeZoneUsed |
ParseFlags.TimeZoneUtc | ParseFlags.CaptureOffset | ParseFlags.UtcSortPattern;
/// <summary>
/// Converts a memory span that contains string representation of a date to its DateOnly 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 date contained in s, as specified by provider and styles.</returns>
public static DateOnly Parse(ReadOnlySpan<char> s, IFormatProvider? provider = default, DateTimeStyles style = DateTimeStyles.None)
{
ParseFailureKind result = TryParseInternal(s, provider, style, out DateOnly dateOnly);
if (result != ParseFailureKind.None)
{
ThrowOnError(result, s);
}
return dateOnly;
}
private const string OFormat = "yyyy'-'MM'-'dd";
private const string RFormat = "ddd, dd MMM yyyy";
/// <summary>
/// Converts the specified span representation of a date to its DateOnly 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 date 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 date contained in s, as specified by format, provider, and style.</returns>
public static DateOnly ParseExact(ReadOnlySpan<char> s, [StringSyntax(StringSyntaxAttribute.DateOnlyFormat)] ReadOnlySpan<char> format, IFormatProvider? provider = default, DateTimeStyles style = DateTimeStyles.None)
{
ParseFailureKind result = TryParseExactInternal(s, format, provider, style, out DateOnly dateOnly);
if (result != ParseFailureKind.None)
{
ThrowOnError(result, s);
}
return dateOnly;
}
/// <summary>
/// Converts the specified span representation of a date to its DateOnly 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 date to convert.</param>
/// <param name="formats">An array of allowable formats of s.</param>
/// <returns>An object that is equivalent to the date contained in s, as specified by format, provider, and style.</returns>
public static DateOnly ParseExact(ReadOnlySpan<char> s, [StringSyntax(StringSyntaxAttribute.DateOnlyFormat)] string[] formats) => ParseExact(s, formats, null, DateTimeStyles.None);
/// <summary>
/// Converts the specified span representation of a date to its DateOnly 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 date 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 date contained in s, as specified by format, provider, and style.</returns>
public static DateOnly ParseExact(ReadOnlySpan<char> s, [StringSyntax(StringSyntaxAttribute.DateOnlyFormat)] string[] formats, IFormatProvider? provider, DateTimeStyles style = DateTimeStyles.None)
{
ParseFailureKind result = TryParseExactInternal(s, formats, provider, style, out DateOnly dateOnly);
if (result != ParseFailureKind.None)
{
ThrowOnError(result, s);
}
return dateOnly;
}
/// <summary>
/// Converts a string that contains string representation of a date to its DateOnly 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 date contained in s.</returns>
public static DateOnly Parse(string s) => Parse(s, null, DateTimeStyles.None);
/// <summary>
/// Converts a string that contains string representation of a date to its DateOnly 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 date contained in s, as specified by provider and styles.</returns>
public static DateOnly 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 date to its DateOnly 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 date 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 date contained in s, as specified by format.</returns>
public static DateOnly ParseExact(string s, [StringSyntax(StringSyntaxAttribute.DateOnlyFormat)] string format) => ParseExact(s, format, null, DateTimeStyles.None);
/// <summary>
/// Converts the specified string representation of a date to its DateOnly 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 date 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 DateOnly value. A typical value to specify is None.</param>
/// <returns>An object that is equivalent to the date contained in s, as specified by format, provider, and style.</returns>
public static DateOnly ParseExact(string s, [StringSyntax(StringSyntaxAttribute.DateOnlyFormat)] 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 date to its DateOnly 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 date to convert.</param>
/// <param name="formats">An array of allowable formats of s.</param>
/// <returns>An object that is equivalent to the date contained in s, as specified by format, provider, and style.</returns>
public static DateOnly ParseExact(string s, [StringSyntax(StringSyntaxAttribute.DateOnlyFormat)] string[] formats) => ParseExact(s, formats, null, DateTimeStyles.None);
/// <summary>
/// Converts the specified string representation of a date to its DateOnly 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 date 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 date contained in s, as specified by format, provider, and style.</returns>
public static DateOnly ParseExact(string s, [StringSyntax(StringSyntaxAttribute.DateOnlyFormat)] 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 date to its DateOnly equivalent and returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">A span containing the characters representing the date to convert.</param>
/// <param name="result">When this method returns, contains the DateOnly value equivalent to the date 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>
public static bool TryParse(ReadOnlySpan<char> s, out DateOnly result) => TryParse(s, null, DateTimeStyles.None, out result);
/// <summary>
/// Converts the specified span representation of a date to its DateOnly 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 date 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 DateOnly value equivalent to the date 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>
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, DateTimeStyles style, out DateOnly result) => TryParseInternal(s, provider, style, out result) == ParseFailureKind.None;
private static ParseFailureKind TryParseInternal(ReadOnlySpan<char> s, IFormatProvider? provider, DateTimeStyles style, out DateOnly 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 & ParseFlagsDateMask) != 0)
{
result = default;
return ParseFailureKind.WrongParts;
}
result = new DateOnly(DayNumberFromDateTime(dtResult.parsedDate));
return ParseFailureKind.None;
}
/// <summary>
/// Converts the specified span representation of a date to its DateOnly 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 date to convert.</param>
/// <param name="format">The required format of s.</param>
/// <param name="result">When this method returns, contains the DateOnly value equivalent to the date 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 date 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.DateOnlyFormat)] ReadOnlySpan<char> format, out DateOnly result) => TryParseExact(s, format, null, DateTimeStyles.None, out result);
/// <summary>
/// Converts the specified span representation of a date to its DateOnly 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 date 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 DateOnly value equivalent to the date 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 date 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.DateOnlyFormat)] ReadOnlySpan<char> format, IFormatProvider? provider, DateTimeStyles style, out DateOnly 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 DateOnly 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 & ParseFlagsDateMask) != 0)
{
result = default;
return ParseFailureKind.WrongParts;
}
result = new DateOnly(DayNumberFromDateTime(dtResult.parsedDate));
return ParseFailureKind.None;
}
/// <summary>
/// Converts the specified char span of a date to its DateOnly 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 DateOnly value equivalent to the date 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 date. 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.DateOnlyFormat)] string?[]? formats, out DateOnly result) => TryParseExact(s, formats, null, DateTimeStyles.None, out result);
/// <summary>
/// Converts the specified char span of a date to its DateOnly 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 date. A typical value to specify is None.</param>
/// <param name="result">When this method returns, contains the DateOnly value equivalent to the date 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 date. 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.DateOnlyFormat)] string?[]? formats, IFormatProvider? provider, DateTimeStyles style, out DateOnly result) =>
TryParseExactInternal(s, formats, provider, style, out result) == ParseFailureKind.None;
private static ParseFailureKind TryParseExactInternal(ReadOnlySpan<char> s, string?[]? formats, IFormatProvider? provider, DateTimeStyles style, out DateOnly 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 & ParseFlagsDateMask) == 0))
{
result = new DateOnly(DayNumberFromDateTime(dtResult.parsedDate));
return ParseFailureKind.None;
}
}
result = default;
return ParseFailureKind.FormatWithOriginalDateTime;
}
/// <summary>
/// Converts the specified string representation of a date to its DateOnly equivalent and returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">A string containing the characters representing the date to convert.</param>
/// <param name="result">When this method returns, contains the DateOnly value equivalent to the date 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>
public static bool TryParse([NotNullWhen(true)] string? s, out DateOnly result) => TryParse(s, null, DateTimeStyles.None, out result);
/// <summary>
/// Converts the specified string representation of a date to its DateOnly 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 date 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 DateOnly value equivalent to the date 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>
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, DateTimeStyles style, out DateOnly result)
{
if (s == null)
{
result = default;
return false;
}
return TryParse(s.AsSpan(), provider, style, out result);
}
/// <summary>
/// Converts the specified string representation of a date to its DateOnly 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 date to convert.</param>
/// <param name="format">The required format of s.</param>
/// <param name="result">When this method returns, contains the DateOnly value equivalent to the date 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 date 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.DateOnlyFormat)] string? format, out DateOnly result) => TryParseExact(s, format, null, DateTimeStyles.None, out result);
/// <summary>
/// Converts the specified span representation of a date to its DateOnly 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 date 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 DateOnly value equivalent to the date 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 date 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.DateOnlyFormat)] string? format, IFormatProvider? provider, DateTimeStyles style, out DateOnly 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 date to its DateOnly equivalent and returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">The string containing date to parse.</param>
/// <param name="formats">An array of allowable formats of s.</param>
/// <param name="result">When this method returns, contains the DateOnly value equivalent to the date 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 date. 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.DateOnlyFormat)] string?[]? formats, out DateOnly result) => TryParseExact(s, formats, null, DateTimeStyles.None, out result);
/// <summary>
/// Converts the specified string of a date to its DateOnly equivalent and returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">The string containing the date 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 DateOnly value equivalent to the date 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 date. 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.DateOnlyFormat)] string?[]? formats, IFormatProvider? provider, DateTimeStyles style, out DateOnly 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_BadDateOnly, 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(DateOnly)));
}
}
/// <summary>
/// Converts the value of the current DateOnly object to its equivalent long date string representation.
/// </summary>
/// <returns>A string that contains the long date string representation of the current DateOnly object.</returns>
public string ToLongDateString() => ToString("D");
/// <summary>
/// Converts the value of the current DateOnly object to its equivalent short date string representation.
/// </summary>
/// <returns>A string that contains the short date string representation of the current DateOnly object.</returns>
public string ToShortDateString() => ToString();
/// <summary>
/// Converts the value of the current DateOnly object to its equivalent string representation using the formatting conventions of the current culture.
/// The DateOnly object will be formatted in short form.
/// </summary>
/// <returns>A string that contains the short date string representation of the current DateOnly object.</returns>
public override string ToString() => ToString("d");
/// <summary>
/// Converts the value of the current DateOnly 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 date format string.</param>
/// <returns>A string representation of value of the current DateOnly object as specified by format.</returns>
public string ToString([StringSyntax(StringSyntaxAttribute.DateOnlyFormat)] string? format) => ToString(format, null);
/// <summary>
/// Converts the value of the current DateOnly 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 DateOnly object as specified by provider.</returns>
public string ToString(IFormatProvider? provider) => ToString("d", provider);
/// <summary>
/// Converts the value of the current DateOnly object to its equivalent string representation using the specified culture-specific format information.
/// </summary>
/// <param name="format">A standard or custom date format string.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>A string representation of value of the current DateOnly object as specified by format and provider.</returns>
public string ToString([StringSyntax(StringSyntaxAttribute.DateOnlyFormat)] string? format, IFormatProvider? provider)
{
if (format == null || format.Length == 0)
{
format = "d";
}
if (format.Length == 1)
{
switch (format[0])
{
case 'o':
case 'O':
return string.Create(10, this, (destination, value) =>
{
bool b = DateTimeFormat.TryFormatDateOnlyO(value.Year, value.Month, value.Day, destination);
Debug.Assert(b);
});
case 'r':
case 'R':
return string.Create(16, this, (destination, value) =>
{
bool b = DateTimeFormat.TryFormatDateOnlyR(value.DayOfWeek, value.Year, value.Month, value.Day, destination);
Debug.Assert(b);
});
case 'm':
case 'M':
case 'd':
case 'D':
case 'y':
case 'Y':
return DateTimeFormat.Format(GetEquivalentDateTime(), format, provider);
default:
throw new FormatException(SR.Format_InvalidString);
}
}
DateTimeFormat.IsValidCustomDateFormat(format.AsSpan(), throwOnError: true);
return DateTimeFormat.Format(GetEquivalentDateTime(), format, provider);
}
/// <summary>
/// Tries to format the value of the current DateOnly 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>
public bool TryFormat(Span<char> destination, out int charsWritten, [StringSyntax(StringSyntaxAttribute.DateOnlyFormat)] ReadOnlySpan<char> format = default(ReadOnlySpan<char>), IFormatProvider? provider = null)
{
if (format.Length == 0)
{
format = "d";
}
if (format.Length == 1)
{
switch (format[0])
{
case 'o':
case 'O':
if (!DateTimeFormat.TryFormatDateOnlyO(Year, Month, Day, destination))
{
charsWritten = 0;
return false;
}
charsWritten = 10;
return true;
case 'r':
case 'R':
if (!DateTimeFormat.TryFormatDateOnlyR(DayOfWeek, Year, Month, Day, destination))
{
charsWritten = 0;
return false;
}
charsWritten = 16;
return true;
case 'm':
case 'M':
case 'd':
case 'D':
case 'y':
case 'Y':
return DateTimeFormat.TryFormat(GetEquivalentDateTime(), destination, out charsWritten, format, provider);
default:
throw new FormatException(SR.Argument_BadFormatSpecifier);
}
}
if (!DateTimeFormat.IsValidCustomDateFormat(format, throwOnError: false))
{
throw new FormatException(SR.Format(SR.Format_DateTimeOnlyContainsNoneDateParts, format.ToString(), nameof(DateOnly)));
}
return DateTimeFormat.TryFormat(GetEquivalentDateTime(), destination, out charsWritten, format, provider);
}
//
// IParsable
//
/// <inheritdoc cref="IParsable{TSelf}.Parse(string, IFormatProvider?)" />
public static DateOnly Parse(string s, IFormatProvider? provider) => Parse(s, provider, DateTimeStyles.None);
/// <inheritdoc cref="IParsable{TSelf}.TryParse(string?, IFormatProvider?, out TSelf)" />
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, out DateOnly result) => TryParse(s, provider, DateTimeStyles.None, out result);
//
// ISpanParsable
//
/// <inheritdoc cref="ISpanParsable{TSelf}.Parse(ReadOnlySpan{char}, IFormatProvider?)" />
public static DateOnly 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 DateOnly result) => TryParse(s, provider, DateTimeStyles.None, out result);
}
}