linux 下获取当前时间精确到微妙

来源:互联网 发布:dnf辅助端口免费制卡 编辑:程序博客网 时间:2024/05/24 04:57

#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