Parsing TimeSpan from string including format
Asked Answered
H

3

7

I'm sure this must be simple, but I can't figure out how to word it correctly in Google...

I have a config which has a field:

TimeToPoll="1d"

Now I want to do something like:

TimeSpan.Parse(TimeToPoll);

Returning a timespan of one day.

In C#

EDIT: I'm looking for a method which allows parse of "1d" as well as "1s" or "1y" etc. Is this possible?

Meaning:

     "1d" parses to {1.00:00:00}
     "1h" parses to {0.01:00:00}
     "1m" parses to {0.00:01:00}
     "1s" parses to {0.00:00:01}
Hamlen answered 18/4, 2012 at 10:33 Comment(4)
My apologies, I have updated.Hamlen
check this link : msdn.microsoft.com/en-us/library/system.timespan.days.aspxRunesmith
Just translate the string in the setting to a format string that TimeSpan.TryParse() accepts. Or change the setting string itself. Watch out of .NET dependency, TryParse() is only available in .NET 4.Gyration
Okay thanks. I may figure out a work around for this. As it would make the config much simpler to implement.Hamlen
C
0

The d is not needed and is the reason your parse fails.

var oneDay = TimeSpan.Parse("1");

Update:

For what you are looking to do, there is no built in support. You would need to write your own parser.

Cornea answered 18/4, 2012 at 11:14 Comment(3)
This would need - TimeSpan.FromDays(double.Parse("1"));Hamlen
@MatthewCanty - I don't follow. What I have written is valid.Cornea
Ah, so it does. Sorry. I just didn't think something so ambiguous could happen. I assumed it would have returned the smallest possible value.Hamlen
H
6

This is my resolution:

    public static TimeSpan ConvertToTimeSpan(this string timeSpan)
    {
        var l = timeSpan.Length - 1;
        var value = timeSpan.Substring(0, l);
        var type = timeSpan.Substring(l, 1);

        switch (type)
        {
            case "d": return TimeSpan.FromDays(double.Parse(value));
            case "h": return TimeSpan.FromHours(double.Parse(value));
            case "m": return TimeSpan.FromMinutes(double.Parse(value));
            case "s": return TimeSpan.FromSeconds(double.Parse(value));
            case "f": return TimeSpan.FromMilliseconds(double.Parse(value));
            case "z": return TimeSpan.FromTicks(long.Parse(value));
            default: return TimeSpan.FromDays(double.Parse(value));
        }
    }
Hamlen answered 19/4, 2012 at 12:26 Comment(0)
O
1

You should store your values in your config file in one of the formats that TimeSpan.Parse can work with. There are several samples on that page.

EDIT: The examples are in the code toward the bottom of the page.

Orva answered 18/4, 2012 at 11:19 Comment(0)
C
0

The d is not needed and is the reason your parse fails.

var oneDay = TimeSpan.Parse("1");

Update:

For what you are looking to do, there is no built in support. You would need to write your own parser.

Cornea answered 18/4, 2012 at 11:14 Comment(3)
This would need - TimeSpan.FromDays(double.Parse("1"));Hamlen
@MatthewCanty - I don't follow. What I have written is valid.Cornea
Ah, so it does. Sorry. I just didn't think something so ambiguous could happen. I assumed it would have returned the smallest possible value.Hamlen

© 2022 - 2024 — McMap. All rights reserved.