diff options
Diffstat (limited to 'src/mscorlib/corefx/System/Globalization/DateTimeFormatInfo.cs')
-rw-r--r-- | src/mscorlib/corefx/System/Globalization/DateTimeFormatInfo.cs | 343 |
1 files changed, 237 insertions, 106 deletions
diff --git a/src/mscorlib/corefx/System/Globalization/DateTimeFormatInfo.cs b/src/mscorlib/corefx/System/Globalization/DateTimeFormatInfo.cs index da746ada88..216fc603d0 100644 --- a/src/mscorlib/corefx/System/Globalization/DateTimeFormatInfo.cs +++ b/src/mscorlib/corefx/System/Globalization/DateTimeFormatInfo.cs @@ -2,15 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; -using System.Collections; using System.Collections.Generic; +using System.Diagnostics; using System.Diagnostics.Contracts; -using System.Runtime.InteropServices; using System.Runtime.Serialization; -using System.Security; -using System.Text; -using System.Threading; namespace System.Globalization { @@ -55,8 +50,7 @@ namespace System.Globalization [Serializable] - [System.Runtime.InteropServices.ComVisible(true)] - public sealed partial class DateTimeFormatInfo : IFormatProvider, ICloneable + public sealed class DateTimeFormatInfo : IFormatProvider, ICloneable { // cache for the invariant culture. // invariantInfo is constant irrespective of your current culture. @@ -228,7 +222,7 @@ namespace System.Globalization { // Get the abbreviated day names for our current calendar this.abbreviatedDayNames = _cultureData.AbbreviatedDayNames(Calendar.ID); - Contract.Assert(this.abbreviatedDayNames.Length == 7, "[DateTimeFormatInfo.GetAbbreviatedDayOfWeekNames] Expected 7 day names in a week"); + Debug.Assert(this.abbreviatedDayNames.Length == 7, "[DateTimeFormatInfo.GetAbbreviatedDayOfWeekNames] Expected 7 day names in a week"); } return (this.abbreviatedDayNames); } @@ -252,7 +246,7 @@ namespace System.Globalization { // Get the super short day names for our current calendar this.m_superShortDayNames = _cultureData.SuperShortDayNames(Calendar.ID); - Contract.Assert(this.m_superShortDayNames.Length == 7, "[DateTimeFormatInfo.internalGetSuperShortDayNames] Expected 7 day names in a week"); + Debug.Assert(this.m_superShortDayNames.Length == 7, "[DateTimeFormatInfo.internalGetSuperShortDayNames] Expected 7 day names in a week"); } return (this.m_superShortDayNames); } @@ -269,7 +263,7 @@ namespace System.Globalization { // Get the day names for our current calendar this.dayNames = _cultureData.DayNames(Calendar.ID); - Contract.Assert(this.dayNames.Length == 7, "[DateTimeFormatInfo.GetDayOfWeekNames] Expected 7 day names in a week"); + Debug.Assert(this.dayNames.Length == 7, "[DateTimeFormatInfo.GetDayOfWeekNames] Expected 7 day names in a week"); } return (this.dayNames); } @@ -286,7 +280,7 @@ namespace System.Globalization { // Get the month names for our current calendar this.abbreviatedMonthNames = _cultureData.AbbreviatedMonthNames(Calendar.ID); - Contract.Assert(this.abbreviatedMonthNames.Length == 12 || this.abbreviatedMonthNames.Length == 13, + Debug.Assert(this.abbreviatedMonthNames.Length == 12 || this.abbreviatedMonthNames.Length == 13, "[DateTimeFormatInfo.GetAbbreviatedMonthNames] Expected 12 or 13 month names in a year"); } return (this.abbreviatedMonthNames); @@ -305,7 +299,7 @@ namespace System.Globalization { // Get the month names for our current calendar this.monthNames = _cultureData.MonthNames(Calendar.ID); - Contract.Assert(this.monthNames.Length == 12 || this.monthNames.Length == 13, + Debug.Assert(this.monthNames.Length == 12 || this.monthNames.Length == 13, "[DateTimeFormatInfo.GetMonthNames] Expected 12 or 13 month names in a year"); } @@ -324,8 +318,8 @@ namespace System.Globalization internal DateTimeFormatInfo(CultureData cultureData, Calendar cal) { - Contract.Requires(cultureData != null); - Contract.Requires(cal != null); + Debug.Assert(cultureData != null); + Debug.Assert(cal != null); // Remember our culture _cultureData = cultureData; @@ -335,8 +329,8 @@ namespace System.Globalization private void InitializeOverridableProperties(CultureData cultureData, CalendarId calendarId) { - Contract.Requires(cultureData != null); - Contract.Assert(calendarId != CalendarId.UNINITIALIZED_VALUE, "[DateTimeFormatInfo.Populate] Expected initalized calendarId"); + Debug.Assert(cultureData != null); + Debug.Assert(calendarId != CalendarId.UNINITIALIZED_VALUE, "[DateTimeFormatInfo.Populate] Expected initalized calendarId"); if (this.firstDayOfWeek == -1) { this.firstDayOfWeek = cultureData.IFIRSTDAYOFWEEK; } if (this.calendarWeekRule == -1) { this.calendarWeekRule = cultureData.IFIRSTWEEKOFYEAR; } @@ -347,19 +341,19 @@ namespace System.Globalization if (this.dateSeparator == null) { this.dateSeparator = cultureData.DateSeparator(calendarId); } this.allLongTimePatterns = _cultureData.LongTimes; - Contract.Assert(this.allLongTimePatterns.Length > 0, "[DateTimeFormatInfo.Populate] Expected some long time patterns"); + Debug.Assert(this.allLongTimePatterns.Length > 0, "[DateTimeFormatInfo.Populate] Expected some long time patterns"); this.allShortTimePatterns = _cultureData.ShortTimes; - Contract.Assert(this.allShortTimePatterns.Length > 0, "[DateTimeFormatInfo.Populate] Expected some short time patterns"); + Debug.Assert(this.allShortTimePatterns.Length > 0, "[DateTimeFormatInfo.Populate] Expected some short time patterns"); this.allLongDatePatterns = cultureData.LongDates(calendarId); - Contract.Assert(this.allLongDatePatterns.Length > 0, "[DateTimeFormatInfo.Populate] Expected some long date patterns"); + Debug.Assert(this.allLongDatePatterns.Length > 0, "[DateTimeFormatInfo.Populate] Expected some long date patterns"); this.allShortDatePatterns = cultureData.ShortDates(calendarId); - Contract.Assert(this.allShortDatePatterns.Length > 0, "[DateTimeFormatInfo.Populate] Expected some short date patterns"); + Debug.Assert(this.allShortDatePatterns.Length > 0, "[DateTimeFormatInfo.Populate] Expected some short date patterns"); this.allYearMonthPatterns = cultureData.YearMonths(calendarId); - Contract.Assert(this.allYearMonthPatterns.Length > 0, "[DateTimeFormatInfo.Populate] Expected some year month patterns"); + Debug.Assert(this.allYearMonthPatterns.Length > 0, "[DateTimeFormatInfo.Populate] Expected some year month patterns"); } [OptionalField(VersionAdded = 1)] @@ -527,7 +521,7 @@ namespace System.Globalization { this.amDesignator = _cultureData.SAM1159; } - Contract.Assert(this.amDesignator != null, "DateTimeFormatInfo.AMDesignator, amDesignator != null"); + Debug.Assert(this.amDesignator != null, "DateTimeFormatInfo.AMDesignator, amDesignator != null"); return (this.amDesignator); } @@ -537,7 +531,7 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_String); } Contract.EndContractBlock(); @@ -553,7 +547,7 @@ namespace System.Globalization { Contract.Ensures(Contract.Result<Calendar>() != null); - Contract.Assert(this.calendar != null, "DateTimeFormatInfo.Calendar: calendar != null"); + Debug.Assert(this.calendar != null, "DateTimeFormatInfo.Calendar: calendar != null"); return (this.calendar); } @@ -563,7 +557,7 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", SR.ArgumentNull_Obj); + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_Obj); } Contract.EndContractBlock(); if (value == calendar) @@ -641,7 +635,7 @@ namespace System.Globalization } // The assigned calendar is not a valid calendar for this culture, throw - throw new ArgumentOutOfRangeException("value", SR.Argument_InvalidCalendar); + throw new ArgumentOutOfRangeException(nameof(value), SR.Argument_InvalidCalendar); } } @@ -670,7 +664,7 @@ namespace System.Globalization { if (eraName == null) { - throw new ArgumentNullException("eraName", + throw new ArgumentNullException(nameof(eraName), SR.ArgumentNull_String); } Contract.EndContractBlock(); @@ -759,7 +753,7 @@ namespace System.Globalization { return (m_eraNames[era]); } - throw new ArgumentOutOfRangeException("era", SR.ArgumentOutOfRange_InvalidEraValue); + throw new ArgumentOutOfRangeException(nameof(era), SR.ArgumentOutOfRange_InvalidEraValue); } internal String[] AbbreviatedEraNames @@ -791,7 +785,7 @@ namespace System.Globalization { return (m_abbrevEraNames[era]); } - throw new ArgumentOutOfRangeException("era", SR.ArgumentOutOfRange_InvalidEraValue); + throw new ArgumentOutOfRangeException(nameof(era), SR.ArgumentOutOfRange_InvalidEraValue); } internal String[] AbbreviatedEnglishEraNames @@ -800,34 +794,41 @@ namespace System.Globalization { if (this.m_abbrevEnglishEraNames == null) { - Contract.Assert(Calendar.ID > 0, "[DateTimeFormatInfo.AbbreviatedEnglishEraNames] Expected Calendar.ID > 0"); + Debug.Assert(Calendar.ID > 0, "[DateTimeFormatInfo.AbbreviatedEnglishEraNames] Expected Calendar.ID > 0"); this.m_abbrevEnglishEraNames = _cultureData.AbbreviatedEnglishEraNames(Calendar.ID); } return (this.m_abbrevEnglishEraNames); } } - // Note that cultureData derives this from the short date format (unless someone's set this previously) // Note that this property is quite undesirable. - internal String DateSeparator + public string DateSeparator { get { - if (this.dateSeparator == null) + if (dateSeparator == null) { - this.dateSeparator = _cultureData.DateSeparator(Calendar.ID); + dateSeparator = _cultureData.DateSeparator(Calendar.ID); } - Contract.Assert(this.dateSeparator != null, "DateTimeFormatInfo.DateSeparator, dateSeparator != null"); - return (this.dateSeparator); + Debug.Assert(this.dateSeparator != null, "DateTimeFormatInfo.DateSeparator, dateSeparator != null"); + return dateSeparator; } set { - throw null; + if (IsReadOnly) + throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); + + if (value == null) + { + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_String); + } + Contract.EndContractBlock(); + ClearTokenHashTable(); + dateSeparator = value; } } - public DayOfWeek FirstDayOfWeek { get @@ -836,7 +837,7 @@ namespace System.Globalization { this.firstDayOfWeek = _cultureData.IFIRSTDAYOFWEEK; } - Contract.Assert(this.firstDayOfWeek != -1, "DateTimeFormatInfo.FirstDayOfWeek, firstDayOfWeek != -1"); + Debug.Assert(this.firstDayOfWeek != -1, "DateTimeFormatInfo.FirstDayOfWeek, firstDayOfWeek != -1"); return ((DayOfWeek)this.firstDayOfWeek); } @@ -852,7 +853,7 @@ namespace System.Globalization else { throw new ArgumentOutOfRangeException( - "value", SR.Format(SR.ArgumentOutOfRange_Range, + nameof(value), SR.Format(SR.ArgumentOutOfRange_Range, DayOfWeek.Sunday, DayOfWeek.Saturday)); } } @@ -866,7 +867,7 @@ namespace System.Globalization { this.calendarWeekRule = _cultureData.IFIRSTWEEKOFYEAR; } - Contract.Assert(this.calendarWeekRule != -1, "DateTimeFormatInfo.CalendarWeekRule, calendarWeekRule != -1"); + Debug.Assert(this.calendarWeekRule != -1, "DateTimeFormatInfo.CalendarWeekRule, calendarWeekRule != -1"); return ((CalendarWeekRule)this.calendarWeekRule); } @@ -881,7 +882,7 @@ namespace System.Globalization else { throw new ArgumentOutOfRangeException( - "value", SR.Format(SR.ArgumentOutOfRange_Range, + nameof(value), SR.Format(SR.ArgumentOutOfRange_Range, CalendarWeekRule.FirstDay, CalendarWeekRule.FirstFourDayWeek)); } } @@ -904,7 +905,7 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_String); } Contract.EndContractBlock(); @@ -938,7 +939,7 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_String); } Contract.EndContractBlock(); @@ -979,7 +980,7 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_String); } Contract.EndContractBlock(); @@ -1005,10 +1006,10 @@ namespace System.Globalization { if (this.monthDayPattern == null) { - Contract.Assert(Calendar.ID > 0, "[DateTimeFormatInfo.MonthDayPattern] Expected calID > 0"); + Debug.Assert(Calendar.ID > 0, "[DateTimeFormatInfo.MonthDayPattern] Expected calID > 0"); this.monthDayPattern = _cultureData.MonthDay(Calendar.ID); } - Contract.Assert(this.monthDayPattern != null, "DateTimeFormatInfo.MonthDayPattern, monthDayPattern != null"); + Debug.Assert(this.monthDayPattern != null, "DateTimeFormatInfo.MonthDayPattern, monthDayPattern != null"); return (this.monthDayPattern); } @@ -1018,7 +1019,7 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_String); } Contract.EndContractBlock(); @@ -1037,7 +1038,7 @@ namespace System.Globalization { this.pmDesignator = _cultureData.SPM2359; } - Contract.Assert(this.pmDesignator != null, "DateTimeFormatInfo.PMDesignator, pmDesignator != null"); + Debug.Assert(this.pmDesignator != null, "DateTimeFormatInfo.PMDesignator, pmDesignator != null"); return (this.pmDesignator); } @@ -1047,7 +1048,7 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_String); } Contract.EndContractBlock(); @@ -1090,7 +1091,7 @@ namespace System.Globalization if (IsReadOnly) throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_String); Contract.EndContractBlock(); @@ -1132,7 +1133,7 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_String); } Contract.EndContractBlock(); @@ -1270,7 +1271,7 @@ namespace System.Globalization // Note that cultureData derives this from the long time format (unless someone's set this previously) // Note that this property is quite undesirable. - internal String TimeSeparator + public string TimeSeparator { get { @@ -1278,17 +1279,27 @@ namespace System.Globalization { timeSeparator = _cultureData.TimeSeparator; } - Contract.Assert(this.timeSeparator != null, "DateTimeFormatInfo.TimeSeparator, timeSeparator != null"); + Debug.Assert(this.timeSeparator != null, "DateTimeFormatInfo.TimeSeparator, timeSeparator != null"); return (timeSeparator); } set { - throw null; + if (IsReadOnly) + throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); + + if (value == null) + { + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_String); + } + + Contract.EndContractBlock(); + ClearTokenHashTable(); + + timeSeparator = value; } } - public String UniversalSortableDateTimePattern { get @@ -1321,7 +1332,7 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_String); } Contract.EndContractBlock(); @@ -1339,8 +1350,8 @@ namespace System.Globalization // private static void CheckNullValue(String[] values, int length) { - Contract.Requires(values != null, "value != null"); - Contract.Requires(values.Length >= length); + Debug.Assert(values != null, "value != null"); + Debug.Assert(values.Length >= length); for (int i = 0; i < length; i++) { if (values[i] == null) @@ -1365,12 +1376,12 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_Array); } if (value.Length != 7) { - throw new ArgumentException(SR.Format(SR.Argument_InvalidArrayLength, 7), "value"); + throw new ArgumentException(SR.Format(SR.Argument_InvalidArrayLength, 7), nameof(value)); } Contract.EndContractBlock(); CheckNullValue(value, value.Length); @@ -1381,7 +1392,6 @@ namespace System.Globalization } // Returns the string array of the one-letter day of week names. - [System.Runtime.InteropServices.ComVisible(false)] public String[] ShortestDayNames { get @@ -1395,12 +1405,12 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_Array); } if (value.Length != 7) { - throw new ArgumentException(SR.Format(SR.Argument_InvalidArrayLength, 7), "value"); + throw new ArgumentException(SR.Format(SR.Argument_InvalidArrayLength, 7), nameof(value)); } Contract.EndContractBlock(); CheckNullValue(value, value.Length); @@ -1422,12 +1432,12 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_Array); } if (value.Length != 7) { - throw new ArgumentException(SR.Format(SR.Argument_InvalidArrayLength, 7), "value"); + throw new ArgumentException(SR.Format(SR.Argument_InvalidArrayLength, 7), nameof(value)); } Contract.EndContractBlock(); CheckNullValue(value, value.Length); @@ -1451,12 +1461,12 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_Array); } if (value.Length != 13) { - throw new ArgumentException(SR.Format(SR.Argument_InvalidArrayLength, 13), "value"); + throw new ArgumentException(SR.Format(SR.Argument_InvalidArrayLength, 13), nameof(value)); } Contract.EndContractBlock(); CheckNullValue(value, value.Length - 1); @@ -1479,12 +1489,12 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_Array); } if (value.Length != 13) { - throw new ArgumentException(SR.Format(SR.Argument_InvalidArrayLength, 13), "value"); + throw new ArgumentException(SR.Format(SR.Argument_InvalidArrayLength, 13), nameof(value)); } Contract.EndContractBlock(); CheckNullValue(value, value.Length - 1); @@ -1550,7 +1560,7 @@ namespace System.Globalization if ((month < 1) || (month > monthNamesArray.Length)) { throw new ArgumentOutOfRangeException( - "month", SR.Format(SR.ArgumentOutOfRange_Range, + nameof(month), SR.Format(SR.ArgumentOutOfRange_Range, 1, monthNamesArray.Length)); } return (monthNamesArray[month - 1]); @@ -1571,7 +1581,7 @@ namespace System.Globalization if (this.m_genitiveAbbreviatedMonthNames == null) { this.m_genitiveAbbreviatedMonthNames = _cultureData.AbbreviatedGenitiveMonthNames(this.Calendar.ID); - Contract.Assert(this.m_genitiveAbbreviatedMonthNames.Length == 13, + Debug.Assert(this.m_genitiveAbbreviatedMonthNames.Length == 13, "[DateTimeFormatInfo.GetGenitiveMonthNames] Expected 13 abbreviated genitive month names in a year"); } return (this.m_genitiveAbbreviatedMonthNames); @@ -1580,7 +1590,7 @@ namespace System.Globalization if (this.genitiveMonthNames == null) { this.genitiveMonthNames = _cultureData.GenitiveMonthNames(this.Calendar.ID); - Contract.Assert(this.genitiveMonthNames.Length == 13, + Debug.Assert(this.genitiveMonthNames.Length == 13, "[DateTimeFormatInfo.GetGenitiveMonthNames] Expected 13 genitive month names in a year"); } return (this.genitiveMonthNames); @@ -1597,9 +1607,9 @@ namespace System.Globalization { if (this.leapYearMonthNames == null) { - Contract.Assert(Calendar.ID > 0, "[DateTimeFormatInfo.internalGetLeapYearMonthNames] Expected Calendar.ID > 0"); + Debug.Assert(Calendar.ID > 0, "[DateTimeFormatInfo.internalGetLeapYearMonthNames] Expected Calendar.ID > 0"); this.leapYearMonthNames = _cultureData.LeapYearMonthNames(Calendar.ID); - Contract.Assert(this.leapYearMonthNames.Length == 13, + Debug.Assert(this.leapYearMonthNames.Length == 13, "[DateTimeFormatInfo.internalGetLeapYearMonthNames] Expepcted 13 leap year month names"); } return (leapYearMonthNames); @@ -1611,7 +1621,7 @@ namespace System.Globalization if ((int)dayofweek < 0 || (int)dayofweek > 6) { throw new ArgumentOutOfRangeException( - "dayofweek", SR.Format(SR.ArgumentOutOfRange_Range, + nameof(dayofweek), SR.Format(SR.ArgumentOutOfRange_Range, DayOfWeek.Sunday, DayOfWeek.Saturday)); } Contract.EndContractBlock(); @@ -1622,11 +1632,28 @@ namespace System.Globalization return (internalGetAbbreviatedDayOfWeekNames()[(int)dayofweek]); } + // Returns the super short day of week names for the specified day of week. + public string GetShortestDayName(DayOfWeek dayOfWeek) + { + if ((int)dayOfWeek < 0 || (int)dayOfWeek > 6) + { + throw new ArgumentOutOfRangeException( + nameof(dayOfWeek), SR.Format(SR.ArgumentOutOfRange_Range, + DayOfWeek.Sunday, DayOfWeek.Saturday)); + } + Contract.EndContractBlock(); + // + // Don't call the public property SuperShortDayNames here since a clone is needed in that + // property, so it will be slower. Instead, use internalGetSuperShortDayNames() directly. + // + return (internalGetSuperShortDayNames()[(int)dayOfWeek]); + } + // Get all possible combination of inputs private static String[] GetCombinedPatterns(String[] patterns1, String[] patterns2, String connectString) { - Contract.Requires(patterns1 != null); - Contract.Requires(patterns2 != null); + Debug.Assert(patterns1 != null); + Debug.Assert(patterns2 != null); // Get array size String[] result = new String[patterns1.Length * patterns2.Length]; @@ -1646,9 +1673,22 @@ namespace System.Globalization return (result); } + public string[] GetAllDateTimePatterns() + { + List<String> results = new List<String>(DEFAULT_ALL_DATETIMES_SIZE); + + for (int i = 0; i < DateTimeFormat.allStandardFormats.Length; i++) + { + String[] strings = GetAllDateTimePatterns(DateTimeFormat.allStandardFormats[i]); + for (int j = 0; j < strings.Length; j++) + { + results.Add(strings[j]); + } + } + return results.ToArray(); + } - // auto-generated - internal String[] GetAllDateTimePatterns(char format) + public string[] GetAllDateTimePatterns(char format) { Contract.Ensures(Contract.Result<String[]>() != null); String[] result = null; @@ -1703,7 +1743,7 @@ namespace System.Globalization result = this.AllYearMonthPatterns; break; default: - throw new ArgumentException(SR.Format_BadFormatSpecifier, "format"); + throw new ArgumentException(SR.Format_BadFormatSpecifier, nameof(format)); } return (result); } @@ -1714,7 +1754,7 @@ namespace System.Globalization if ((int)dayofweek < 0 || (int)dayofweek > 6) { throw new ArgumentOutOfRangeException( - "dayofweek", SR.Format(SR.ArgumentOutOfRange_Range, + nameof(dayofweek), SR.Format(SR.ArgumentOutOfRange_Range, DayOfWeek.Sunday, DayOfWeek.Saturday)); } Contract.EndContractBlock(); @@ -1730,7 +1770,7 @@ namespace System.Globalization if (month < 1 || month > 13) { throw new ArgumentOutOfRangeException( - "month", SR.Format(SR.ArgumentOutOfRange_Range, + nameof(month), SR.Format(SR.ArgumentOutOfRange_Range, 1, 13)); } Contract.EndContractBlock(); @@ -1744,7 +1784,7 @@ namespace System.Globalization if (month < 1 || month > 13) { throw new ArgumentOutOfRangeException( - "month", SR.Format(SR.ArgumentOutOfRange_Range, + nameof(month), SR.Format(SR.ArgumentOutOfRange_Range, 1, 13)); } Contract.EndContractBlock(); @@ -1761,9 +1801,9 @@ namespace System.Globalization // The resulting [] can get returned to the calling app, so clone it. private static string[] GetMergedPatterns(string[] patterns, string defaultPattern) { - Contract.Assert(patterns != null && patterns.Length > 0, + Debug.Assert(patterns != null && patterns.Length > 0, "[DateTimeFormatInfo.GetMergedPatterns]Expected array of at least one pattern"); - Contract.Assert(defaultPattern != null, + Debug.Assert(defaultPattern != null, "[DateTimeFormatInfo.GetMergedPatterns]Expected non null default string"); // If the default happens to be the first in the list just return (a cloned) copy @@ -1864,9 +1904,9 @@ namespace System.Globalization { if (this.allYearMonthPatterns == null) { - Contract.Assert(Calendar.ID > 0, "[DateTimeFormatInfo.UnclonedYearMonthPatterns] Expected Calendar.ID > 0"); + Debug.Assert(Calendar.ID > 0, "[DateTimeFormatInfo.UnclonedYearMonthPatterns] Expected Calendar.ID > 0"); this.allYearMonthPatterns = _cultureData.YearMonths(this.Calendar.ID); - Contract.Assert(this.allYearMonthPatterns.Length > 0, + Debug.Assert(this.allYearMonthPatterns.Length > 0, "[DateTimeFormatInfo.UnclonedYearMonthPatterns] Expected some year month patterns"); } @@ -1883,9 +1923,9 @@ namespace System.Globalization { if (allShortDatePatterns == null) { - Contract.Assert(Calendar.ID > 0, "[DateTimeFormatInfo.UnclonedShortDatePatterns] Expected Calendar.ID > 0"); + Debug.Assert(Calendar.ID > 0, "[DateTimeFormatInfo.UnclonedShortDatePatterns] Expected Calendar.ID > 0"); this.allShortDatePatterns = _cultureData.ShortDates(this.Calendar.ID); - Contract.Assert(this.allShortDatePatterns.Length > 0, + Debug.Assert(this.allShortDatePatterns.Length > 0, "[DateTimeFormatInfo.UnclonedShortDatePatterns] Expected some short date patterns"); } @@ -1901,9 +1941,9 @@ namespace System.Globalization { if (allLongDatePatterns == null) { - Contract.Assert(Calendar.ID > 0, "[DateTimeFormatInfo.UnclonedLongDatePatterns] Expected Calendar.ID > 0"); + Debug.Assert(Calendar.ID > 0, "[DateTimeFormatInfo.UnclonedLongDatePatterns] Expected Calendar.ID > 0"); this.allLongDatePatterns = _cultureData.LongDates(this.Calendar.ID); - Contract.Assert(this.allLongDatePatterns.Length > 0, + Debug.Assert(this.allLongDatePatterns.Length > 0, "[DateTimeFormatInfo.UnclonedLongDatePatterns] Expected some long date patterns"); } @@ -1920,7 +1960,7 @@ namespace System.Globalization if (this.allShortTimePatterns == null) { this.allShortTimePatterns = _cultureData.ShortTimes; - Contract.Assert(this.allShortTimePatterns.Length > 0, + Debug.Assert(this.allShortTimePatterns.Length > 0, "[DateTimeFormatInfo.UnclonedShortTimePatterns] Expected some short time patterns"); } @@ -1937,7 +1977,7 @@ namespace System.Globalization if (this.allLongTimePatterns == null) { this.allLongTimePatterns = _cultureData.LongTimes; - Contract.Assert(this.allLongTimePatterns.Length > 0, + Debug.Assert(this.allLongTimePatterns.Length > 0, "[DateTimeFormatInfo.UnclonedLongTimePatterns] Expected some long time patterns"); } @@ -1949,7 +1989,7 @@ namespace System.Globalization { if (dtfi == null) { - throw new ArgumentNullException("dtfi", + throw new ArgumentNullException(nameof(dtfi), SR.ArgumentNull_Obj); } Contract.EndContractBlock(); @@ -1973,7 +2013,99 @@ namespace System.Globalization } } - [System.Runtime.InteropServices.ComVisible(false)] + // Return the native name for the calendar in DTFI.Calendar. The native name is referred to + // the culture used to create the DTFI. E.g. in the following example, the native language is Japanese. + // DateTimeFormatInfo dtfi = new CultureInfo("ja-JP", false).DateTimeFormat.Calendar = new JapaneseCalendar(); + // String nativeName = dtfi.NativeCalendarName; // Get the Japanese name for the Japanese calendar. + // DateTimeFormatInfo dtfi = new CultureInfo("ja-JP", false).DateTimeFormat.Calendar = new GregorianCalendar(GregorianCalendarTypes.Localized); + // String nativeName = dtfi.NativeCalendarName; // Get the Japanese name for the Gregorian calendar. + public string NativeCalendarName + { + get + { + return _cultureData.CalendarName(Calendar.ID); + } + } + + // + // Used by custom cultures and others to set the list of available formats. Note that none of them are + // explicitly used unless someone calls GetAllDateTimePatterns and subsequently uses one of the items + // from the list. + // + // Most of the format characters that can be used in GetAllDateTimePatterns are + // not really needed since they are one of the following: + // + // r/R/s/u locale-independent constants -- cannot be changed! + // m/M/y/Y fields with a single string in them -- that can be set through props directly + // f/F/g/G/U derived fields based on combinations of various of the below formats + // + // NOTE: No special validation is done here beyond what is done when the actual respective fields + // are used (what would be the point of disallowing here what we allow in the appropriate property?) + // + // WARNING: If more validation is ever done in one place, it should be done in the other. + // + public void SetAllDateTimePatterns(String[] patterns, char format) + { + if (IsReadOnly) + throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); + + if (patterns == null) + { + throw new ArgumentNullException(nameof(patterns), SR.ArgumentNull_Array); + } + + if (patterns.Length == 0) + { + throw new ArgumentException(SR.Arg_ArrayZeroError, nameof(patterns)); + } + + Contract.EndContractBlock(); + + for (int i=0; i<patterns.Length; i++) + { + if (patterns[i] == null) + { + throw new ArgumentNullException("patterns[" + i + "]", SR.ArgumentNull_ArrayValue); + } + } + + // Remember the patterns, and use the 1st as default + switch (format) + { + case 'd': + allShortDatePatterns = patterns; + shortDatePattern = allShortDatePatterns[0]; + break; + + case 'D': + allLongDatePatterns = patterns; + longDatePattern = allLongDatePatterns[0]; + break; + + case 't': + allShortTimePatterns = patterns; + shortTimePattern = allShortTimePatterns[0]; + break; + + case 'T': + allLongTimePatterns = patterns; + longTimePattern = allLongTimePatterns[0]; + break; + + case 'y': + case 'Y': + allYearMonthPatterns = patterns; + yearMonthPattern = allYearMonthPatterns[0]; + break; + + default: + throw new ArgumentException(SR.Format_BadFormatSpecifier, nameof(format)); + } + + // Clear the token hash table, note that even short dates could require this + ClearTokenHashTable(); + } + public String[] AbbreviatedMonthGenitiveNames { get @@ -1987,12 +2119,12 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_Array); } if (value.Length != 13) { - throw new ArgumentException(SR.Format(SR.Argument_InvalidArrayLength, 13), "value"); + throw new ArgumentException(SR.Format(SR.Argument_InvalidArrayLength, 13), nameof(value)); } Contract.EndContractBlock(); CheckNullValue(value, value.Length - 1); @@ -2001,7 +2133,6 @@ namespace System.Globalization } } - [System.Runtime.InteropServices.ComVisible(false)] public String[] MonthGenitiveNames { get @@ -2015,12 +2146,12 @@ namespace System.Globalization throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) { - throw new ArgumentNullException("value", + throw new ArgumentNullException(nameof(value), SR.ArgumentNull_Array); } if (value.Length != 13) { - throw new ArgumentException(SR.Format(SR.Argument_InvalidArrayLength, 13), "value"); + throw new ArgumentException(SR.Format(SR.Argument_InvalidArrayLength, 13), nameof(value)); } Contract.EndContractBlock(); CheckNullValue(value, value.Length - 1); @@ -2630,7 +2761,7 @@ namespace System.Globalization } while (i < str.Value.Length && (state != HebrewNumberParsingState.FoundEndOfHebrewNumber)); // When we are here, we are either at the end of the string, or we find a valid Hebrew number. - Contract.Assert(state == HebrewNumberParsingState.ContinueParsing || state == HebrewNumberParsingState.FoundEndOfHebrewNumber, + Debug.Assert(state == HebrewNumberParsingState.ContinueParsing || state == HebrewNumberParsingState.FoundEndOfHebrewNumber, "Invalid returned state from HebrewNumber.ParseByChar()"); if (state != HebrewNumberParsingState.FoundEndOfHebrewNumber) @@ -2665,7 +2796,7 @@ namespace System.Globalization tokenValue = 0; TokenHashValue value; - Contract.Assert(str.Index < str.Value.Length, "DateTimeFormatInfo.Tokenize(): start < value.Length"); + Debug.Assert(str.Index < str.Value.Length, "DateTimeFormatInfo.Tokenize(): start < value.Length"); char ch = str.m_current; bool isLetter = Char.IsLetter(ch); @@ -2788,7 +2919,7 @@ namespace System.Globalization } previousNode = temp; }; - Contract.Assert(false, "The hashtable is full. This should not happen."); + Debug.Assert(false, "The hashtable is full. This should not happen."); } private void InsertHash(TokenHashValue[] hashTable, String str, TokenType tokenType, int tokenValue) @@ -2876,7 +3007,7 @@ namespace System.Globalization hashcode += hashProbe; if (hashcode >= TOKEN_HASH_SIZE) hashcode -= TOKEN_HASH_SIZE; } while (i < TOKEN_HASH_SIZE); - Contract.Assert(false, "The hashtable is full. This should not happen."); + Debug.Assert(false, "The hashtable is full. This should not happen."); } private bool CompareStringIgnoreCaseOptimized(string string1, int offset1, int length1, string string2, int offset2, int length2) |