Parsing TimeSpan with optional minus sign
Asked Answered
S

2

6

MSDN says:

The styles parameter affects the interpretation of strings parsed using custom format strings. It determines whether input is interpreted as a negative time interval only if a negative sign is present (TimeSpanStyles.None), or whether it is always interpreted as a negative time interval (TimeSpanStyles.AssumeNegative). If TimeSpanStyles.AssumeNegative is not used, format must include a literal negative sign symbol (such as "-") to successfully parse a negative time interval.

I have try the following:

TimeSpan.ParseExact("-0700", @"\-hhmm", null, TimeSpanStyles.None)

However it returns 07:00:00. And fails for "0700".

If I try:

TimeSpan.ParseExact("-0700", "hhmm", null, TimeSpanStyles.None)

It fails too.

TimeSpan.ParseExact("0700", new string [] { "hhmm", @"\-hhmm" }, null, TimeSpanStyles.None)

Does not fail for both "0700" and "-0700", but always return the positive 07:00:00.

How is it supposed to be used?

Shela answered 15/1, 2015 at 14:22 Comment(3)
As a workaround, if you can't get this to work, you could parse a Duration in Noda Time. Will see what I can do with TimeSpan though...Geraldine
Thanks for hint. But it is a simple script I do not want to and an additional library, I would rather use a StartsWith('-') instead.Shela
Fair enough. I've included it in my answer anyway, but mostly for the sake of other readers.Geraldine
G
4

It looks like this isn't supported. From the custom TimeSpan format strings page:

Custom TimeSpan format specifiers also do not include a sign symbol that enables you to differentiate between negative and positive time intervals. To include a sign symbol, you have to construct a format string by using conditional logic. The Other Characters section includes an example.

This does seem really odd though. Ick.

As mentioned in my comment, you could use my Noda Time project's Duration parsing for this; overkill for just this case, but if you had other date/time work in the project, it could be useful.

For example:

var pattern = DurationPattern.CreateWithInvariantCulture("-hhmm");
var timeSpan = pattern.Parse("-0700").Value.ToTimeSpan();
Geraldine answered 15/1, 2015 at 14:30 Comment(3)
So, the styles parameter is useless. One could just append - sign in front?Shela
@TN.: Somewhat useless, yes. The whole feature seems a little underdesigned, to be honest... (I mean, the style has an effect, but not a terribly useful one - at least for custom patterns. Maybe it's better for standard patterns...)Geraldine
@TN.: Assuming you meant 0700, yes. It requires that the hour value isn't more than 23 though. 7000 is harder to handle. (If you actually want to handle that, let me know, but I suspect you meant 0700 :)Geraldine
S
3

It does look like you annoyingly need to check yourself if it begins with a leading -

// tsPos = 07:00:00
string pos = "0700";
TimeSpan tsPos = TimeSpan.ParseExact(pos, new string [] { "hhmm", @"\-hhmm" }, null,
    pos[0] == '-' ? TimeSpanStyles.AssumeNegative : TimeSpanStyles.None);

// tsNeg = -07:00:00            
string neg = "-0700";
TimeSpan tsNeg = TimeSpan.ParseExact(neg, new string [] { "hhmm", @"\-hhmm" }, null,
    neg[0] == '-' ? TimeSpanStyles.AssumeNegative : TimeSpanStyles.None);
Slyviasm answered 15/1, 2015 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.