#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <time.h> char time_string[40]; char *getLocalTime(char *timeStr, int len, struct timeval tv ) { struct tm *ptm; // char time_string[40]; long milliseconds; ptm = localtime (&(tv.tv_sec)); strftime(time_string,len, "%Y-%m-%d %H-%M-%S", ptm ); milliseconds = tv.tv_usec/1000; sprintf(timeStr, "%s.%03ld", time_string,milliseconds ); return time_string; } int main(int argc, const char **argv) { char local_time_str[128]; char *p = NULL; struct timeval tv; gettimeofday(&tv, NULL); p = getLocalTime(local_time_str, sizeof(local_time_str), tv); printf("Get local time: \n%s\n", local_time_str); return 0; }