/
DateTimeFormatInfo.cs
2553 lines (2222 loc) · 107 KB
/
DateTimeFormatInfo.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
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace System.Globalization
{
/// <summary>
/// Flags used to indicate different styles of month names.
/// This is an internal flag used by internalGetMonthName().
/// Use flag here in case that we need to provide a combination of these styles
/// (such as month name of a leap year in genitive form. Not likely for now,
/// but would like to keep the option open).
/// </summary>
[Flags]
internal enum MonthNameStyles
{
Regular = 0x00000000,
Genitive = 0x00000001,
LeapYear = 0x00000002,
}
/// <summary>
/// Flags used to indicate special rule used in parsing/formatting
/// for a specific DateTimeFormatInfo instance.
/// This is an internal flag.
///
/// This flag is different from MonthNameStyles because this flag
/// can be expanded to accommodate parsing behaviors like CJK month names
/// or alternative month names, etc.
/// </summary>
[Flags]
internal enum DateTimeFormatFlags
{
None = 0x00000000,
UseGenitiveMonth = 0x00000001,
UseLeapYearMonth = 0x00000002,
UseSpacesInMonthNames = 0x00000004, // Has spaces or non-breaking space in the month names.
UseHebrewRule = 0x00000008, // Format/Parse using the Hebrew calendar rule.
UseSpacesInDayNames = 0x00000010, // Has spaces or non-breaking space in the day names.
UseDigitPrefixInTokens = 0x00000020, // Has token starting with numbers.
NotInitialized = -1,
}
public sealed class DateTimeFormatInfo : IFormatProvider, ICloneable
{
// cache for the invariant culture.
// invariantInfo is constant irrespective of your current culture.
private static volatile DateTimeFormatInfo? s_invariantInfo;
// an index which points to a record in Culture Data Table.
private readonly CultureData _cultureData;
// The culture name used to create this DTFI.
private string? _name;
// The language name of the culture used to create this DTFI.
private string? _langName;
// CompareInfo usually used by the parser.
private CompareInfo? _compareInfo;
// Culture matches current DTFI. mainly used for string comparisons during parsing.
private CultureInfo? _cultureInfo;
private string? amDesignator;
private string? pmDesignator;
private string? dateSeparator; // derived from short date (whidbey expects, arrowhead doesn't)
private string? generalShortTimePattern; // short date + short time (whidbey expects, arrowhead doesn't)
private string? generalLongTimePattern; // short date + long time (whidbey expects, arrowhead doesn't)
private string? timeSeparator; // derived from long time (whidbey expects, arrowhead doesn't)
private string? monthDayPattern;
private string? dateTimeOffsetPattern;
private const string rfc1123Pattern = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'";
// The sortable pattern is based on ISO 8601.
private const string sortableDateTimePattern = "yyyy'-'MM'-'dd'T'HH':'mm':'ss";
private const string universalSortableDateTimePattern = "yyyy'-'MM'-'dd HH':'mm':'ss'Z'";
private Calendar calendar;
private int firstDayOfWeek = -1;
private int calendarWeekRule = -1;
private string? fullDateTimePattern; // long date + long time (whidbey expects, arrowhead doesn't)
private string[]? abbreviatedDayNames;
private string[]? m_superShortDayNames;
private string[]? dayNames;
private string[]? abbreviatedMonthNames;
private string[]? monthNames;
// Cache the genitive month names that we retrieve from the data table.
private string[]? genitiveMonthNames;
// Cache the abbreviated genitive month names that we retrieve from the data table.
private string[]? m_genitiveAbbreviatedMonthNames;
// Cache the month names of a leap year that we retrieve from the data table.
private string[]? leapYearMonthNames;
// For our "patterns" arrays we have 2 variables, a string and a string[]
//
// The string[] contains the list of patterns, EXCEPT the default may not be included.
// The string contains the default pattern.
// When we initially construct our string[], we set the string to string[0]
// The "default" Date/time patterns
private string? longDatePattern;
private string? shortDatePattern;
private string? yearMonthPattern;
private string? longTimePattern;
private string? shortTimePattern;
private string[]? allYearMonthPatterns;
private string[]? allShortDatePatterns;
private string[]? allLongDatePatterns;
private string[]? allShortTimePatterns;
private string[]? allLongTimePatterns;
// Cache the era names for this DateTimeFormatInfo instance.
private string[]? m_eraNames;
private string[]? m_abbrevEraNames;
private string[]? m_abbrevEnglishEraNames;
private CalendarId[]? optionalCalendars;
private const int DEFAULT_ALL_DATETIMES_SIZE = 132;
// CultureInfo updates this
internal bool _isReadOnly;
// This flag gives hints about if formatting/parsing should perform special code path for things like
// genitive form or leap year month names.
private DateTimeFormatFlags formatFlags = DateTimeFormatFlags.NotInitialized;
private string CultureName => _name ??= _cultureData.CultureName;
private CultureInfo Culture => _cultureInfo ??= CultureInfo.GetCultureInfo(CultureName);
private string LanguageName => _langName ??= _cultureData.TwoLetterISOLanguageName;
/// <summary>
/// Create an array of string which contains the abbreviated day names.
/// </summary>
private string[] InternalGetAbbreviatedDayOfWeekNames() => abbreviatedDayNames ?? InternalGetAbbreviatedDayOfWeekNamesCore();
[MethodImpl(MethodImplOptions.NoInlining)]
private string[] InternalGetAbbreviatedDayOfWeekNamesCore()
{
// Get the abbreviated day names for our current calendar
abbreviatedDayNames = _cultureData.AbbreviatedDayNames(Calendar.ID);
Debug.Assert(abbreviatedDayNames.Length == 7, "[DateTimeFormatInfo.GetAbbreviatedDayOfWeekNames] Expected 7 day names in a week");
return abbreviatedDayNames;
}
/// <summary>
/// Returns the string array of the one-letter day of week names.
/// </summary>
private string[] InternalGetSuperShortDayNames() => m_superShortDayNames ?? InternalGetSuperShortDayNamesCore();
[MethodImpl(MethodImplOptions.NoInlining)]
private string[] InternalGetSuperShortDayNamesCore()
{
// Get the super short day names for our current calendar
m_superShortDayNames = _cultureData.SuperShortDayNames(Calendar.ID);
Debug.Assert(m_superShortDayNames.Length == 7, "[DateTimeFormatInfo.InternalGetSuperShortDayNames] Expected 7 day names in a week");
return m_superShortDayNames;
}
/// <summary>
/// Create an array of string which contains the day names.
/// </summary>
private string[] InternalGetDayOfWeekNames() => dayNames ?? InternalGetDayOfWeekNamesCore();
[MethodImpl(MethodImplOptions.NoInlining)]
private string[] InternalGetDayOfWeekNamesCore()
{
// Get the day names for our current calendar
dayNames = _cultureData.DayNames(Calendar.ID);
Debug.Assert(dayNames.Length == 7, "[DateTimeFormatInfo.GetDayOfWeekNames] Expected 7 day names in a week");
return dayNames;
}
/// <summary>
/// Create an array of string which contains the abbreviated month names.
/// </summary>
private string[] InternalGetAbbreviatedMonthNames() => abbreviatedMonthNames ?? InternalGetAbbreviatedMonthNamesCore();
[MethodImpl(MethodImplOptions.NoInlining)]
private string[] InternalGetAbbreviatedMonthNamesCore()
{
// Get the month names for our current calendar
abbreviatedMonthNames = _cultureData.AbbreviatedMonthNames(Calendar.ID);
Debug.Assert(abbreviatedMonthNames.Length == 12 || abbreviatedMonthNames.Length == 13,
"[DateTimeFormatInfo.GetAbbreviatedMonthNames] Expected 12 or 13 month names in a year");
return abbreviatedMonthNames;
}
/// <summary>
/// Create an array of string which contains the month names.
/// </summary>
private string[] InternalGetMonthNames() => monthNames ?? internalGetMonthNamesCore();
[MethodImpl(MethodImplOptions.NoInlining)]
private string[] internalGetMonthNamesCore()
{
// Get the month names for our current calendar
monthNames = _cultureData.MonthNames(Calendar.ID);
Debug.Assert(monthNames.Length == 12 || monthNames.Length == 13,
"[DateTimeFormatInfo.GetMonthNames] Expected 12 or 13 month names in a year");
return monthNames;
}
// Invariant DateTimeFormatInfo doesn't have user-overridden values
// Default calendar is gregorian
public DateTimeFormatInfo()
: this(CultureInfo.InvariantCulture._cultureData, GregorianCalendar.GetDefaultInstance())
{
}
internal DateTimeFormatInfo(CultureData cultureData, Calendar cal)
{
Debug.Assert(cultureData != null);
Debug.Assert(cal != null);
// Remember our culture
_cultureData = cultureData;
calendar = cal;
InitializeOverridableProperties(cultureData, calendar.ID);
}
private void InitializeOverridableProperties(CultureData cultureData, CalendarId calendarId)
{
Debug.Assert(cultureData != null);
Debug.Assert(calendarId != CalendarId.UNINITIALIZED_VALUE, "[DateTimeFormatInfo.Populate] Expected initialized calendarId");
if (firstDayOfWeek == -1)
{
firstDayOfWeek = cultureData.FirstDayOfWeek;
}
if (calendarWeekRule == -1)
{
calendarWeekRule = cultureData.CalendarWeekRule;
}
amDesignator ??= cultureData.AMDesignator;
pmDesignator ??= cultureData.PMDesignator;
timeSeparator ??= cultureData.TimeSeparator;
dateSeparator ??= cultureData.DateSeparator(calendarId);
allLongTimePatterns = _cultureData.LongTimes;
Debug.Assert(allLongTimePatterns.Length > 0, "[DateTimeFormatInfo.Populate] Expected some long time patterns");
allShortTimePatterns = _cultureData.ShortTimes;
Debug.Assert(allShortTimePatterns.Length > 0, "[DateTimeFormatInfo.Populate] Expected some short time patterns");
allLongDatePatterns = cultureData.LongDates(calendarId);
Debug.Assert(allLongDatePatterns.Length > 0, "[DateTimeFormatInfo.Populate] Expected some long date patterns");
allShortDatePatterns = cultureData.ShortDates(calendarId);
Debug.Assert(allShortDatePatterns.Length > 0, "[DateTimeFormatInfo.Populate] Expected some short date patterns");
allYearMonthPatterns = cultureData.YearMonths(calendarId);
Debug.Assert(allYearMonthPatterns.Length > 0, "[DateTimeFormatInfo.Populate] Expected some year month patterns");
}
/// <summary>
/// Returns a default DateTimeFormatInfo that will be universally
/// supported and constant irrespective of the current culture.
/// </summary>
public static DateTimeFormatInfo InvariantInfo
{
get
{
if (s_invariantInfo == null)
{
DateTimeFormatInfo info = new DateTimeFormatInfo();
info.Calendar.SetReadOnlyState(true);
info._isReadOnly = true;
s_invariantInfo = info;
}
return s_invariantInfo;
}
}
/// <summary>
/// Returns the current culture's DateTimeFormatInfo.
/// </summary>
public static DateTimeFormatInfo CurrentInfo
{
get
{
System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CurrentCulture;
if (!culture._isInherited)
{
DateTimeFormatInfo? info = culture._dateTimeInfo;
if (info != null)
{
return info;
}
}
return (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo))!;
}
}
public static DateTimeFormatInfo GetInstance(IFormatProvider? provider) =>
provider == null ? CurrentInfo :
provider is CultureInfo cultureProvider && !cultureProvider._isInherited ? cultureProvider.DateTimeFormat :
provider is DateTimeFormatInfo info ? info :
provider.GetFormat(typeof(DateTimeFormatInfo)) is DateTimeFormatInfo info2 ? info2 :
CurrentInfo; // Couldn't get anything, just use currentInfo as fallback
public object? GetFormat(Type? formatType)
{
return formatType == typeof(DateTimeFormatInfo) ? this : null;
}
public object Clone()
{
DateTimeFormatInfo n = (DateTimeFormatInfo)MemberwiseClone();
// We can use the data member calendar in the setter, instead of the property Calendar,
// since the cloned copy should have the same state as the original copy.
n.calendar = (Calendar)Calendar.Clone();
n._isReadOnly = false;
return n;
}
public string AMDesignator
{
get
{
amDesignator ??= _cultureData.AMDesignator;
Debug.Assert(amDesignator != null, "DateTimeFormatInfo.AMDesignator, amDesignator != null");
return amDesignator;
}
set
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
ArgumentNullException.ThrowIfNull(value);
ClearTokenHashTable();
amDesignator = value;
}
}
public Calendar Calendar
{
get
{
Debug.Assert(calendar != null, "DateTimeFormatInfo.Calendar: calendar != null");
return calendar;
}
[MemberNotNull(nameof(calendar))]
set
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
ArgumentNullException.ThrowIfNull(value);
if (value == calendar)
{
return;
}
for (int i = 0; i < OptionalCalendars.Length; i++)
{
if (OptionalCalendars[i] == value.ID)
{
// We can use this one, so do so.
// Clean related properties if we already had a calendar set
if (calendar != null)
{
// clean related properties which are affected by the calendar setting,
// so that they will be refreshed when they are accessed next time.
// These properites are in the order as appearing in calendar.xml.
m_eraNames = null;
m_abbrevEraNames = null;
m_abbrevEnglishEraNames = null;
monthDayPattern = null;
dayNames = null;
abbreviatedDayNames = null;
m_superShortDayNames = null;
monthNames = null;
abbreviatedMonthNames = null;
genitiveMonthNames = null;
m_genitiveAbbreviatedMonthNames = null;
leapYearMonthNames = null;
formatFlags = DateTimeFormatFlags.NotInitialized;
allShortDatePatterns = null;
allLongDatePatterns = null;
allYearMonthPatterns = null;
dateTimeOffsetPattern = null;
// The defaults need reset as well:
longDatePattern = null;
shortDatePattern = null;
yearMonthPattern = null;
// These properies are not in the OS data, but they are dependent on the values like shortDatePattern.
fullDateTimePattern = null; // Long date + long time
generalShortTimePattern = null; // short date + short time
generalLongTimePattern = null; // short date + long time
// Derived item that changes
dateSeparator = null;
// We don't need to do these because they are not changed by changing calendar
// amDesignator
// pmDesignator
// timeSeparator
// longTimePattern
// firstDayOfWeek
// calendarWeekRule
// remember to reload tokens
ClearTokenHashTable();
}
// Remember the new calendar
calendar = value;
InitializeOverridableProperties(_cultureData, calendar.ID);
// We succeeded, return
return;
}
}
// The assigned calendar is not a valid calendar for this culture, throw
throw new ArgumentOutOfRangeException(nameof(value), value, SR.Argument_InvalidCalendar);
}
}
private CalendarId[] OptionalCalendars => optionalCalendars ??= _cultureData.CalendarIds;
/// <summary>
/// Get the era value by parsing the name of the era.
/// </summary>
public int GetEra(string eraName)
{
ArgumentNullException.ThrowIfNull(eraName);
// The Era Name and Abbreviated Era Name
// for Taiwan Calendar on non-Taiwan SKU returns empty string (which
// would be matched below) but we don't want the empty string to give
// us an Era number
// confer 85900 DTFI.GetEra("") should fail on all cultures
if (eraName.Length == 0)
{
return -1;
}
// The following is based on the assumption that the era value is starting from 1, and has a
// serial values.
// If that ever changes, the code has to be changed.
// The calls to String.Compare should use the current culture for the string comparisons, but the
// invariant culture when comparing against the english names.
for (int i = 0; i < EraNames.Length; i++)
{
// Compare the era name in a case-insensitive way for the appropriate culture.
if (m_eraNames![i].Length > 0)
{
if (Culture.CompareInfo.Compare(eraName, m_eraNames[i], CompareOptions.IgnoreCase) == 0)
{
return i + 1;
}
}
}
for (int i = 0; i < AbbreviatedEraNames.Length; i++)
{
// Compare the abbreviated era name in a case-insensitive way for the appropriate culture.
if (Culture.CompareInfo.Compare(eraName, m_abbrevEraNames![i], CompareOptions.IgnoreCase) == 0)
{
return i + 1;
}
}
for (int i = 0; i < AbbreviatedEnglishEraNames.Length; i++)
{
// this comparison should use the InvariantCulture. The English name could have linguistically
// interesting characters.
if (CompareInfo.Invariant.Compare(eraName, m_abbrevEnglishEraNames![i], CompareOptions.IgnoreCase) == 0)
{
return i + 1;
}
}
return -1;
}
internal string[] EraNames => m_eraNames ??= _cultureData.EraNames(Calendar.ID);
/// <summary>
/// Get the name of the era for the specified era value.
/// Era names are 1 indexed
/// </summary>
public string GetEraName(int era)
{
if (era == Calendar.CurrentEra)
{
era = Calendar.CurrentEraValue;
}
// The following is based on the assumption that the era value is starting from 1, and has a
// serial values.
// If that ever changes, the code has to be changed.
if ((--era) < EraNames.Length && (era >= 0))
{
return m_eraNames![era];
}
throw new ArgumentOutOfRangeException(nameof(era), era, SR.ArgumentOutOfRange_InvalidEraValue);
}
internal string[] AbbreviatedEraNames => m_abbrevEraNames ??= _cultureData.AbbrevEraNames(Calendar.ID);
/// <remarks>
/// Era names are 1 indexed
/// </remarks>
public string GetAbbreviatedEraName(int era)
{
if (AbbreviatedEraNames.Length == 0)
{
// If abbreviation era name is not used in this culture,
// return the full era name.
return GetEraName(era);
}
if (era == Calendar.CurrentEra)
{
era = Calendar.CurrentEraValue;
}
if ((--era) < m_abbrevEraNames!.Length && (era >= 0))
{
return m_abbrevEraNames[era];
}
throw new ArgumentOutOfRangeException(nameof(era), era, SR.ArgumentOutOfRange_InvalidEraValue);
}
internal string[] AbbreviatedEnglishEraNames
{
get
{
if (m_abbrevEnglishEraNames == null)
{
Debug.Assert(Calendar.ID > 0, "[DateTimeFormatInfo.AbbreviatedEnglishEraNames] Expected Calendar.ID > 0");
m_abbrevEnglishEraNames = _cultureData.AbbreviatedEnglishEraNames(Calendar.ID);
}
return m_abbrevEnglishEraNames;
}
}
/// <remarks>
/// Note that cultureData derives this from the short date format (unless someone's set this previously)
/// Note that this property is quite undesirable.
/// </remarks>
public string DateSeparator
{
get
{
dateSeparator ??= _cultureData.DateSeparator(Calendar.ID);
Debug.Assert(dateSeparator != null, "DateTimeFormatInfo.DateSeparator, dateSeparator != null");
return dateSeparator;
}
set
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
ArgumentNullException.ThrowIfNull(value);
ClearTokenHashTable();
dateSeparator = value;
}
}
public DayOfWeek FirstDayOfWeek
{
get
{
if (firstDayOfWeek == -1)
{
firstDayOfWeek = _cultureData.FirstDayOfWeek;
}
Debug.Assert(firstDayOfWeek != -1, "DateTimeFormatInfo.FirstDayOfWeek, firstDayOfWeek != -1");
return (DayOfWeek)firstDayOfWeek;
}
set
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
if (value < DayOfWeek.Sunday || value > DayOfWeek.Saturday)
{
throw new ArgumentOutOfRangeException(
nameof(value),
value,
SR.Format(SR.ArgumentOutOfRange_Range, DayOfWeek.Sunday, DayOfWeek.Saturday));
}
firstDayOfWeek = (int)value;
}
}
public CalendarWeekRule CalendarWeekRule
{
get
{
if (calendarWeekRule == -1)
{
calendarWeekRule = _cultureData.CalendarWeekRule;
}
Debug.Assert(calendarWeekRule != -1, "DateTimeFormatInfo.CalendarWeekRule, calendarWeekRule != -1");
return (CalendarWeekRule)calendarWeekRule;
}
set
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
if (value < CalendarWeekRule.FirstDay || value > CalendarWeekRule.FirstFourDayWeek)
{
throw new ArgumentOutOfRangeException(
nameof(value),
value,
SR.Format(SR.ArgumentOutOfRange_Range, CalendarWeekRule.FirstDay, CalendarWeekRule.FirstFourDayWeek));
}
calendarWeekRule = (int)value;
}
}
public string FullDateTimePattern
{
get => fullDateTimePattern ??= LongDatePattern + " " + LongTimePattern;
set
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
ArgumentNullException.ThrowIfNull(value);
fullDateTimePattern = value;
}
}
/// <summary>
/// For our "patterns" arrays we have 2 variables, a string and a string[]
/// The string[] contains the list of patterns, EXCEPT the default may not be included.
/// The string contains the default pattern.
/// When we initially construct our string[], we set the string to string[0]
/// </summary>
public string LongDatePattern
{
get => longDatePattern ??= UnclonedLongDatePatterns[0]; // initialize from the 1st array value if not set
set
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
ArgumentNullException.ThrowIfNull(value);
// Remember the new string
longDatePattern = value;
OnLongDatePatternChanged();
}
}
private void OnLongDatePatternChanged()
{
// Clear the token hash table
ClearTokenHashTable();
// Clean up cached values that will be affected by this property.
fullDateTimePattern = null;
}
/// <summary>
/// For our "patterns" arrays we have 2 variables, a string and a string[]
///
/// The string[] contains the list of patterns, EXCEPT the default may not be included.
/// The string contains the default pattern.
/// When we initially construct our string[], we set the string to string[0]
/// </summary>
public string LongTimePattern
{
get => longTimePattern ??= UnclonedLongTimePatterns[0]; // initialize from the 1st array value if not set
set
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
ArgumentNullException.ThrowIfNull(value);
// Remember the new string
longTimePattern = value;
OnLongTimePatternChanged();
}
}
private void OnLongTimePatternChanged()
{
// Clear the token hash table
ClearTokenHashTable();
// Clean up cached values that will be affected by this property.
fullDateTimePattern = null; // Full date = long date + long Time
generalLongTimePattern = null; // General long date = short date + long Time
dateTimeOffsetPattern = null;
}
/// <remarks>
/// Just to be confusing there's only 1 month day pattern, not a whole list
/// </remarks>
public string MonthDayPattern
{
get
{
if (monthDayPattern == null)
{
Debug.Assert(Calendar.ID > 0, "[DateTimeFormatInfo.MonthDayPattern] Expected calID > 0");
monthDayPattern = _cultureData.MonthDay(Calendar.ID);
}
Debug.Assert(monthDayPattern != null, "DateTimeFormatInfo.MonthDayPattern, monthDayPattern != null");
return monthDayPattern;
}
set
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
ArgumentNullException.ThrowIfNull(value);
monthDayPattern = value;
}
}
public string PMDesignator
{
get
{
pmDesignator ??= _cultureData.PMDesignator;
Debug.Assert(pmDesignator != null, "DateTimeFormatInfo.PMDesignator, pmDesignator != null");
return pmDesignator;
}
set
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
ArgumentNullException.ThrowIfNull(value);
ClearTokenHashTable();
pmDesignator = value;
}
}
public string RFC1123Pattern => rfc1123Pattern;
/// <summary>
/// For our "patterns" arrays we have 2 variables, a string and a string[]
///
/// The string[] contains the list of patterns, EXCEPT the default may not be included.
/// The string contains the default pattern.
/// When we initially construct our string[], we set the string to string[0]
/// </summary>
public string ShortDatePattern
{
get
{
return shortDatePattern ??= UnclonedShortDatePatterns[0]; // initialize from the 1st array value if not set
}
set
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
ArgumentNullException.ThrowIfNull(value);
// Remember the new string
shortDatePattern = value;
OnShortDatePatternChanged();
}
}
private void OnShortDatePatternChanged()
{
// Clear the token hash table, note that even short dates could require this
ClearTokenHashTable();
// Clean up cached values that will be affected by this property.
generalLongTimePattern = null; // General long time = short date + long time
generalShortTimePattern = null; // General short time = short date + short Time
dateTimeOffsetPattern = null;
}
/// <summary>
/// For our "patterns" arrays we have 2 variables, a string and a string[]
///
/// The string[] contains the list of patterns, EXCEPT the default may not be included.
/// The string contains the default pattern.
/// When we initially construct our string[], we set the string to string[0]
/// </summary>
public string ShortTimePattern
{
get => shortTimePattern ??= UnclonedShortTimePatterns[0]; // initialize from the 1st array value if not set
set
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
ArgumentNullException.ThrowIfNull(value);
// Remember the new string
shortTimePattern = value;
OnShortTimePatternChanged();
}
}
private void OnShortTimePatternChanged()
{
// Clear the token hash table, note that even short times could require this
ClearTokenHashTable();
// Clean up cached values that will be affected by this property.
generalShortTimePattern = null; // General short date = short date + short time.
}
public string SortableDateTimePattern => sortableDateTimePattern;
/// <summary>
/// Return the pattern for 'g' general format: shortDate + short time
/// This is used by DateTimeFormat.cs to get the pattern for 'g'.
/// We put this internal property here so that we can avoid doing the
/// concatation every time somebody asks for the general format.
/// </summary>
internal string GeneralShortTimePattern => generalShortTimePattern ??= ShortDatePattern + " " + ShortTimePattern;
/// <summary>
/// Return the pattern for 'g' general format: shortDate + Long time.
/// We put this internal property here so that we can avoid doing the
/// concatation every time somebody asks for the general format.
/// </summary>
internal string GeneralLongTimePattern => generalLongTimePattern ??= ShortDatePattern + " " + LongTimePattern;
/// Return the default pattern DateTimeOffset : shortDate + long time + time zone offset.
/// This is used by DateTimeFormat.cs to get the pattern for short Date + long time + time zone offset
/// We put this internal property here so that we can avoid doing the
/// concatation every time somebody uses this form.
internal string DateTimeOffsetPattern
{
get
{
if (dateTimeOffsetPattern == null)
{
/* LongTimePattern might contain a "z" as part of the format string in which case we don't want to append a time zone offset */
bool foundZ = false;
bool inQuote = false;
char quote = '\'';
for (int i = 0; !foundZ && i < LongTimePattern.Length; i++)
{
switch (LongTimePattern[i])
{
case 'z':
/* if we aren't in a quote, we've found a z */
foundZ = !inQuote;
/* we'll fall out of the loop now because the test includes !foundZ */
break;
case '\'':
case '\"':
if (inQuote && (quote == LongTimePattern[i]))
{
/* we were in a quote and found a matching exit quote, so we are outside a quote now */
inQuote = false;
}
else if (!inQuote)
{
quote = LongTimePattern[i];
inQuote = true;
}
else
{
/* we were in a quote and saw the other type of quote character, so we are still in a quote */
}
break;
case '%':
case '\\':
i++; /* skip next character that is escaped by this backslash */
break;
default:
break;
}
}
dateTimeOffsetPattern = foundZ ?
ShortDatePattern + " " + LongTimePattern :
ShortDatePattern + " " + LongTimePattern + " zzz";
}
return dateTimeOffsetPattern;
}
}
/// <remarks>
/// Note that cultureData derives this from the long time format (unless someone's set this previously)
/// Note that this property is quite undesirable.
/// </remarks>
public string TimeSeparator
{
get
{
timeSeparator ??= _cultureData.TimeSeparator;
Debug.Assert(timeSeparator != null, "DateTimeFormatInfo.TimeSeparator, timeSeparator != null");
return timeSeparator;
}
set
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
ArgumentNullException.ThrowIfNull(value);
ClearTokenHashTable();
timeSeparator = value;
}
}
public string UniversalSortableDateTimePattern => universalSortableDateTimePattern;
/// <summary>
/// For our "patterns" arrays we have 2 variables, a string and a string[]
///
/// The string[] contains the list of patterns, EXCEPT the default may not be included.
/// The string contains the default pattern.
/// When we initially construct our string[], we set the string to string[0]
/// </summary>
public string YearMonthPattern
{
get => yearMonthPattern ??= UnclonedYearMonthPatterns[0]; // initialize from the 1st array value if not set
set
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
ArgumentNullException.ThrowIfNull(value);
// Remember the new string
yearMonthPattern = value;
OnYearMonthPatternChanged();
}
}
private void OnYearMonthPatternChanged()
{