Getting the Current Time (In Milliseconds) from the System Clock in Windows

Getting the current time (in milliseconds) from the system clock in Windows?

To get the time expressed as UTC, use GetSystemTime in the Win32 API.

SYSTEMTIME st;
GetSystemTime(&st);

SYSTEMTIME is documented as having these relevant members:

WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;

As shf301 helpfully points out below, GetLocalTime (with the same prototype) will yield a time corrected to the user's current timezone.

You have a few good answers here, depending on what you're after. If you're looking for just time of day, my answer is the best approach -- if you need solid dates for arithmetic, consider Alex's. There's a lot of ways to skin the time cat on Windows, and some of them are more accurate than others (and nobody has mentioned QueryPerformanceCounter yet).

How can I get the Windows system time with millisecond resolution?

GetTickCount will not get it done for you.

Look into QueryPerformanceFrequency / QueryPerformanceCounter. The only gotcha here is CPU scaling though, so do your research.

How to get current system time in milliseconds from 1/1/1970 (C++)

You can use GetSystemTimeAsFileTime to get the current time as a FILETIME. Create a SYSTEMTIME representing your desired epoch (1/1/1970) and call SystemTimeToFileTime to convert it to a FILETIME. Subtract the two FILETIMEs and scale to your desired accuracy (from 100ns units to 1ms units).

This will give you the current UTC time. If you need the local time, you'll need to convert to local time using e.g. SystemTimeToTzSpecificLocalTime.

How to get the time elapsed in C in milliseconds? (Windows)

A cross platform way is to use ftime.

Windows specific link here: http://msdn.microsoft.com/en-us/library/aa297926(v=vs.60).aspx

Example below.

#include <stdio.h>
#include <sys\timeb.h>

int main()
{
struct timeb start, end;
int diff;
int i = 0;
ftime(&start);

while(i++ < 999) {
/* do something which takes some time */
printf(".");
}

ftime(&end);
diff = (int) (1000.0 * (end.time - start.time)
+ (end.millitm - start.millitm));

printf("\nOperation took %u milliseconds\n", diff);
return 0;
}

I ran the code above and traced through it using VS2008 and saw it actually calls the windows GetSystemTimeAsFileTime function.

Anyway, ftime will give you milliseconds precision.

Get system time accurate to milliseconds in Windows cmd

As Neil pointed out there is no native solution in cmd. For anyone who has the option of using PowerShell instead, you could use the following:

(Get-Date -UFormat "%Y-%m-%d %H:%M:%S").toString() + "." + ((Get-Date).millisecond)

There may be a more succinct way of doing it but this worked for my purposes.

Since the question is tagged cmd the appropriate command line for calling this from cmd is:

powershell -command "(Get-Date -UFormat '%Y-%m-%d %H:%M:%S').toString() + '.' + ((Get-Date).millisecond)"

How to get the current time in milliseconds in C Programming

quick answer

#include<stdio.h>   
#include<time.h>

int main()
{
clock_t t1, t2;
t1 = clock();
int i;
for(i = 0; i < 1000000; i++)
{
int x = 90;
}

t2 = clock();

float diff = ((float)(t2 - t1) / 1000000.0F ) * 1000;
printf("%f",diff);

return 0;
}

How to get time in milliseconds or nanoseconds in C++

If you are ok with a platform specific implementation (windows) look at the QueryPerformanceCounter API.

And this answer has a quick example of usage.



Related Topics



Leave a reply



Submit