How to deal with Rounding-off TimeSpan?
Asked Answered
B

3

7

I take the difference between two DateTime fields, and store it in a TimeSpan variable, Now I have to round-off the TimeSpan by the following rules:

if the minutes in TimeSpan is less than 30 then Minutes and Seconds must be set to zero,
if the minutes in TimeSpan is equal to or greater than 30 then hours must be incremented by 1 and Minutes and Seconds must be set to zero.

TimeSpan can also be a negative value, so in that case I need to preserve the sign..

I could be able to achieve the requirement if the TimeSpan wasn't a negative value, though I have written a code I am not happy with its inefficiency as it is more bulky ..

Please suggest me a simpler and efficient method.

Thanks regards,

This is my code which works fine, when TimeSpan is not negative value ..

TimeSpan time_span = endTime.Subtract(startTime);
            TimeSpan time_span1;
            if (time_span.Minutes >= 30)
            {
                time_span1 = new TimeSpan(time_span.Hours + 1, 0, 0);
            }
            else
            {
                time_span1 = new TimeSpan(time_span.Hours, 0, 0);
            }

time_span1 will contain the result ..

Bridoon answered 26/4, 2010 at 14:26 Comment(2)
Show us the code you have, pleaseCorelation
Why don't you share your code as a base to start from?Beautifully
O
12

How about:

public static TimeSpan Round(TimeSpan input)
{
    if (input < TimeSpan.Zero)
    {
        return -Round(-input);
    }
    int hours = (int) input.TotalHours;
    if (input.Minutes >= 30)
    {
        hours++;
    }
    return TimeSpan.FromHours(hours);
}
Obau answered 26/4, 2010 at 14:30 Comment(4)
@jon, It is not supported with .NET V2.0, it pop ups error when I try to trigger the transformation, how to fix the error??Bridoon
@infant programmer: "it pops up error" doesn't give me much indication of what's wrong. What error are you getting?Obau
@jon, ya, I am sorry for that .. actually I couldn't explain the error in my words, along with the general errors there are some business related things too, I have copied the error into a text file, please find it here :: sites.google.com/site/aravindspattar/NewTextDocument.txtBridoon
@infant: It looks like it's got more to do with the fact that you're using this from XSLT than anything else - I don't think it's got anything to do with the actual code. Change it to just "return input" and I suspect you'll see the same behaviour.Obau
A
4

You can use

double v = span.TotalHours;     
v = Math.Round(v, MidpointRounding.AwayFromZero);
span = TimeSpan.FromHours(v);

It depends on whether I understood your rules for negative values correctly.

Aspectual answered 26/4, 2010 at 14:31 Comment(2)
@Jon, yes, I was just looking that up.Aspectual
+1 This is the most straightforward way to do it IMO, and it even can even be reduced to a nice one liner.Griffie
B
3

TimeSpan is immutable, so you have to create a new one. This is also a perfect case for using extension methods in C#:

public static class TimeSpanUtility
{
   public static TimeSpan Round( this TimeSpan ts )
   {
       var sign = ts < TimeSpan.Zero ? -1 : 1;
       var roundBy = Math.Abs(ts.Minutes) >= 30 ? 1 : 0;
       return TimeSpan.FromHours( ts.TotalHours + (sign * roundBy) );
   }
}

// usage would be:
var someTimeSpan = new TimeSpan( 2, 45, 15 );
var roundedTime = someTimeSpan.Round();
Baxie answered 26/4, 2010 at 14:31 Comment(2)
You want TotalHours, otherwise 26 hours will end up becoming 2 hours.Obau
thanks for the response .. your code works fine too :) I would like to appreciate the logic that you have used :)Bridoon

© 2022 - 2024 — McMap. All rights reserved.