Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using strftime in C, how can I format time exactly like a Unix timestamp?

Tags:

c

strftime

Here's the sort of time formatting I'm after:

2009-10-08 04:31:33.918700000 -0500

I'm currently using this:

strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S %Z", ts);

Which gives:

2009-10-11 13:42:57 CDT

Which is close, but not exact. I can't seem to find anything on displaying -0500 at the end. Plus I'm getting the seconds as an int.

How can I resolve these two issues?

like image 976
Vlad the Impala Avatar asked Oct 11 '09 19:10

Vlad the Impala


People also ask

What does Strftime do in C?

strftime() function in C/C++ strftime() is a function in C which is used to format date and time. It comes under the header file time. h, which also contains a structure named struct tm which is used to hold the time and date.


1 Answers

I came up with this:

    char            fmt[64], buf[64];
    struct timeval  tv;
    struct tm       *tm;

    gettimeofday(&tv, NULL);
    if((tm = localtime(&tv.tv_sec)) != NULL)
    {
            strftime(fmt, sizeof fmt, "%Y-%m-%d %H:%M:%S.%%06u %z", tm);
            snprintf(buf, sizeof buf, fmt, tv.tv_usec);
            printf("'%s'\n", buf); 
    }

Fixes for the problems you had:

  • Use gettimeofday(), and struct timeval, which has a microseconds member for the higher precision.
  • Use a two-step approach, where we first build a string containing all the data except the microseconds.
  • Use lower-case 'z' for the timezone offset. This seems to be a GNU extension.

I tried re-creating the timezone offset manually, through the second struct timezone * argument of gettimeofday(), but on my machine it returns an offset of 0 which is not correct. The manual page for gettimefday() has quite a lot to say about the handling of timezones under Linux (which is the OS I tested on).

like image 175
unwind Avatar answered Oct 20 '22 18:10

unwind