揮発性のメモ2

http://d.hatena.ne.jp/iww/

clock_gettime の使い方

Man page of CLOCK_GETRES

#include <stdio.h>
#include <time.h>

char *getnow()
{
    static char buf[32];
    struct timespec t;
    struct tm tm;

    clock_gettime(CLOCK_REALTIME, &t);
    localtime_r(&t.tv_sec, &tm);
    strftime(buf, 32, "%Y-%m-%d %H:%M:%S.", &tm);
    sprintf(buf+20, "%03d", (int)t.tv_nsec/1000000);

    return buf;
}

int main()
{
    printf("%s\n", getnow());
    return 0;
}

CLOCK_REALTIME_COARSE は精度がmsすら出ないので こんなん使うならtime()使った方がマシ