Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better time parser #168

Open
jbatte47 opened this issue Nov 5, 2014 · 1 comment
Open

Better time parser #168

jbatte47 opened this issue Nov 5, 2014 · 1 comment

Comments

@jbatte47
Copy link
Member

jbatte47 commented Nov 5, 2014

Currently, TimeSpan.Parse freaks out if you don't use fractional values (1.3:00:00 is valid, but 27:00:00 is not). This is not intuitive and can lead to disappointing results if you don't remember the "rules". We need something better!

ITimeParser parser = GetParser(); //likely provided via DI
TimeSpan span = parser.Parse("27:00:00");
Console.WriteLine(span); // 1.03:00:00
@prakash-patel
Copy link

I am not sure how much correct i am on above solution. Correct me if i am wrong on this solution. I try this in LINQPad.

void Main()
{
    String timeStamp = "27:00:00";
    ITimeParser parser = GetParser();
    TimeSpan span = parser.Parse(timeStamp);
    Console.WriteLine(span);
}

    Parser GetParser(){
        return new Parser();
    }   

public class Parser : ITimeParser
{
    public TimeSpan Parse(string timeStamp)
    {
        var segments = timeStamp.Split(':');
        TimeSpan time = new TimeSpan(0, Convert.ToInt32(segments[0]), Convert.ToInt32(segments[1]), Convert.ToInt32(segments[2]));
        return time;
    }
}

public interface ITimeParser
{
    TimeSpan Parse(string timeStamp);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants