2011-11-20 2 views
1

ISO-8601의 시간을 십진 초의 정밀도로 인쇄하려고합니다. YYYY-MM ISO 형식 : mm : 나는 그것이 실행C : gettimeofday()는 매번 같은 값을 반환합니다.

#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 

이 다른 것은 내가 알 여기

내 코드입니다 ss.s tv_usec이 백만 이상이라는 것입니다.

답변

5

변경 struct timeval* theTimestruct timeval theTime과 그것에 대응하는 참조를 업데이트 :

gettimeofday(&theTime, NULL); 
// etc 

이 방법은 오히려 구조체 단지 포인터보다, 구조체의 공간을 할당하고 있습니다. 내 컴퓨터에서 실행하려고하면 코드가 segfault됩니다.

+0

덕분에 많은 도움이되었습니다. – julianc

관련 문제