Clocktime Resolution

I

IdleBrain

I am trying to convert long system clock time (1.2841535619381043E+17)
to Date Time as {{12/7/2007 17:21:22XXX.XXX } ie in order of us.

I receive clock time as string object type. So I have tried something
like:

strValue = Convert.ToString("1.2841535619381043E+17");
longValue = long.Parse(strValue, NumberStyles.Any);
dt = DateTime.FromFileTime(longValue);

The output was dt = "12/7/2007 17:21:22" and I have loosing datetime
resolution in the conversion.
Can someone please advise on a better way of conversion so that the
time resolution in us is not lost?

Thanks in advance.
 
N

Nicholas Paldino [.NET/C# MVP]

IdleBrain,

You didn't really lose anything. What you are seeing is the string
format of the time, which doesn't show the milliseconds in the DateTime.

I would call ToString on the structure, passing this as the format
string:

M/d/yyy H:m:s.F

The F will tell it to display the most significant digits of the seconds
fraction. You can also guarantee up to seven significant digits of the
fraction of sections by replicating the "F" (or "f" if you want zeros to be
displayed).
 
I

IdleBrain

Thank you for your reply Nicholas..It helped.

Here is what I have been doing:
strValue = Convert.ToString(myDataRow.Table.Rows.ItemArray[2]);
longValue = long.Parse(strValue, NumberStyles.Any);

dt = DateTime.FromFileTime(longValue);
strValue = dt.ToString("M/dd/yyyy, H:m:s.FFF");
// myDataRow.Table.Rows[2] = strValue;

The result is that in strValue = "12/07/2007, 17:19:4.069".

But, unfortunately the time resolution in us is lost and that is
because of the conversion from double object type to string and then
to long.
I had to do that because DateTime.FromFileTime() wasnt accepting the
double value.

Is there any workaround for this to get the time in us?
 
N

Nicholas Paldino [.NET/C# MVP]

IdleBrain,

What is the type of the value in myDataRow.Table.Rows.ItemArray[2]?

I think you are doing too much meddling in trying to get the value, when
you can use a more direct approach.

Also, what do you mean "to get the time in us", what exactly is "us"?
 
I

IdleBrain

The data in myDataRow.Table.Rows.ItemArray[2] is of type Object
{Double}.

By 'us' I meant micro seconds. The time resolution in
myDataRow.Table.Rows.ItemArray[2] is 10s of nano seconds.
 
P

Peter Duniho

[...]
But, unfortunately the time resolution in us is lost and that is
because of the conversion from double object type to string and then to
long.
I had to do that because DateTime.FromFileTime() wasnt accepting the
double value.

Is there any workaround for this to get the time in us?

I don't understand the complaing about "us". I'm assuming you mean
microseconds, but the only reason you only get milliseconds in your output
is that you only specified three places for the fraction (e.g.
"H:m:s.FFF"...there's only three F's there). Just put more F's (up to the
maximum of seven, of course).

As far as the conversion goes, I don't see the point in converting the
data to string before back to long, but I doubt that's the source of your
problem. If the original data is a double, you should be able to cast
that to long directly. But in either case, if the double is really an
integer and it's small enough to be stored in a long, there will be no
loss of precision.

Pete
 
I

IdleBrain

OK.. For some reason I had problems converting from Object {Double} to
long in the first place and I had to go through converting it to
string and then to long.

But, finally this code does the magic:

longValue = Convert.ToInt64(myDataRow.Table.Rows.ItemArray[2]);
dt = DateTime.FromFileTime(longValue);
strValue = dt.ToString("M/dd/yyyy, H:m:s.FFFFFFF");

Really appreciate your help Pete & Nicholas..
 
N

Nicholas Paldino [.NET/C# MVP]

Just one more optimization. You really don't need to call ItemArray, as
it is allocating a new array of objects for each call, and you really just
want the one value:

longValue = Convert.ToInt64(myDataRow.Table.Rows[2]);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top