我有一个C程序,它使用以下行将时间戳打印为字符串:
sprintf (scratch_buffer, "%s\0", dfr_time_date_strg);
此时间戳始终以UTC格式打印,但我想添加调用字符串date +%z
和date +%Z
。
可以这样调用两个时区值:
system("date +%z");
system("date +%Z");
但是如何将这些字符串分配给名为char
和tz_offset
的{{1}}字符串,以便最终的时间戳打印行为:
tz_name
答案 0 :(得分:4)
首先,你所描述的“系统调用”是而不是系统调用,只调用system()
函数。系统调用是something different。
关于你的问题:你有stereotypical XY problem。 您绝对不想获取date
命令的输出。(That could be done using popen()
,但请不要。)您更愿意使用标准库:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
time_t t = time(NULL);
struct tm *tm = localtime(&t);
char buf[256];
strftime(buf, sizeof buf, "%Z", tm);
printf("%s\n", buf);
return 0;
}
上面的代码片段在编译和运行时会为我打印CEST
。