Strange TimeSpan.ToString output
Asked Answered
B

3

5

I have a question regarding the symbol that separates days from hours in TimeSpan.ToString output.

The standard TimeSpan format strings produce different separator symbols:

  • "c" produces a period (".") character
  • "g" and "G" produce a colon (":") character

Example:

// 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)

Does anybody know what's the logic behind it?

However TimeSpan.Parse parses all of these string successfully.

Beatify answered 15/1, 2016 at 13:12 Comment(1)
Sometimes you say TimeSpan and sometimes you say DateTime, is this by mistake?Immutable
L
6

These characters are hardcoded for those formats.

For "c" standard format

[-][d.]hh:mm:ss[.fffffff]

For "g" standard format

[-][d:]h:mm:ss[.FFFFFFF]

And for "G" Format Specifier

[-]d:hh:mm:ss.fffffff

Also doc says;

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.

Also in 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.

Sounds like the most important reason is consistency between all .NET Framework versions. Maybe that's why they call this format as constant :)

Laywoman answered 15/1, 2016 at 13:18 Comment(2)
Thanks for the answer!Beatify
@Beatify Glad to help.Beverage
A
2

There is more detail on MSDN - Standard TimeSpan Format Strings.

Essentially:

"c" is the Constant format: This specifier is not culture-sensitive. Format is [d’.’]hh’:’mm’:’ss[‘.’fffffff]

"g" is the General Short format: This is culture sensitive. Format is [-][d’:’]h’:’mm’:’ss[.FFFFFFF]

"G" is the General Long format: This is culture sensitive. Format is [-]d’:’hh’:’mm’:’ss.fffffff.

Ailing answered 15/1, 2016 at 13:19 Comment(1)
I've already read the MSDN documentation. My question was what's the logic behind it...Beatify
A
0

Look at the 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]

Actually answered 15/1, 2016 at 13:19 Comment(1)
I've already read the MSDN documentation. My question was what's the logic behind it...Beatify

© 2022 - 2024 — McMap. All rights reserved.