TimeSpan.Parse(String)使用什么文化?

时间:2014-10-03 00:33:38

标签: c# .net cultureinfo timespan

TimeSpan.Parse(String) http://msdn.microsoft.com/en-us/library/se73z7b9(v=vs.110).aspx的doco没有明确说明使用的文化。

1 个答案:

答案 0 :(得分:2)

简短回答 - System.Threading.Thread.CurrentThread.CurrentCulture;

答案很长 - Source会说实话:

public static TimeSpan Parse(String s) {
    /* Constructs a TimeSpan from a string.  Leading and trailing white space characters are allowed. */
    return TimeSpanParse.Parse(s, null);
}

并将代码深入到代码中:

public static DateTimeFormatInfo GetInstance(IFormatProvider provider) {

    // Fast case for a regular CultureInfo
    DateTimeFormatInfo info;
    CultureInfo cultureProvider = provider as CultureInfo;
    if (cultureProvider != null && !cultureProvider.m_isInherited)
    {
        return cultureProvider.DateTimeFormat;
    }

    // Fast case for a DTFI;
    info = provider as DateTimeFormatInfo;
    if (info != null) {
        return info;
    }

    // Wasn't cultureInfo or DTFI, do it the slower way
    if (provider != null) {
        info = provider.GetFormat(typeof(DateTimeFormatInfo)) as DateTimeFormatInfo;
        if (info != null) {
            return info;
        }
    }

    // Couldn't get anything, just use currentInfo as fallback
    return CurrentInfo;
 }

CurrentInfo定义如下:

public static DateTimeFormatInfo CurrentInfo {
    get {
        Contract.Ensures(Contract.Result<DateTimeFormatInfo>() != null);
        System.Globalization.CultureInfo culture = System.Threading.Thread.CurrentThread.CurrentCulture;
        if (!culture.m_isInherited) {
            DateTimeFormatInfo info = culture.dateTimeInfo;
            if (info != null) {
                return info;
            }
        }
        return (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo));
    }
}