0

I have a C programme, which prints a timestamp as a string using the following line:

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

This timestamp is always printed as UTC, but I would like to add the string of the calls date +%z and date +%Z.

The two timezone values can be called as such:

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

But how can I assign these to char strings called tz_offset and tz_name so that the final timestamp print line is:

sprintf (scratch_buffer, "%s %s (%s)\0", dfr_time_date_strg, tz_offset, tz_name);
Dustin Cook
  • 1,215
  • 5
  • 26
  • 44

1 Answers1

4

First off, the thing you are describing as "system call" is not a system call, only a call to the system() function. A system call is something different.

As to your question: you have a stereotypical XY problem. You definitely do NOT want to grab the output of the date command. (That could be done using popen(), but please don't.) You rather want to use the standard library:

#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;
}

The above snippet, when compiled and run, prints CEST for me.

Community
  • 1
  • 1