我正在使用 RED HAT 机器。我想在我的程序中获取系统时间。下面是代码的一部分。
date=%Y-%m-%d time=%H:%M:%S
是我想要的格式。
问题是它给我 GMT 时间而不是我当前的系统时间。
我怎样才能得到这种格式的系统时间
?
char *buf_cur = buf;
char timestr[TIMEBUF_SIZE];
time_t now = time (0);
strftime(timestr,TIMEBUF_SIZE, "date=%Y-%m-%d time=%H:%M:%S", localtime(&now));
buf_cur += sprintf(buf_cur, "%s",timestr);
简而言之,我想要一个 linux 中“date”命令给出的时间。以上格式。
最佳答案
localtime()
函数将日历时间timep转换为broken-
停机时间表示,相对于用户指定的表示
时区
即GMT
要获取 UTC
时间,您应该使用 gmtime()
。
关于c - c程序中特定格式的系统时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20368740/