본문 바로가기

UNIX_LINUX_C_C++

Unix C 시스템 시간출력 예제

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

char *current_time( void )
{
static char time_str[ 64 ];
time_t *p_time = NULL ;
struct tm *p_tm = NULL ;
struct timeval tv ;

gettimeofday(&tv, NULL);

p_time = (time_t *)&(tv.tv_sec);
p_tm = localtime(p_time);

sprintf( time_str, "%04d%02d%02d %02d%02d%02d.%02ld",
p_tm->tm_year+1900, p_tm->tm_mon+1, p_tm->tm_mday,
p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec,
tv.tv_usec/10000 );

return time_str;
}

int main()
{
printf("%s\n", current_time() );

}