C++ 标准库没有提供所谓的日期类型,头文件ctime提供了继承于 C 语言的日期和时间的结构和函数。
结构 tm 把日期和时间以 C 结构的形式保存,tm 结构的定义如下:
struct tm { int tm_sec; // 秒,正常范围从 0 到 59,但允许至 61 int tm_min; // 分,范围从 0 到 59 int tm_hour; // 小时,范围从 0 到 23 int tm_mday; // 一月中的第几天,范围从 1 到 31 int tm_mon; // 一年中的第几月,范围从 0 到 11 int tm_year; // 自 1900 年起的年数 int tm_wday; // 一周中的第几天,范围从 0 到 6,从星期日算起 int tm_yday; // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起 int tm_isdst; // 夏令时 };
类型 clock_t、time_t 把系统时间和日期表示为整数类型;结构 timeval 把时间表示为长整型。
1. struct timeval 2. { 3. long tv_sec; //秒 4. long tv_usec; //微秒 5. };
函数:time_t time(0);
功能:默认为自1970年1月1日到当前系统时间所经过的秒数,差值为秒数。
函数:clock_t clock();
功能:返回自程序启动起处理器时钟所使用的时间,差值为毫秒数 。
函数:double difftime(time_t time1, time_t time2);
功能:返回 time1 和 time2 之间的差值,可以用 time1-time2代替。
函数:unsigned int Sleep(unsigned int miliseconds); //非<ctime>库函数
功能:执行挂起一段时间,也就是等待一段时间在继续执行。
注意:(1)Sleep是区分大小写的,有的编译器是大写,有的是小写。
(2)Sleep括号里的unsigned型时间数,windows以毫秒为单位,而linux是以秒为单位。
(3)头文件在windows里为<windows.h>,在linux里为<unistd.h>。
#include <iostream> #include <ctime> #include <windows.h> //Sleep() using namespace std; int main(void) { time_t now0 = time(0); clock_t now1 = clock(); Sleep(1499); // 模拟一段程序在工作的时间 time_t now2 = time(0); clock_t now3 = clock(); cout<<"程序运行时间:"<<difftime(now2,now0)<<endl; cout<<"程序运行时间:"<<difftime(now3,now1)<<endl; cout<<"程序运行时间:"<<now2-now0<<endl; cout<<"程序运行时间:"<<now3-now1<<endl; return 0; } /* 程序运行时间:1 程序运行时间:1499 程序运行时间:1 程序运行时间:1499 -------------------------------- Process exited after 2.136 seconds with return value 0 请按任意键继续. . . */
函数:struct tm *localtime(const time_t *time);
功能:返回一个指向表示本地时间的 tm 结构的指针。
setw(int)、setfill(char) 设置宽度和填充字符,来自库<iomanip>,用于控制输出格式。
使用 tm 结构格式化时间:
#include <iostream> #include <ctime> #include <iomanip> using namespace std; int main( ) { time_t now = time(0); cout << "1970/01/01 00:00:00到当前经过秒数:"<<now<<endl; tm *ltm = localtime(&now); cout << "\n日期: "<< 1900 + ltm->tm_year << "年"; cout << setw(2) << setfill('0') << ltm->tm_mon +1 << "月"; cout << setw(2) << setfill('0') << ltm->tm_mday << "日" << endl; cout << "\n时间: "; cout << setw(2) << setfill('0') << ltm->tm_hour << ":"; cout << setw(2) << setfill('0') << ltm->tm_min << ":"; cout << setw(2) << setfill('0') << ltm->tm_sec << endl; return 0; } /* 执行结果: 1970/01/01 00:00:00到当前经过秒数:1612144248 日期: 2021年02月01日 时间: 09:50:48 -------------------------------- Process exited after 1.033 seconds with return value 0 请按任意键继续. . . */
函数:char* ctime(const time_t *time);
功能:返回一个表示当地时间的字符串指针。
函数:char* asctime ( const struct tm *time );
功能:返回一个指向字符串的指针,字符串包含了 time 所指向结构中存储的信息。
函数:struct tm* gmtime(const time_t *time);
功能:返回一个指向 time 结构的指针,用协调世界时(UTC)表示。
#include <iostream> #include <ctime> using namespace std; int main(void) { time_t now = time(0); char* dt = ctime(&now); // 把 now 转换为 char* 字符串形式 string str(ctime(&now)); // 或者把 now 转换为 string 字符串形式 cout << "本地日期和时间:" << str; tm *tm = gmtime(&now); // 把 now 转换为 tm 结构 dt = asctime(tm); cout << "UTC 日期和时间:"<< dt; /* 提示:ctime(), asctime()转换出的字串已包括\n */ return 0; } /* 运行结果: 本地日期和时间:Sat Jan 30 06:51:43 2021 UTC 日期和时间:Fri Jan 29 22:51:43 2021 -------------------------------- Process exited after 3.677 seconds with return value 0 请按任意键继续. . . */
UTC时间和本地时间(LocalTime)的区别
世界协调时间(Universal Time Coordinated,UTC),GPS 系统中有两种时间区分,一为UTC,另一为LT(地方时)两者的区别为时区不同,UTC就是0时区的时间,(LocalTime)地方时为本地时间,如北京为早上八点(东八区),UTC时间就为零点,时间比北京时晚八小时,以此计算即可。UTC相当于本初子午线(即经度0度)上的平均太阳时,过去曾用格林威治平均时(GMT)来表示,北京时间比UTC时间早8小时,以2021年1月28日00:00UTC为例,UTC时间是零点,北京时间为2021年1月28日早上8点整。
函数:time_t mktime(struct tm *time);
功能:返回日历时间,相当于 time 所指向结构中存储的时间。
函数:size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr);
功能:格式化结构 timeptr 表示的时间,并把它存储在 str 中。
参数:str -- 这是指向目标数组的指针,用来复制产生的 C 字符串。
maxsize -- 被复制到 str 的最大字符个数。
format -- 包含了普通字符和特殊格式说明符的 C 字符串,这些格式说明符由函数替换为表示 tm 中所指定时间的相对应值。
timeptr -- 指向 tm 结构的指针。
返回:如果产生的 C 字符串小于 size 个字符(包括空结束字符),则会返回复制到 str 中的字符总数(不包括空结束字符),否则返回零。
说明符 | 替换为 | 实例 |
%a | 缩写的星期几名称 | Sun |
%A | 完整的星期几名称 | Sunday |
%b | 缩写的月份名称 | Mar |
%B | 完整的月份名称 | March |
%c | 日期和时间表示法 | Sun Aug 19 02:56:02 2012 |
%d | 一月中的第几天(01-31) | 19 |
%H | 24 小时格式的小时(00-23) | 14 |
%I | 12 小时格式的小时(01-12) | 05 |
%j | 一年中的第几天(001-366) | 231 |
%m | 十进制数表示的月份(01-12) | 08 |
%M | 分(00-59) | 55 |
%p | AM 或 PM 名称 | PM |
%S | 秒(00-61) | 02 |
%U | 一年中的第几周,以第一个星期日作为第一周的第一天(00-53) | 33 |
%w | 十进制数表示的星期几,星期日表示为 0(0-6) | 4 |
%W | 一年中的第几周,以第一个星期一作为第一周的第一天(00-53) | 34 |
%x | 日期表示法 | 08/19/12 |
%X | 时间表示法 | 02:50:06 |
%y | 年份,最后两个数字(00-99) | 01 |
%Y | 年份 | 2012 |
%Z | 时区的名称或缩写 | CDT |
%% | 一个 % 符号 | % |
#include <iostream> #include <ctime> #include <array> using namespace std; int main () { time_t ltm; struct tm *info; char buff[12]; char datetime[20]; array<string,7>weekday = {"日","一","二","三","四","五","六"}; int year=2021, month=1, day=31; //或可由cin输入 time(<m); info = localtime(<m); info->tm_year = year - 1900; info->tm_mon = month - 1; info->tm_mday = day; mktime(info); //调用 mktime: info->tm_wday cout<<"那天是星期"<<weekday.at(info->tm_wday)<<endl; strftime(buff, 12, "%Y-%m-%d", info); cout<<"那天的日期:"<<buff<<endl; strftime(buff, 12, "%H:%M:%S", info); cout<<"现在当地时间:"<<buff<<endl; info = localtime(<m); strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", info); cout<<"当前日期时间:"<<datetime<<endl; return 0; } /* 运行结果: 那天是星期日 那天的日期:2021-01-31 现在当地时间:11:20:45 当前日期时间:2021-01-31 11:20:45 -------------------------------- Process exited after 0.812 seconds with return value 0 请按任意键继续. . . */