C++ 标准库没有独特的日期类型。处理日期和时间的结构和函数继承自 C。要访问日期和时间相关的函数和结构,我们必须包含 ctime 头文件。处理时间的四种类型是 clock_t、time_t、size_t 和 tm。系统时间和日期可以通过类型 clock_t、size_t 和 time_t 表示为整数。
C++ 标准库中没有合适的可用数据类型。C 中日期和时间操作的结构以及函数被继承到 C++ 中。您必须在 C++ 应用程序中使用/包含名为 <ctime> 的头文件才能访问与日期和时间相关的函数和结构。Clock_t、time_t、size_t 和 tm 是四种与时间相关的类型。系统时间和日期可以通过类型 clock_t、size_t 和 time_t 表示为整数。
在本教程中,我们将学习 C++ 的日期和时间格式。由于 C++ 缺乏这些的完整实现,日期和时间格式继承自 C 语言。程序必须包含 <ctime> 头文件才能在 C++ 中使用日期和时间。
此头文件中的四种时间相关类型如下:
| 类型 | 描述 |
|---|---|
| Clock_t | Clock_t 指的是时钟类型,它是算术类型的别名。它表示时钟滴答数(系统特定长度常数的时间单位)。clock() 返回的值是 Clock_t 类型。 |
| Time_t | Time_t 是 time_type 的缩写。它表示函数 time() 返回的时间。当时间到达 00:00 时,它会生成一个整数值,表示已经过去的秒数。 |
| Size_t | 对象大小(以字节为单位)由无符号整数类型 Size_t 表示,它是该类型的别名。Size_t 是 sizeof() 运算符的结果,它将打印大小和计数。 |
| tm | - 日期和时间通过 tm 结构存储在 C 结构中。它描述如下: |
struct tm {
int tm_sec; // seconds of the minutes just from 0 to 61
int tm_min; // minutes of an hour just from 0 to 59
int tm_hour; // an hours of the day just from 0 to 24
int tm_mday; // the day of the month just from 1 to 31
int tm_mon; //the month of a year just from 0 to 11
int tm_year; // since 1900, year
int tm_wday; // days have passed since Sunday
int tm_yday; // days that have passed since January 1st
int tm_isdst; // hours of the daylight savings time
}
在 C 或 C++ 编程语言中使用日期和时间时,tm 结构非常重要。日期和时间以 C 结构的形式存储在此结构中。Tm 结构经常被相关函数使用。让我们看一些 C++ 日期和时间示例来完全理解这个概念。
下表列出了一些在 C 和 C++ 中使用的日期和时间函数。
| 函数名 | 函数原型 | 描述 |
|---|---|---|
|
ctime
|
char *ctime(const time_t *time);
|
返回指向格式为“星期 月 日 时:分:秒 年”的字符串的指针。
|
|
gmtime
|
struct tm *gmtime(const time_t *time);
|
以协调世界时 (UTC) 格式返回指向 tm 结构的指针,这实际上是格林威治标准时间 (GMT)。 |
|
localtime
|
struct tm *localtime(const time_t *time);
|
返回指向表示本地时间的 tm 结构的指针。 |
|
strftime
|
size_t strftime();
|
用于格式化日期和时间的格式。 |
|
asctime
|
char * asctime ( const struct tm * time );
|
将 tm 类型的 time 对象转换为字符串后,返回指向字符串的指针。 |
|
time
|
time_t time(time_t *time);
|
返回当前时间。
提供调用程序已执行时间的粗略估计。如果没有时间,则返回值为 -1。 |
|
Clock
|
clock_t clock(void);
|
|
|
difftime
|
double difftime ( time_t time2, time_t time1 );
|
它将比较两个时间对象 time1 和 time2,并返回差值。 |
|
mktime
|
time_t mktime(struct tm *time); |
tm 结构仅转换为 time_t 格式或等效的日历格式。 |
#include <iostream>
#include <ctime>
using namespace std;
int main( )
{
time_t ttime = time(0);
char* dt = ctime(&ttime);
cout << "The current local date and time is: " << dt << endl;
tm *gmt_time = gmtime(&ttime);
dt = asctime(gmt_time);
cout << "The current UTC date and time is:"<< dt << endl;
}
输出
The current local date and time is: Tue Sep 6 18:37:21 2022 The current UTC date and time is: Tue Sep 6 18:37:21 2022
以上程序的解释
上面的示例使用 time 函数检索当前时间,然后将其转换为字符串格式并显示在输出中。同样,它还使用 gmtime 函数获取 GMT,并使用“asctime”函数将其转换为字符串格式。稍后,它会向用户显示 GMT 时间。
#include <iostream>
#include <ctime>
using namespace std;
int main( )
{
time_t ttime = time(0);
cout <<"Number of seconds that is elapsed since January 1, 1990:" << ttime << endl;
tm *local_time = localtime(&ttime;);
cout << "Year: "<< 1900 + local_time->tm_year << endl;
cout << "Month: "<< 1 + local_time->tm_mon<< endl;
cout << "Day: "<< local_time->tm_mday << endl;
cout << "Time: "<< 1 + local_time->tm_hour << ":";
cout << 1 + local_time->tm_min << ":";
cout << 1 + local_time->tm_sec << endl;
}
输出
Number of seconds that is elapsed since January 1, 1990:1662489811 Year: 2022 Month: 9 Day: 6 Time: 19:44:32
我们获取了本地时间,然后以“小时:分钟:秒”的格式显示了年份、月份、日期和时间,如上面结果所示。
在 C++ 11 中可以使用 std::chrono::system clock::now() 函数获取当前系统时间和日期。让我们看一个示例。将此代码放入 compute time.cpp 文件中。此代码确定系统实际计算前 100 个自然数的平方所需的时间。
#include <iostream>
#include <chrono>
#include <ctime>
#include <time.h>
#include <string.h>
int main()
{
int i;
int sq;
struct tm tm;
auto start = std::chrono::system_clock::now();
for (i=1; i<=100; i++)
sq = i * i;
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_sec
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
localtime_r(&end_time, &tm);
char CharLocalTimeofUTCTime[30];
strftime(CharLocalTimeofUTCTime, 30, "%Y-%m-%dT%H:%M:%SZ", &tm);
//std::string strLocalTimeofUTCTime(CharLocalTimeofUTCTime);
std::cout << "finished computation at " << std::ctime(&end_time)
<< "elapsed time: " << elapsed_seconds.count() << "s\n";
}
输出
finished computation at Tue Sep 6 19:08:28 2022 elapsed time: 7.1e-08s
在 C++ 和 C 中,处理日期和时间时,tm 结构都非常重要。Tm 结构经常被相关函数使用。前面的代码片段利用时区转换将时间从 UTC 更改为 IST。
结论
关于日期和时间函数的 C++ 教程到此结束。尽管它是一个很小的主题,但它对我们理解 C++ 起着重要作用。