PDA

View Full Version : [SOLVED] gettimeofday on Windows?



Vadi
January 11th, 2008, 07:35 PM
Hi all,

I'm doing some experimentation with time on a simple C program. I got the Linux version to work okay with the help of gettimeofday, localtime. But on windows, I found that the timeval structure exists in winsock2.h, which doesn't have a gettimeofday..

I searched around some, but I'm not familiar with programming resources for Windows. Can anyone help me out / point me in the right direction?

I essentially want to be able to tell the current system time in hours, minutes, seconds, and miliseconds.

#Reistlehr-
January 11th, 2008, 08:07 PM
Only thing i can suggest is create an fsock to udp://localhost:13 to connect to the time server on your machine.

Vadi
January 11th, 2008, 10:05 PM
How efficient would that me? I'd need to check the time many times per second at times.

Wybiral
January 11th, 2008, 10:28 PM
I'm fairly certain windows has it's own way of doing this, unfortunately I'm not a Window programmer so I can't help. You might try a Windows-specific forum for people who can probably help more.

aks44
January 11th, 2008, 10:32 PM
The daytime service is NOT enabled on Windows by default. So I seriously doubt about the usefulness of that approach.

A good resource for the Win32 API is http://msdn.microsoft.com

And FWIW Google yields very relevant results when it comes to Windows equivalents of gettimeofday()...

Vadi
January 14th, 2008, 10:43 PM
Here's how I did it in the end:

#include <windows.h>

SYSTEMTIME st;
GetSystemTime(&st);

Then you can use st.wHour, st.wMinute, st.wSecond, and st.wMilliseconds to retrieve the values (it has days, months in it too but I don't need them).

onetechguy
October 30th, 2009, 02:39 AM
Even though this thread was marked as solved, the solution provide was not proper (i mean not complete), so for benefit of other I would like to point to complete solution http://www.cpp-programming.net/c-tidbits/gettimeofday-function-for-windows/