1

I am trying to format my time stamp in ISO 8601 with microseconds in C. Like this 2014-11-06T10:34:47.123456Z, but I cant seem to get microseconds. Any help would be greatful.

#pragma warning(disable:4996)
#define _CRT_SECURE_NO_WARNING
#include <time.h>
#include <stdlib.h>
#include <stdio.h>


#define SIZE 256

int
main(void)
{
    char buffer[SIZE];
    time_t curtime;


    /* Get the current time. */
    curtime = time(NULL);

    strftime(buffer, sizeof buffer, "ISO 8601 %Y-%m-%dT%H:%M:%S\n", gmtime(&curtime));
    fputs(buffer, stdout);
    getchar();
    return 0;
}
drhunn
  • 21
  • 1
  • 2
  • 13
  • 1
    `time(NULL)` returns whole seconds since the epoch. How do you propose to get a meaningful fraction of a second from that? The documentation doesn't mention a format string for that at all. https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strftime-wcsftime-strftime-l-wcsftime-l – Retired Ninja Dec 24 '17 at 20:53
  • 1
    `"ISO 8601 %Y-%m-%dT%H:%M:%S\n"` --> `"ISO 8601 %Y-%m-%dT%H:%M:%S.000000\n"` – chux - Reinstate Monica Dec 24 '17 at 21:10
  • 1
    See [C get system time to microsecond accuracy on windows?](https://stackoverflow.com/q/4568221/2410359) – chux - Reinstate Monica Dec 24 '17 at 21:14
  • 1
    Part 1 of your problem is to get a time with a microseconds component. Part 2 is to find a variant of `strftime()` that will (a) accept a time value with a microseconds component and (b) provide a way of formatting the microseconds component. The C standard does not recognize such a function; nor does POSIX. You may have to write your own if you can’t find one on the web. – Jonathan Leffler Dec 25 '17 at 01:24

0 Answers0