c - Getting timestamp with date and time including microseconds -
for error-logbook want create timestamp date , time, including microseconds. should have form 2016.07.19 13:59:31:123.456
i found lot of examples of time_t
, resolution seconds...
you can use gettimeofday
:
#include <time.h> #include <sys/time.h> .... struct timeval tv; gettimeofday(&tv, null);
where struct timeval
defined as:
struct timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ };
you can use gmtime
split seconds part:
struct tm *ts = gmtime(&tv.tv_sec);
where struct tm
defined as:
struct tm { int tm_sec; /* seconds */ int tm_min; /* minutes */ int tm_hour; /* hours */ int tm_mday; /* day of month */ int tm_mon; /* month */ int tm_year; /* year */ int tm_wday; /* day of week */ int tm_yday; /* day in year */ int tm_isdst; /* daylight saving time */ };
the members of tm structure are:
tm_sec number of seconds after minute, in range 0 59, can 60 allow leap seconds.
tm_min number of minutes after hour, in range 0 59.
tm_hour number of hours past midnight, in range 0 23.
tm_mday day of month, in range 1 31.
tm_mon number of months since january, in range 0 11.
tm_year number of years since 1900.
tm_wday number of days since sunday, in range 0 6.
tm_yday number of days since january 1, in range 0 365.
tm_isdst flag indicates whether daylight saving time in effect @ time described. value positive if daylight saving time in effect, 0 if not, , negative if infor- mation not available.
edit:
windows doesn't have gettimeofday
. here's implementation can use:
int gettimeofday(struct timeval *tv, struct timezone *tz) { const unsigned __int64 epoch_diff = 11644473600000000; unsigned __int64 tmp; filetime t; if (tv) { getsystemtimeasfiletime(&t); tmp = 0; tmp |= t.dwhighdatetime; tmp <<= 32; tmp |= t.dwlowdatetime; tmp /= 10; tmp -= epoch_diff; tv->tv_sec = (long)(tmp / 1000000); tv->tv_usec = (long)(tmp % 1000000); } return 0; }
Comments
Post a Comment