4

Is there a difference in the returned values between Timespan(0,0,secs) and Timespan.FromSeconds(secs)?

It seems to me the difference is that FromSeconds accepts a double.

Yariv
  • 12,945
  • 19
  • 54
  • 75
  • 4
    Yes, the first one better be a value between 0 and 59. Just try it, you don't need our help. – Hans Passant Feb 03 '14 at 11:50
  • 3
    @HansPassant: I've just tried it with a value of 120 and it was fine. What makes you think it needs to be between 0 and 59? – Jon Skeet Feb 03 '14 at 11:51
  • That it works doesn't have much to do with the response you'll get from the guy that is going to maintain your code some day. – Hans Passant Feb 03 '14 at 11:52
  • Allowing the number of seconds to be greater that 59 in the constructor seems like a serious flaw in the design of the API. – Sean Feb 03 '14 at 11:54
  • 3
    @HansPassant: I disagree. It's entirely feasible that you may have a number of hours, minutes and seconds which aren't meant to be within a particular range. I also suggest that your comment is at least misleading, as it suggests that the call *requires* the values to be in that range. I'd personally use `TimeSpan.FromSeconds` here, but the correct answer is "No, there isn't a different in the returned values." – Jon Skeet Feb 03 '14 at 11:54
  • 1
    @Sean: I disagree... and aside from anything else, you'd have to consider negative values. – Jon Skeet Feb 03 '14 at 11:55
  • 3
    That's a valid point about negative time so a range of -59 to +59, but I think you're wrong to suggest it's not bad API design. Allowing something like Timespan(23,67,120) is a terrible idea. – Sean Feb 03 '14 at 11:58
  • @Sean: I think it depends on exactly what you want the API to represent. It becomes even odder when you have mixed positive and negative values. Ultimately these are three fungible aspects of the same value, via different units: a number of ticks. It feels reasonable to me that `new TimeSpan(hours, minutes, seconds) == TimeSpan.FromHours(hours) + TimeSpan.FromMinutes(minute) + TimeSpan.FromSeconds(seconds)`. – Jon Skeet Feb 03 '14 at 12:41

3 Answers3

9

Ultimately no, under the hood, TimeSpan deals with ticks.

Personally I would prefer to use TimeSpan.FromSeconds as it is completely clear what the intent is.

Trevor Pilley
  • 16,156
  • 5
  • 44
  • 60
3

The parameter being a double in the second case is an important difference indeed: in some cases it can lead to an OverflowException. Quoting the documentation below.

TimeSpan Constructor (Int32, Int32, Int32):

The specified hours, minutes, and seconds are converted to ticks, and that value initializes this instance.

TimeSpan.FromSeconds Method:

The value parameter is converted to milliseconds, which is converted to ticks, and that number of ticks is used to intialize the new TimeSpan. Therefore, value will only be considered accurate to the nearest millisecond. Note that, because of the loss of precision of the Double data type, this can generate an OverflowException for values that are near but still in the range of either MinValue or MaxValue, This is the cause of an OverflowException, for example, in the following attempt to instantiate a TimeSpan object.

// The following throws an OverflowException at runtime
TimeSpan maxSpan = TimeSpan.FromSeconds(TimeSpan.MaxValue.TotalSeconds);
Community
  • 1
  • 1
user247702
  • 23,641
  • 15
  • 110
  • 157
  • 2
    It can't lead to an `OverflowException` for any value that would actually be representable via an `int` though. So for any call where the two are viable alternatives, they'll give the same result. – Jon Skeet Feb 03 '14 at 12:42
2

You could test it easily:

int secs = 10;
var ts = new TimeSpan(0, 0, secs);
var ts2 = TimeSpan.FromSeconds(secs);
if(ts == ts2)
{
    Console.WriteLine("Equal");
}
else
{
   Console.WriteLine("Not Equal");
}

Output is: Equal

Even though i find the TimeSpan.FromSeconds method more readable, hence less error-prone, than the constructor.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939