奇怪的 TimeSpan.ToString 输出
Strange TimeSpan.ToString output
我对 TimeSpan.ToString
输出中分隔天数和小时数的符号有疑问。
标准 TimeSpan
格式字符串产生不同的分隔符:
- "c" 产生句点 (.) 字符
- "g" 和 "G" 产生冒号(“:”)字符
示例:
// Constant format
Console.WriteLine(TimeSpan.FromDays(42).ToString("c", CultureInfo.InvariantCulture));
// Output: 42.00:00:00 (period character between days and hours)
// General short format
Console.WriteLine(TimeSpan.FromDays(42).ToString("g", CultureInfo.InvariantCulture));
// Output: 42:0:00:00 (colon character between days and hours)
// General long format
Console.WriteLine(TimeSpan.FromDays(42).ToString("G", CultureInfo.InvariantCulture));
// Output: 42:00:00:00.0000000 (colon character between days and hours)
有人知道背后的逻辑是什么吗?
但是 TimeSpan.Parse
成功解析了所有这些字符串。
这些字符针对这些格式进行了硬编码。
[-][d.]hh:mm:ss[.fffffff]
[-][d:]h:mm:ss[.FFFFFFF]
[-]d:hh:mm:ss.fffffff
医生也说;
Unlike the "g" and "G" format specifiers, the "c" format specifier is
not culture-sensitive. It produces the string representation of a
TimeSpan value that is invariant and that is common to all previous
versions of the .NET Framework before the .NET Framework 4. "c" is the
default TimeSpan format string; the TimeSpan.ToString() method formats
a time interval value by using the "c" format string.
也在Custom TimeSpan Format Strings
The .NET Framework does not define a grammar for separators in time
intervals. This means that the separators between days and hours,
hours and minutes, minutes and seconds, and seconds and fractions of a
second must all be treated as character literals in a format string.
听起来最重要的原因是所有 .NET Framework 版本之间的一致性。也许这就是为什么他们将这种格式称为 constant :)
看看MSDN
The "g" TimeSpan format specifier returns the string representation of
a TimeSpan value in a compact form by including only the elements that
are necessary.
[-][d:]h:mm:ss[.FFFFFFF]
.....................
The "c" format specifier returns the string representation of a
TimeSpan value in the following form:
[-][d.]hh:mm:ss[.fffffff]
MSDN - Standard TimeSpan Format Strings 上有更多详细信息。
本质上:
“c”是 Constant format:此说明符不是 culture-sensitive。格式为 [d'.']hh':'mm':'ss['.'fffffff]
“g”是 General Short format:这是文化敏感的。格式为 [-][d':']h':'mm':'ss[.FFFFFFF]
“G”是 General Long format:这是文化敏感的。格式为[-]d’:’hh’:’mm’:’ss.fffffff.
我对 TimeSpan.ToString
输出中分隔天数和小时数的符号有疑问。
标准 TimeSpan
格式字符串产生不同的分隔符:
- "c" 产生句点 (.) 字符
- "g" 和 "G" 产生冒号(“:”)字符
示例:
// Constant format
Console.WriteLine(TimeSpan.FromDays(42).ToString("c", CultureInfo.InvariantCulture));
// Output: 42.00:00:00 (period character between days and hours)
// General short format
Console.WriteLine(TimeSpan.FromDays(42).ToString("g", CultureInfo.InvariantCulture));
// Output: 42:0:00:00 (colon character between days and hours)
// General long format
Console.WriteLine(TimeSpan.FromDays(42).ToString("G", CultureInfo.InvariantCulture));
// Output: 42:00:00:00.0000000 (colon character between days and hours)
有人知道背后的逻辑是什么吗?
但是 TimeSpan.Parse
成功解析了所有这些字符串。
这些字符针对这些格式进行了硬编码。
[-][d.]hh:mm:ss[.fffffff]
[-][d:]h:mm:ss[.FFFFFFF]
[-]d:hh:mm:ss.fffffff
医生也说;
Unlike the "g" and "G" format specifiers, the "c" format specifier is not culture-sensitive. It produces the string representation of a TimeSpan value that is invariant and that is common to all previous versions of the .NET Framework before the .NET Framework 4. "c" is the default TimeSpan format string; the TimeSpan.ToString() method formats a time interval value by using the "c" format string.
也在Custom TimeSpan Format Strings
The .NET Framework does not define a grammar for separators in time intervals. This means that the separators between days and hours, hours and minutes, minutes and seconds, and seconds and fractions of a second must all be treated as character literals in a format string.
听起来最重要的原因是所有 .NET Framework 版本之间的一致性。也许这就是为什么他们将这种格式称为 constant :)
看看MSDN
The "g" TimeSpan format specifier returns the string representation of a TimeSpan value in a compact form by including only the elements that are necessary.
[-][d:]h:mm:ss[.FFFFFFF]
.....................
The "c" format specifier returns the string representation of a TimeSpan value in the following form:
[-][d.]hh:mm:ss[.fffffff]
MSDN - Standard TimeSpan Format Strings 上有更多详细信息。
本质上:
“c”是 Constant format:此说明符不是 culture-sensitive。格式为 [d'.']hh':'mm':'ss['.'fffffff]
“g”是 General Short format:这是文化敏感的。格式为 [-][d':']h':'mm':'ss[.FFFFFFF]
“G”是 General Long format:这是文化敏感的。格式为[-]d’:’hh’:’mm’:’ss.fffffff.