Linux系统中可以通过gettimeofday()函数来获取当前时间,获取的时间存放在由struct timeval 定义的变量中,结构体有两个成员,分别表示秒和微秒。
若需要打印出系统时间,不经过类型强制转换会出现负数现象。
因此,需要把秒转化为long long 类型。具体代码如下:
struct timeval tv_begin;
gettimeofday(&tv_begin, NULL);
long long time_over = (long long)tv_begin.tv_sec*1000000 + tv_begin.tv_usec;
printf ("time is %lld\n", time_over);