1

ISO-8601 の時刻をデシ秒の精度で出力しようとしています。YYYY-MM-DDThh:mm:ss.s

これが私のコードです:

#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void milli_time(char* dest, struct timeval* t)
{
    struct tm* timeInfo;
    strftime(dest, 22, "%Y-%m-%dT%t", localtime(&t->tv_sec));
    printf("%s\n", dest);
    fflush(stdout);
    char deciTime[3];
    sprintf(deciTime, ".%lu", ((t->tv_usec)/100000ul));

    strcat(dest, deciTime);
}

int main()
{
    struct timeval* theTime;
    char timeString[32];
    gettimeofday(theTime, NULL);
    printf("%lu.%lu\n", theTime->tv_sec, theTime->tv_usec);
    milli_time(timeString, theTime);
    printf("%s\n", timeString);
    fflush(stdout);
}

そして、実行するたびに出力は次のようになります。

134520616.3077826840
1974-04-06T17:50:16
1974-04-06T17:50:16.30778

もう 1 つ気づいたことは、tv_usec が 100 万を超えていることです。

4

1 に答える 1

5

それに対応する参照を変更struct timeval* theTimestruct timeval theTimeて更新します。

gettimeofday(&theTime, NULL);
// etc

このようにして、構造体への単なるポインターではなく、構造体にスペースを割り当てます。自分のマシンでコードを実行しようとすると、コードのセグメンテーション違反が発生します。

于 2011-11-20T07:06:12.133 に答える