0

The only thing I know is time(NULL), but it return seconds since 1970. It's fine to me to use WinApi functions if C doesn't have needed function.

I even found GetLocalTime WinApi function, but it return current date-time as struct...

The reason I don't want to multiply seconds by 1000 is because I inject my code into some program, and my code used to capture certain events from two sources, and I need to know their exact time, because if both events happened at same amount of seconds since 1970, but different amount of milliseconds, then for me they seems like happen in same time, and it's hard to determine what happened first (I doing events sorting later...)

Kosmo零
  • 4,001
  • 9
  • 45
  • 88
  • exist [`RtlTimeToSecondsSince1970`](https://learn.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-rtltimetosecondssince1970) and [`RtlTimeToSecondsSince1980`](https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/nf-ntifs-rtltimetosecondssince1980) for convert time (from 1601) to seconds – RbMm Jul 27 '20 at 10:43
  • @RbMm - Sorry, but I don't understand how it can help me to get milliseconds since 1970. – Kosmo零 Jul 27 '20 at 10:45
  • 1
    if want more exactly time - `GetSystemTimeAsFileTime` - it return by fact 8 byte integer. or can use relative (boot) time - `GetTickCount64` - milliseconds but since last boot – RbMm Jul 27 '20 at 10:57
  • You can use clock, which starts at 0 when your program starts. – stark Jul 27 '20 at 11:13
  • @stark - Thanks, but I prefer something I can turn into real time later, so I can measure time passed between certain events in human friendly time. – Kosmo零 Jul 27 '20 at 11:26
  • @RbMm - Thanks, with `GetSystemTimeAsFileTime` ans some tricks I was able to get milliseconds passed since 1970. – Kosmo零 Jul 27 '20 at 11:33
  • If all you need is to distinguish whether events happened at the same time, there's no need to limit yourself to any particular epoch. Time since system start is good enough, [GetTickCount64](https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-gettickcount64) returns that value. – IInspectable Jul 27 '20 at 12:53

3 Answers3

1

In Unix, you have (probably you'll get some of these apis also working in windows) gettimeofday(2), which is BSD implementation of time, it is based on struct timeval which is a struct that has two fields, tv_sec (time in seconds since epoch, as given by time(2)) and tv_usec (time in µsec, as an integer, between 0 and 999999)

This will suffice for your requirements, but today, it is common to use Posix' calls clock_gettime(2), which allow you to select the type of time you want to get (wall clock time, cpu time, etc.) clock_gettime(2) uses a similar struct timespec (this time it has tv_sec and tv_nsec ---nanosecond--- resolution)

No clock is warranted to get nanosecond resolution (but someones do), but at least you get up to the µsec level, which is more than you want)

In order to be able to give the time as milliseconds, you have just to multiply the tv_sec by 1000, and then add the tv_usec(if using gettimeofday()) value divided by 1000. Or if you prefer to use clock_gettime(), you will add the tv_sec field multiplied by 1000, and then add the tv_nsec field divided by 1000000.

If you just need to compare which timestamp is earlier than other, you can just compare both tv_sec fields, and if they happen to be equal, then compare the tv_usec fields. Every unix I know about (except SCO UNIX) do implement gettimeofday() to µsec resolution.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31
0

This is WinApi way to determine milliseconds passed since 1970. I made it since no one provide answer with native C way (maybe there is no native way at all)...

    SYSTEMTIME unix_epoch;
    unix_epoch.wYear = 1970;
    unix_epoch.wMonth = 1;
    unix_epoch.wDay = 1;
    unix_epoch.wDayOfWeek = 4;
    unix_epoch.wHour = 0;
    unix_epoch.wMilliseconds = 0;
    unix_epoch.wMinute = 0;
    unix_epoch.wSecond = 0;

    FILETIME curr_time_as_filetime;
    GetSystemTimeAsFileTime(&curr_time_as_filetime);

    FILETIME unix_epoch_as_filetime;
    SystemTimeToFileTime(&unix_epoch, &unix_epoch_as_filetime);

    ULARGE_INTEGER curr_time_as_uint64;
    ULARGE_INTEGER unix_epoch_as_uint64;

    curr_time_as_uint64.HighPart = curr_time_as_filetime.dwHighDateTime;
    curr_time_as_uint64.LowPart = curr_time_as_filetime.dwLowDateTime;

    unix_epoch_as_uint64.HighPart = unix_epoch_as_filetime.dwHighDateTime;
    unix_epoch_as_uint64.LowPart = unix_epoch_as_filetime.dwLowDateTime;

    ULARGE_INTEGER milliseconds_since_1970;
    milliseconds_since_1970.QuadPart = (curr_time_as_uint64.QuadPart - unix_epoch_as_uint64.QuadPart) / 10000;
Kosmo零
  • 4,001
  • 9
  • 45
  • 88
  • I am glad you have got your solution and thanks for your sharing, I would appreciate it if you mark them as answer and this will be beneficial to other community. – Zeus Jul 29 '20 at 01:07
0

An alternative would be to use clock() in combination with time(NULL) at start of program

start_time = time(NULL)

....

clock() + start_time * CLOCKS_PER_SEC

That would give you a better estimate and maybe exact enough for your needs?

AndersK
  • 35,813
  • 6
  • 60
  • 86