I am trying to get time with microseconds through glib which will work on windows too, if possible.
My approach:
char buff[256];
GTimeVal mtime;
g_get_current_time(&mtime);
strftime(buff, sizeof(buff), "%Y-%m-%d %H:%M:%S.%%06u", mtime);
printf("%s\n", buff);
… don’t work as expected.
What to do to get this working?
Thanks.
[Edit]
Second example:
GTimeVal start, finish;
g_get_current_time(&start);
//some operation
g_get_current_time(&finish);
GTimeVal el;
el.tv_sec = finish.tv_sec - start.tv_sec;
el.tv_usec = finish.tv_usec - start.tv_usec;
if (el.tv_usec < 0)
{
el.tv_usec += 1000000;
el.tv_sec--;
}
char st[24] = {0};
char qt[32] = {0};
if (counter)
{
sprintf(st, "%s%d%s", " Finded ", counter, " results");
sprintf(qt, " %u.%06u %s", (guint)el.tv_sec, (guint)el.tv_usec, " sec");
}
Your best shot for portability may be to drop
GTimeVal, which is deprecated in glib, forGDateTimeand related functions. (g_date_time_format()in place ofstrftime(),g_date_time_new_now()in place ofg_get_current_time()).