0

I would like to use next code:

long long DateTimeToTimeT(System::DateTime dt)
{
    System::DateTime epoch(1970, 1, 1, 0, 0, 0, 0);
    long long totalSeconds = (dt - epoch).TotalSeconds;

    return totalSeconds >= 0 ? totalSeconds : 0;
}

So question is: Is it exception safe or I should handle some errors here?

I mean: Is is safe to convert from double (which is TotalSeconds) to long long in such case?

user2706838
  • 1,061
  • 2
  • 13
  • 21

1 Answers1

0

Yes, it is safe: the subtraction of two dates never throws (http://msdn.microsoft.com/en-us/library/1905yhe2(v=vs.110).aspx) since TimeSpan covers a much larger interval than valid dates.

Loghorn
  • 2,729
  • 17
  • 22
  • Actually, when I was posting this question - I thought about problems with double - long long convert issues. – user2706838 Mar 03 '14 at 11:00
  • the internal representation of TimeSpan is Int64, so there is no conversion to or from double, but from Int64 to long long, that is the same type. – Loghorn Mar 03 '14 at 11:03