这里关于C++的时间相关的内容的一些总结,当前有一些帖子标题写的C++,但是实际用的C的库,希望在这里区分一下。如有疑问,可以及时联系笔者,讨论讨论。觉得有帮助的,欢迎评论和帮忙点个赞。
C++关于时间的库事实上有两个,C++ 11之前的<ctime>,和C++ 11之后<chrono>。
在介绍这两个库之前,首先说一下什么是计算机的时间。其实只要记住什么是UNIX时间戳?GMT?UTC?什么是夏令时就可以了?这里不展开讲,简单说一下,便于理解。
UNIX时间戳:格林尼治时间1970年1月1日00:00:00到现在的秒数。
GMT:格林尼治标准时间
UTC:世界标准时间,以地球自转为标准,在GMT上修正了闰年。
CST:北京时间,比UTC多8h
夏令时:一些国家为了节约能约,把一年中一天减少1h,最后一天多1h,这样在夏令的时候比之前都早了1h。我们国家没有。
两种时间格式time_t和tm
time_t:和UNIX时间戳差不多,1970年1月1日00:00:00到现在的秒数
tm:是个结构体
struct tm
{
int tm_sec; // 秒,正常范围0-59
int tm_min; // 分钟,0-59
int tm_hour; // 小时, 0-23
int tm_mday; // 日,即一个月中的第几天,1-31
int tm_mon; // 月, 从一月算起,0-11,一般要写成p->tm_mon+1
int tm_year; // 年, 从1900至今已经多少年,一般写成 p->tm_year+1900;
int tm_wday; // 星期,一周中的第几天, 从星期日算起,0-6
int tm_yday; // 从今年1月1日到目前的天数,范围0-365
int tm_isdst; // 是否夏令时,0为否,非0为是
};
<ctime>
是C语言<time>移植过去,现在已经是内置库了,一般不用特别include
time():获取当前时间,格式是time_t
ctime:将time_t转换成字符串
localtime:将time_t转换成tm格式
#include <iostream>
#include <string>
using namespace std;
int main() {
time_t now = time(NULL);
string t1 = ctime(&now);
cout << "当前时间:" << t1 << endl;
tm* t2 = localtime(&now);
cout << "秒:" << t2->tm_sec << endl; // 秒,正常范围0-59
cout << "分钟:" << t2->tm_min << endl; // 分钟,0-59
cout << "小时:" << t2->tm_hour << endl; // 小时, 0-23
cout << "日:" << t2->tm_mday << endl; // 日,即一个月中的第几天,1-31
cout << "月:" << t2->tm_mon + 1 << endl; // 月, 从一月算起,0-11,一般要写成p->tm_mon+1
cout << "年:" << t2->tm_year + 1900 << endl; // 年, 从1900至今已经多少年,一般写成 p->tm_year+1900;
cout << "星期:" << t2->tm_wday << endl; // 星期,一周中的第几天, 从星期日算起,0-6
cout << "天数:" << t2->tm_yday << endl; // 从今年1月1日到目前的天数,范围0-365
cout << "是否夏令:" << t2->tm_isdst << endl; // 是否夏令时,0为否,非0为是
return 0;
}
输出:
当前时间:Tue Oct 10 20:55:49 2023
秒:49
分钟:55
小时:20
日:10
月:10
年:2023
星期:2
天数:282
是否夏令:0
<chrono>
精度
num和den
#include <iostream>
#include <chrono>
using namespace std;
int main() {
cout << "豪秒:" << chrono::milliseconds::period::num << "/" << chrono::milliseconds::period::den << endl;
cout << "秒:" << chrono::seconds::period::num << "/" << chrono::seconds::period::den << endl;
cout << "分:" << chrono::minutes::period::num << "/" << chrono::minutes::period::den << endl;
cout << "小时:" << chrono::hours::period::num << "/" << chrono::hours::period::den << endl;
}
结果:
豪秒:1/1000
秒:1/1
分:60/1
小时:3600/1
时间段
chrono::duration
成员函数:count()
#include <iostream>
#include <chrono>
using namespace std;
int main() {
chrono::duration tt(chrono::seconds(10));
cout << tt.count() << endl;
}
结果:10
时间点
time_point(clock,duration)
成员函数:time_since_epoch(),返回到格林尼治的duration
#include <iostream>
#include <chrono>
using namespace std;
int main() {
chrono::time_point<chrono::system_clock, chrono::seconds> tp(chrono::seconds(100));
cout << tp.time_since_epoch().count() << endl;
}
时钟
成员函数:
now():返回time_point类型的当前时间
to_time_t:将time_point转换成time_t
from_time_t():从time_t转换成time_point
#include <iostream>
#include <chrono>
#include <string>
using namespace std;
int main() {
chrono::time_point now = chrono::system_clock::now();
time_t tt = chrono::system_clock::to_time_t(now);
string str_t = ctime(&tt);
cout << str_t << endl;
}
结果:Tue Oct 10 21:43:26 2023