https://tool.lu/timestamp/
c++时间戳
自 1970 年 1 月 1 日以来经过的秒数:
time_t time1 = time(0);//这里获取到的其实就是一个long类型的时间戳,是秒级别的(10位),非毫秒级别
time_t time1 = time(0);
cout << "time1 = " << time1 << endl;//1498122787
char * strTime = ctime(&time1);
cout << "strTime = " << strTime << endl;//Thu Jun 22 17:13:07 2017
time_t startTime = 1498122787;
double betweenSecond = difftime(time1, startTime);//该函数返回 time1 和 time2 之间相差的秒数。
cout << "betweenSecond = " << betweenSecond << endl;//Thu Jun 22 17:13:07 2017
1.时间戳转格式化
time_t t = time(0);
struct tm *p;
p=gmtime(&t);
char s[100];
strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", p);
printf("%d: %s\n", (long)t, s); //1498124250: 2017-06-22 09:37:30
2.格式化转时间戳
long getTick(char *str_time)
{
struct tm stm;
int iY, iM, iD, iH, iMin, iS;
memset(&stm,0,sizeof(stm));
iY = atoi(str_time);
iM = atoi(str_time+5);
iD = atoi(str_time+8);
iH = atoi(str_time+11);
iMin = atoi(str_time+14);
iS = atoi(str_time+17);
stm.tm_year=iY-1900;
stm.tm_mon=iM-1;
stm.tm_mday=iD;
stm.tm_hour=iH;
stm.tm_min=iMin;
stm.tm_sec=iS;
return mktime(&stm);
}
int main()
{
char str_time[19];
printf("请输入时间:"); /*(格式:2011-12-31 11:43:07)*/
gets(str_time);
printf("%ld\n", GetTick(str_time));
return 0;
}
应示示例2
// mothed 1 2019-04-28 17:07:19
#include <string>
#include <time.h>
#include <stdio.h>
#include <iostream>
using namespace std;
string getTime()
{
time_t timep;//time_t 获得时间只能精确到秒,clock_t 获得时间能够精确到毫秒
time (&timep);
cout << timep<< endl;//1556449032 10位
char tmp[64];
strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S",localtime(&timep) );
return tmp;
}
int main(){
string time = getTime();
cout << time << endl;//2019-04-29 13:51:45
return 0;
}
C++11获取时间戳和时间戳转日期(毫秒精度)
获取时间戳
std::time_t getTimeStamp()
{
std::chrono::time_point<std::chrono::system_clock,std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
auto tmp=std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
std::time_t timestamp = tmp.count();
//std::time_t timestamp = std::chrono::system_clock::to_time_t(tp);
return timestamp;
}
时间戳转日期
其中int64 为自定义跨平台的数据类型
std::tm* gettm(int64 timestamp)
{
int64 milli = timestamp+ (int64)8*60*60*1000;//此处转化为东八区北京时间,如果是其它时区需要按需求修改
auto mTime = std::chrono::milliseconds(milli);
auto tp=std::chrono::time_point<std::chrono::system_clock,std::chrono::milliseconds>(mTime);
auto tt = std::chrono::system_clock::to_time_t(tp);
std::tm* now = std::gmtime(&tt);
printf("%4d年%02d月%02d日 %02d:%02d:%02d\n",now->tm_year+1900,now->tm_mon+1,now->tm_mday,now->tm_hour,now->tm_min,now->tm_sec);
return now;
}
应用示例
// mothed 2019-04-28 17:07:19
#include <string>
#include <time.h>
#include <chrono> // for time stamp
#include <stdio.h>
#include <iostream>
#include<iomanip>
using namespace std;
std::time_t getTimeStamp()
{
std::chrono::time_point<std::chrono::system_clock,std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
auto tmp=std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
std::time_t timestamp = tmp.count();
//std::time_t timestamp = std::chrono::system_clock::to_time_t(tp);
return timestamp;
}
std::clock_t getTimeStamp_s()
{
std::chrono::time_point<std::chrono::system_clock,std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
auto tmp=std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
std::clock_t timestamp = tmp.count();
//std::clock_t timestamp = std::chrono::system_clock::to_time_t(tp);
return timestamp;
}
int main(){
// 记录系统时间
auto start = chrono::system_clock::now();
long i=0;
while(i<1111111111)
i++;
auto now = chrono::system_clock::now();
auto timestamp = chrono::duration_cast<chrono::milliseconds>(now - start);
cout<<setiosflags(ios::fixed)<<setprecision(6)<<double(timestamp.count())/1000.0<<endl;//1.569000
time_t start1=getTimeStamp();
cout<<setprecision(6)<<start1/1000.0<<endl;//1556450562.840000
clock_t start2=getTimeStamp_s();
cout<<setprecision(6)<<start2/1000.0<<endl;//1556450562.840000
return 0;
}
其他示例
// mothed 2 //1556442813891 秒级 13位
#include<iostream>
#include <sys/time.h>
#include<iomanip>
using namespace std;
int64_t getCurrentTime()
{
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec * 1000 + tv.tv_usec/1000;//????????
}
double getCurrentTime_s()//XXXXXXX
{
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec * 1000 + tv.tv_usec/1000;//????????
}
int main()
{
int64_t curTime;
int64_t start = getCurrentTime();
while(true)
{
curTime = getCurrentTime();
cout <<std::defaultfloat<< setprecision(16)<<curTime<<endl; //1556442813891 (13位 int64_t)
long i=0;
while(i<1000) i++;
cout << curTime - start <<endl;
}
//XXXXXXXXXXXX
// double curTime_s;
// double start_s = getCurrentTime_s();
// while(true)
// {
// curTime_s = getCurrentTime_s();
// //cout <<std::defaultfloat<< setprecision(6)<<curTime_s/1000<<endl; //1.55652e+09
// cout <<setprecision(6)<<curTime_s/1000<<endl; //1.55652e+09
// long i=0;
// while(i<11111) i++;
// cout << curTime_s - start_s <<endl;
// }
return 0;
}
其他示例 XX
// mothed 3
#include <string>
#include <time.h>
#include <stdio.h>
#include <iostream>
#include<iomanip>
using namespace std;
int main(){
clock_t start,ends;
start=clock();
cout<<setiosflags(ios::fixed)<<setprecision(6)<<start<<endl;//7368
long i=0;
while(i<1000000) i++;
ends=clock();
cout<<ends-start<<endl;//1453
return 0;
}
补充
在time.h文件中,我们可以找到对它的定义:
#ifndef _CLOCK_T_DEFINED
typedef long clock_t;
#define _CLOCK_T_DEFINED
#endif
很明显,clock_t是一个长整形数。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:
#define CLOCKS_PER_SEC ((clock_t)1000)
在linux系统下,CLOCKS_PER_SEC的值可能有所不同,目前使用的linux打印出来的值是1000000,表示的是微秒。这一点需要注意。
可以看到每过千分之一秒(1毫秒),调用clock()函数返回的值就加1。下面举个例子,你可以使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的运行时间:
void elapsed_time()
{
printf("Elapsed time:%u secs.\n",clock()/CLOCKS_PER_SEC);
}
当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
运行结果
Time to do 10000000 empty loops is 0.03000 seconds
上面我们看到时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一些,从而使计时精度更高呢?通过尝试,你会发现这样是不行的。在标准C/C++中,最小的计时单位是1毫秒。
宏
宏CLOCKS_PER_SEC适用于将计算系统时间类型转换为用户可读的秒时间,包含于头文件ctime(或time.h)中
利用clock()函数和for循环完成程序定时暂停
在很久以前人们就用for来使程序暂停了,但是面对的问题是计算机越来越强大,以至于单用for循环已经无法控制计算机的处理速度,此时使用clock()函数就可以完成对程序的有效计时,并将这个时间用于暂停。
#include<iostream>
#include<ctime>//or time.h
using namespace std;
int main()
{
//省略
int b;
int b=clock()/CLOCKS_PER_SEC;
for(int i=0;i<100;)//完成100毫秒定时
{
i=clock()/CLOCKS_PER_SEC;
i=i-b;
}
//省略
return 0;
}