在C编程中存储系统调用

时间:2014-08-27 08:57:37

标签: c datetime

我有一个C程序,它使用以下行将时间戳打印为字符串:

sprintf (scratch_buffer, "%s\0", dfr_time_date_strg);

此时间戳始终以UTC格式打印,但我想添加调用字符串date +%zdate +%Z

可以这样调用两个时区值:

system("date +%z");
system("date +%Z");

但是如何将这些字符串分配给名为chartz_offset的{​​{1}}字符串,以便最终的时间戳打印行为:

tz_name

1 个答案:

答案 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