#include<iostream>

#include <stdlib.h>

#include <stdio.h>

#include <sys/time.h>

#include <unistd.h>

int main(){

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); //微秒

sleep(3);// 为方便观看,让程序睡三秒后对比std::cout << "3s later:" << std::endl;

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); //微秒return0;}


将秒数转换成标准时间格式

转秒用%s
date +%s
date -d "2014-10-25 11:11:11" +%s
秒转标准时间
date -d "1970-1-1 0:0:0 +1415101567 seconds"
date -d @1415101567

Logo

更多推荐