views:

305

answers:

2

I have just gone through an unexpectedly convoluted process to define datetimes.

The underlying data has a better precision than milliseconds.

I ended up constructing an intermediate datetime to the nearest second, reading it's value in ticks (10 000 to the millisecond), adjusting the ticks then creating the datetime that I actually wanted.

I expected constructors that took seconds as doubles and that takes a string date/time format specifier. Neither seems to exist.

Anybody got a good alternate approach?

+2  A: 

There is no direct way to instantiate a DateTime based on fractions of a second. The closest you can get, as you have discovered, is using ticks. A function like this should at least save you from having to create multiple DateTimes in every case.

public DateTime DateTimeFromFractionalSeconds(int year, int month, int day, int hour, int minute, double seconds)
{
    return new DateTime(year, month, day, hour, minute).AddTicks((long)(seconds * 1000000000));
}

You're right that the designers could have included such a constructor. However, the DateTime class is not based around seconds any more than it's around minutes. I realize that you want fractional seconds, but can you truly make the case that fractional seconds would be more legitimate than fractional minutes? Or hours? The DateTime class is based around ticks. That is the smallest indivisible unit of measure that it deals with, which is why the most granular constructor is represented in ticks.

Adam Robinson
I used that in my solution. It's a pretty stupid approach though. I have a datetime in years, months... fractional seconds. I have to convert that to ticks to then create the datetime I really want using ticks. A truly idiotic procedure.
Mike Gale
If this provided you with a solution, then marking as accepted would be appreciated!
Adam Robinson
No that's not a solution. I already did that. I was looking for a better way, or maybe some undocumented approach that did what I expected.
Mike Gale
@Mike: What you're asking for doesn't exist. "It doesn't exist" is a perfectly legitimate answer to a question, and is correct in this case. Given that this is the only answer that is correct and provides potentially useful code, I would appreciate it if you would mark it as accepted.
Adam Robinson
+1  A: 

If you want to use this date time to time how long it takes to run your code, you'd be much better off using the Stopwatch stopwatch class. It uses a high precision timer that isn't available to the DateTime object. If you aren't timing how long something takes between 2 points, and just want the current time, to something more precise than milliseconds, I think that you are going a bit overboard, and may not need all that precision, or that you the extra precision is meaningless.

Kibbee
You are entirely entitled to your opinion about precise timing, but you should keep it to yourself. Without knowledge of the sitution you can easily say something wrong. (Hint discriminating between times that are already logged against the same clock but are very close to one another.) Yes I've used much more precise timers since .NET 1, they are really good, but they are no use here.
Mike Gale