一 简介
1. 类型
(1) struct tm
日历时间类型;
被解开( broken down )的日历时间类型。
struct tm
{
int tm_sec; // seconds after the minute - [0, 60] including leap second
int tm_min; // minutes after the hour - [0, 59]
int tm_hour; // hours since midnight - [0, 23]
int tm_mday; // day of the month - [1, 31]
int tm_mon; // months since January - [0, 11]
int tm_year; // years since 1900
int tm_wday; // days since Sunday - [0, 6]
int tm_yday; // days since January 1 - [0, 365]
int tm_isdst; // daylight savings time flag
};
(2)time_t
从纪元( epoch 或叫做起始点)起的时间类型;
数值类型的 typedef, 表现为 timepoint。
(3)clock_t
进程运行时间;
数值类型的 typedef, 表现为elapsed CPU time;
clock()返回。
(4)timespec(C++17)
本次不介绍
2. 时间操作(Time manipulation)
(1)difftime
计算时间( time_t )之间的差 。
double difftime( std::time_t time_end, std::time_t time_beg );
(2)time
返回自纪元起计的系统当前时间 。
std::time_t time( std::time_t* arg );
(3)clock
返回自程序启动时起的原始处理器时钟时间 ;
单位是 1/CLOCKS_PER_SEC 秒。
std::clock_t clock();
(4)timespec_get(C++17)
本次不介绍
3. 格式转换
(1)asctime
转换 tm 对象为文本表示;
转换为一个标准日历时间字符串。格式为 “Www Mmm dd hh:mm:ss yyyy\n”
char* asctime( const std::tm* time_ptr );
(2)ctime
转换 time_t 对象为文本表示;
转换为一个标准日历时间字符串,考虑时区。
如同通过调用 std::asctime(std::localtime(time)) 。
char* ctime( const std::time_t* time );
(3)strftime
转换 tm 对象到自定义的文本表示。
注意各种转换指定符的含义。
std::size_t strftime( char* str, std::size_t count, const char* format, const std::tm* time );
(4)wcsftime
转换 tm 对象为定制的宽字符串文本表示。
(5)gmtime
转换纪元起时间为以 UTC 表示的日历时间;
time_t 转换为 tm, 不考虑时区。
std::tm* gmtime( const std::time_t* time );
(6)localtime
转换纪元起时间为以本地时间表示的日历时间。
time_t 转换为 tm,考虑时区。
std::tm* localtime( const std::time_t *time );
(7)mktime
转换日历时间为纪元起的时间;
tm 转换为一个 time_t。
std::time_t mktime( std::tm* time );
3. 常量
(1)CLOCKS_PER_SEC
每秒的处理器始时钟嘀嗒数 (宏常量)。定义了clock()的单位类型。
二 例子
#include <ctime>
#include <thread> // this_thread
#include <chrono> // chrono
#include <iomanip> // put_time
#include <iostream>
int main()
{
{
// std::time
auto time = std::time(0);
std::cout << "time: "<< time << std::endl;
}
{
// std::difftime
auto start = std::time(0);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
auto end = std::time(0);
std::cout << "std:difftime: " << std::difftime(end, start) << std::endl;
}
{
// clock
auto clock = std::clock();
std::cout << "clock: " << clock << std::endl;
}
{
// asctime
auto time = std::time(0);
std::cout << "std::asctime: " << std::asctime(std::localtime(&time)) << std::endl;
}
{
// ctime
auto time = std::time(0);
std::cout << "std::ctime: " << std::ctime(&time) << std::endl;
}
{
// strftime
auto time = std::time(0);
char str[200];
std::strftime(str, sizeof(str), "%A %c", std::localtime(&time));
std::cout << "std::strftime: " << str << std::endl;
}
{
// gmtime
// localtime
auto time = std::time(0);
std::cout << "std:gmtime: " << std::put_time(std::gmtime(&time), "%c %A") << std::endl;
std::cout << "std:localtime: " << std::put_time(std::localtime(&time), "%c %A") << std::endl;
}
{
// mktime
auto time = std::time(0);
auto tm = *std::localtime(&time);
tm.tm_mon += 2;
std::mktime(&tm);
auto time1 = std::mktime(&tm);
std::cout << "std:mktime: " << std::put_time(std::localtime(&time1), "%c %A") << std::endl;
}
{
// CLOCKS_PER_SEC
std::cout << "CLOCKS_PER_SEC: " << CLOCKS_PER_SEC << std::endl;
}
std::cin.get();
return 0;
}
过程中的 this_thread, 请参考 C++11 std::this_thread
猜猜结果中的两个空行怎么来的。
三 参考