【问题标题】:glib, portable and readable timeglib,便携和可读的时间
【发布时间】:2012-03-22 11:18:44
【问题描述】:

我正在尝试通过 glib 获得微秒时间,如果可能的话,它也可以在 Windows 上运行。

我的做法:

     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);

...没有按预期工作。

怎样做才能让它发挥作用?

谢谢。

[编辑] 第二个例子:

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");
}

【问题讨论】:

  • 您的期望是什么?它与您的期望有何不同?
  • 我只是想对操作(和其他目的)所需的时间进行便携、可靠和精确的测量。我可以在 linux 上得到它,但在 windows(也有 gtk)中,这似乎不能正常工作。这是真的还是我的程序不够好?
  • 您需要说明 what 不起作用以及 如何 您知道它不起作用。您的第二个代码示例与您的第一个代码示例相比没有透露更多信息 - 它们只是两个使用 g_get_current_time() 的代码示例。
  • 好吧,第一个例子根本不起作用,所以可能我的代码不好,但第二个,它测量 db 查询时间在 linux 值上给出 0.142562 而在 Windows 中给出 1524126982.254264 什么是不正确的,因为类似时间流逝。
  • 第一个示例不起作用,因为GTimeVal 不是struct tm,这是strftime() 所要求的。至于第二个例子,为什么不直接打印出startend的成员来验证它们是否有意义?

标签: glib


【解决方案1】:

您最好的可移植性可能是放弃GTimeVal,它在glib 中已被弃用,用于GDateTime 和相关功能。 (g_date_time_format() 代替 strftime()g_date_time_new_now() 代替 g_get_current_time())。

【讨论】:

  • 如何使用g_date_time_format() 打印微秒?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 2016-06-22
  • 1970-01-01
  • 2014-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多