其他分享
首页 > 其他分享> > 为什么GetAllDateTimePatterns方法不返回唯一格式?

为什么GetAllDateTimePatterns方法不返回唯一格式?

作者:互联网

我真的很惊讶DateTimeFormatInfo.GetAllDateTimePatterns method没有返回唯一的标准格式.

让我举一个InvariantCulture为例;

var pattern = CultureInfo.InvariantCulture.DateTimeFormat
                          GetAllDateTimePatterns();
foreach (var format in pattern)
{
    Console.WriteLine(format);
}

在.NET Framework 4.5中,此代码将打印34个标准模式.

当我以这种方式使用Enumerable.Distinct method时;

var pattern = CultureInfo.InvariantCulture.DateTimeFormat.
                          GetAllDateTimePatterns().
                          Distinct();
foreach (var format in pattern)
{
     Console.WriteLine(format);
}

该代码将在相同条件下打印29个标准图案.

您甚至可以在调试时看到这些重复的模式.

为什么此方法多次返回相同的模式?

解决方法:

主要原因是:因为某些standard date and time format strings具有多个格式说明符.如;

> "M", "m"
> "O", "o"
> "R", "r"
> "Y", "y"

让我们看一下GetAllDateTimePatterns方法implemented

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();
}

此方法使用DateTimeFormat.allStandardFormats数组的所有返回值调用GetAllDateTimePatterns(char) method.和这个数组implemented as;

internal static char[] allStandardFormats = 
{
    'd', 'D', 'f', 'F', 'g', 'G', 
    'm', 'M', 'o', 'O', 'r', 'R', 
    's', 't', 'T', 'u', 'U', 'y', 'Y',
};

这就是为什么此方法和重载将为m和M生成相同的格式,并将它们添加到作为此方法来源的结果数组中的原因.

除此之外,("F") Format Specifier("U") format specifier是等效的.这些也生成与GetAllDateTimePatterns(char) method’s impelementation处理的格式相同的格式.

public  String[] GetAllDateTimePatterns(char format)
{
    String [] result = null;

    switch (format)
    {
        case 'd':
            result = this.AllShortDatePatterns;
            break;
        case 'D':
            result = this.AllLongDatePatterns;
            break;
        case 'f':
            result = GetCombinedPatterns(AllLongDatePatterns, AllShortTimePatterns, " ");
            break;
        case 'F':
        case 'U':
            result = GetCombinedPatterns(AllLongDatePatterns, AllLongTimePatterns, " ");
            break;
        case 'g':
            result = GetCombinedPatterns(AllShortDatePatterns, AllShortTimePatterns, " ");
            break;
        case 'G':
            result = GetCombinedPatterns(AllShortDatePatterns, AllLongTimePatterns, " ");
            break;
        case 'm':
        case 'M':
            result = new String[] {MonthDayPattern};
            break;
        case 'o':
        case 'O':
            result = new String[] {DateTimeFormat.RoundtripFormat};
            break;
        case 'r':
        case 'R':
            result = new String[] {rfc1123Pattern};
            break;
        case 's':
            result = new String[] {sortableDateTimePattern};
            break;
        case 't':
            result = this.AllShortTimePatterns;
            break;
        case 'T':
            result = this.AllLongTimePatterns;
            break;
        case 'u':
            result = new String[] {UniversalSortableDateTimePattern};
            break;
        case 'y':
        case 'Y':
            result = this.AllYearMonthPatterns;
            break;
        default:
            throw new ArgumentException(Environment.GetResourceString("Format_BadFormatSpecifier"), "format");
    }
    return (result);
}

标签:datetime-format,datetime,c,net
来源: https://codeday.me/bug/20191121/2053683.html