Boris Raznikov
Boris Raznikov

Reputation: 2453

.NET QueryPerformanceCounter printing the day time

I need to format the day time using QueryPerformanceCounter Win32 API. The format, is: HH:mm:ss.ffffff , containing hours minuts seconds and microseconds. I need to use THIS function, because another process (written in C) is using this function and the purpose is using the same function in both places.

Thanks

Upvotes: 2

Views: 2318

Answers (4)

zerm
zerm

Reputation: 2842

Possibly I misunderstand the question, as for me none of the previous answers are relevant at all.

I had the problem (which sent me here): Given a value from QueryPerformanceCounter, because something out of my control specifies timestamps using that function, how can I convert these values to a normal date / time?

I figured that QueryPerformanceCounter returns the number of seconds since the system booted, multiplied (and extended in resolution) depending on QueryPerformanceFrequency. Thus, the most simple solution is to get the current date/time, subtract the amount of seconds returned by QueryPerformanceCounter/QueryPerformanceFrequency, and then add the values you like to format as time of day.

Upvotes: 0

Boris Raznikov
Boris Raznikov

Reputation: 2453

Can use : 1) The System.Diagnostics.Stopwatch class uses QueryPerformanceCounter(), saves you from having to P/Invoke it. 2) Can use directly by importing from the Win32 dll . [DLLImport(Win32)] and the name ofthe function

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942000

The System.Diagnostics.Stopwatch class uses QueryPerformanceCounter(), saves you from having to P/Invoke it.

Upvotes: 5

Mike Dinescu
Mike Dinescu

Reputation: 55750

You should not use QueryPerformanceCounter to determine time of day. It can only be used to determine an elapsed interval with a very high resolution as it returns the number of ticks that passed since the computer was last restarted.

As such, at best, you may only determine how many hours, minutes, and seconds have passed since a previous reading of QueryPerformanceCounter which must not have happened too long in the past.

In order to convert from ticks to seconds you need to determine the frequency (using QueryPerformanceFrequency) of the ticks on the computer you're running the QueryPerformanceCounter function and then divide your reading by that frequency:

// obtain frequency
long freq;
QueryPerformanceFrequency(freq);

// then obtain your first reading
long start_count;
long end_count;
QueryPerformanceCounter(start_count)

// .. do some work

// obatin your second reading
QueryPerformanceCounter(end_count);

// calculate time elapsed
long milliseconds_elapsed = (long)(((double)(end_count - start_count) / freq) * 1000);

// from here on you can format milliseconds_elapsed any way you need to

An alternative to the above example would be to use the TimeSpan structure available in .Net which has a constructor that takes ticks like so:

// then obtain your first reading
long start_count;
long end_count;
QueryPerformanceCounter(start_count)

// .. do some work

// obatin your second reading
QueryPerformanceCounter(end_count);

TimeSpan time_elapsed = new TimeSpan(end_count - start_count);

Console.WriteLine("Time Elapsed: " + time_elapsed.ToString());

Upvotes: 2

Related Questions