Timespan data type C #

I have a timepan value which is the difference between two Datetime values. It goes something like this:

myvalue = {41870.01:44:22.8365404} 

      

I also have a string that represents a different time interval value and it looks like this:

ttime= "23:55" // which means 23 minutes and 55 seconds

      

I want to check if myvalue is less than ttime. Here's my attempt:

if(myvalue < Timespan.Parse(ttime))
//do this
else
//do that

      

Is my approach correct? Do I need to do something? Thank.

+3


source to share


1 answer


The only problem I see is that it is TimeSpan.Parse

limited to 24 hours. Thus, you will need this workaround:

string ttime = "48:23:55"; // 48 hours
TimeSpan ts = new TimeSpan(int.Parse(ttime.Split(':')[0]),    // hours
                           int.Parse(ttime.Split(':')[1]),    // minutes
                           int.Parse(ttime.Split(':')[2]));   // seconds

      

Also, it's perfectly okay to compare twice this way.

If it can contain two or three parts (with or without a clock), it should be safer:

string[] tokens = ttime.Split(':');
// support only two or three tokens:
if (tokens.Length < 2 || tokens.Length > 3)
    throw new NotSupportedException("Timespan could not be parsed successfully: " + ttime);
TimeSpan ts;
if(tokens.Length == 3)
    ts = new TimeSpan(int.Parse(tokens[0]), int.Parse(tokens[1]),int.Parse(tokens[2]));
else
    ts = new TimeSpan(0, int.Parse(tokens[0]), int.Parse(tokens[1]));

      




For what it's worth, here's a method that is written from scratch that parses the string to TimeSpan

, allowing two or three tokens (hh, mm, ss or mm, ss) and also supports weird formats like 100:100:100

(100 hours + 100 minutes + 100 seconds):

public static TimeSpan SaferTimeSpanParse(string input, char delimiter = ':')
{
    if (string.IsNullOrEmpty(input))
        throw new ArgumentNullException("input must not be null or empty", "input");
    string[] tokens = input.Split(delimiter);
    if (tokens.Length < 2 || tokens.Length > 3)
        throw new NotSupportedException("Timespan could not be parsed successfully: " + input);
    int i;
    if (!tokens.All(t => int.TryParse(t, out i) && i >= 0))
        throw new ArgumentException("All token must contain a positive integer", "input");

    int[] all = tokens.Select(t => int.Parse(t)).ToArray();
    int hoursFinal = 0, minutesFinal = 0, secondsFinal = 0;

    int seconds = all.Last();
    secondsFinal = seconds % 60;
    minutesFinal = seconds / 60;

    int minutes = (all.Length == 3 ? all[1] : all[0]) + minutesFinal; // add current minutes which might already be increased through seconds
    minutesFinal = minutes % 60;
    hoursFinal = minutes / 60;

    hoursFinal = all.Length == 3 ? all[0] + hoursFinal : hoursFinal; // add current hours which might already be increased through minutes

    return new TimeSpan(hoursFinal, minutesFinal, secondsFinal);
}

      

checked your line, my line and the strange one:

Console.WriteLine(SaferTimeSpanParse("23:55"));
Console.WriteLine(SaferTimeSpanParse("48:23:55"));
Console.WriteLine(SaferTimeSpanParse("100:100:100"));

      

Output:

00:23:55
2.00:23:55
4.05:41:40

      

+5


source







All Articles