Second, Millisecond(毫秒), Microsecond(微秒)
gettimeofday()
struct timeval {
// seconds since Jan. 1, 1970
time_t tv_sec;
// and microseconds
suseconds_t tv_usec;
};
#include "stdio.h"
#include "sys/time.h"
// for sleep
#include "unistd.h"
void showtime(){
struct timeval tv;
gettimeofday(&tv,NULL);
printf("second :%ld\n",tv.tv_sec);
printf("millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000);
printf("microsecond:%ld\n",tv.tv_sec*1000000 + tv.tv_usec);
}
int main(){
showtime();
printf("\n");
sleep(1);
showtime();
return 0;
}
second :1631610949
millisecond:1631610949920
microsecond:1631610949920783
second :1631610950
millisecond:1631610950921
microsecond:1631610950921375