2014-08-27 50 views
0

我有一个C程序,打印使用以下行时间戳作为字符串:贮藏系统调用在C语言编程

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

此时间戳总是打印为UTC,但我想添加字符串拨打电话date +%zdate +%Z

两个时区的值可以被称为例如:

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

但我怎么可以将这些以char字符串名为tz_offsettz_name,使最终时间戳打印线是:

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

你想使用'system(“date +%z”);'和其他命令的输出吗? – 2014-08-27 08:59:22

+0

是的。例如'+ 0100'和'BST'。 – 2014-08-27 09:00:03

+3

@DustinCook首先,这些不是“系统调用”(这是非常不同的),第二,谷歌“popen()”。 **但是,如果您只需要格式化当前日期,那么只需使用'strftime()'就会好很多。 – 2014-08-27 09:00:10

回答

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我。