c++时间类型

时间类型可以说是非常常用的类型,在使用的过程中经常感觉掌握的不够全面,这里梳理一下,作为记录。

C风格时间

tm结构体

在C++11之前,标准库中并没有提供专门的时间和日期类型。为了处理时间和日期,开发者通常会C语言提供的时间和类型。现在C++项目基本上都使用了C++11及更高的版本,但是一些古早的代码或者库接口还是能看到C风格的时间。

为了使用C语言踢狗的日期和时间相关的函数和结构,需要在 C++ 程序中引用 <ctime> 头文件。

有四个与时间相关的类型:clock_ttime_tsize_t 和 tm。类型 clock_tsize_ttime_t 能够把系统时间和日期表示为某种整数。

结构类型 tm 把日期和时间以 C 结构的形式保存,tm 结构的定义如下:

1
2
3
4
5
6
7
8
9
10
11
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; // 夏令时
};

相关函数

一些跟C风格时间有关的函数如下:

序号 函数 & 描述
time_t time(time_t *time); 该函数返回系统的当前日历时间,自 1970 年 1 月 1 日以来经过的秒数。如果系统没有时间,则返回 -1。
char *ctime(const time_t *time); 该返回一个表示当地时间的字符串指针,字符串形式 day month year hours:minutes:seconds year\n\0。
struct tm *localtime(const time_t *time); 该函数返回一个指向表示本地时间的 tm 结构的指针。
clock_t clock(void); 该函数返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。如果时间不可用,则返回 -1。
char * asctime ( const struct tm * time ); 该函数返回一个指向字符串的指针,字符串包含了 time 所指向结构中存储的信息,返回形式为:day month date hours:minutes:seconds year\n\0。
struct tm *gmtime(const time_t *time); 该函数返回一个指向 time 的指针,time 为 tm 结构,用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。
time_t mktime(struct tm *time); 该函数返回日历时间,相当于 time 所指向结构中存储的时间。
double difftime ( time_t time2, time_t time1 ); 该函数返回 time1 和 time2 之间相差的秒数。
size_t strftime(); 该函数可用于格式化日期和时间为指定的格式。

格式化成字符串:

1
2
3
4
5
6
time_t now = time(NULL);
struct tm *tm_now = localtime(&now);
char buffer[80];

strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_now);
printf("Formatted time: %s\n", buffer);

C++11之后提供了string格式化函数put_time用法如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip> // 包含 put_time 的头文件
#include <ctime>
#include <sstream> // 包含 ostringstream 的头文件

int main() {
// 获取当前时间
std::time_t now = std::time(nullptr);
std::tm tm_now = *std::localtime(&now);

// 使用 ostringstream 和 put_time 格式化时间
std::ostringstream oss;
oss << std::put_time(&tm_now, "%Y-%m-%d %H:%M:%S");
std::string formatted_time = oss.str();

// 输出格式化的时间
std::cout << "Formatted time: " << formatted_time << std::endl;

return 0;
}

字符串格式化成时间:

1
2
3
4
5
6
const char *time_string = "2023-10-07 14:30:00";
struct tm tm_time;
char *result;

// 使用 strptime 将字符串解析为 tm 结构体
result = strptime(time_string, "%Y-%m-%d %H:%M:%S", &tm_time);

用例

用时间类函数给代码片段计时:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <time.h>

int main() {
time_t start, end;
double elapsed;

time(&start); // 获取开始时间
// 这里放置需要计时的代码
for (int i = 0; i < 100000000; i++) {
// 一些耗时操作
}

time(&end); // 获取结束时间

elapsed = difftime(end, start); // 计算时间差

printf("Elapsed time: %.2f seconds\n", elapsed);

return 0;
}

这种方法虽然简单易用,但是实际上这种计时方法只能精确到秒级,如果想要精确到毫秒级需要使用POSIX提供的gettimeofday函数和timeval结构体

timeval结构体

timeval结构体非常简单,只由两个成员变量组成

1
2
3
4
struct timeval {
time_t tv_sec; // 秒
suseconds_t tv_usec; // 微秒
};

相关函数

gettimeofday() 函数是POSIX标准的一部分,用于获取当前时间,精度可以达到微秒级别。以下是 gettimeofday() 函数的详细文档:

1
2
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
  • tv:指向 struct timeval 类型的指针,用于存储当前时间。
  • tz:指向 struct timezone 类型的指针,用于存储时区信息。不过,这个参数在现代系统中通常被忽略,应该设置为 NULL
  • 成功时返回0,失败时返回-1,并设置 errno 以指示错误类型。

用例

使用下面这段代码可以统计高精度的计时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>#include <sys/time.h>int main() {
struct timeval start, end;
double elapsed;

gettimeofday(&start, NULL); // 获取开始时间

// 这里放置需要计时的代码
for (int i = 0; i < 100000000; i++) {
// 一些耗时操作
}

gettimeofday(&end, NULL); // 获取结束时间

elapsed = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0; // 计算时间差

printf("Elapsed time: %.6f seconds\n", elapsed);

return 0;
}

C++风格时间

C++11标准引入了<chrono> 库是。它提供了C++版的时间和日期处理函数和数据类型,并且它是跨平台的,在开始时不用考虑平台上的差异,他包含以下内容:

  1. 时钟(Clocks)

    • system_clock:表示系统的实时时钟,通常与当前日期和时间相关联。
    • steady_clock:一个稳定的时钟,不会因为系统时间的调整而改变,适用于测量时间间隔。
    • high_resolution_clock:系统中具有最高分辨率的时钟,通常是 system_clock 或 steady_clock 的别名。

    一般来说steady_clock有比system_clock更高的时间精度,具有纳秒级别的时间精度。

  2. 时间点(Time Points)

    • time_point:表示一个特定的时间点,通常与某个时钟相关联,可以选择上面介绍的三种时钟之一。例如:

      1
      std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
  3. 时间间隔(Durations)
    std::chrono::duration 是 C++ 标准库中的一个类模板,用于表示时间间隔。

    1
    2
    3
    4
    namespace std::chrono {
    template <class Rep, class Period = std::ratio<1>>
    class duration;
    }
    • Rep:表示存储时间值的类型(如 intlong longdouble 等)。
    • Period:表示时间单位,相对于秒的比例(如 std::ratio<1> 表示秒,std::milli 表示毫秒)。
  4. 时间转换和格式化

    • duration_cast:用于在不同的时间单位之间进行转换。例如:

      1
      auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_seconds);
    • time_point_cast:用于在不同的时间点类型之间进行转换。例如:

      1
      auto tp_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
    • to_time_t:可以将 system_clock 的时间点转换为 time_t 类型,以便与C风格的日期时间函数交互。例如:

      1
      std::time_t now_c = std::chrono::system_clock::to_time_t(now);

用例

一段使用chrono统计代码耗时的代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <chrono>
#include <thread>

int main() {
// 记录开始时间
auto start = std::chrono::high_resolution_clock::now();

// 需要测量的代码块
for (int i = 0; i < 1000000; ++i) {
// 模拟一些耗时操作
std::this_thread::sleep_for(std::chrono::microseconds(1));
}

// 记录结束时间
auto end = std::chrono::high_resolution_clock::now();

// 计算耗时
std::chrono::duration<double> elapsed = end - start;

// 输出耗时
std::cout << "Elapsed time: " << elapsed.count() << " seconds" << std::endl;

return 0;
}

参考文章


Chrono in C++ - GeeksforGeeks

C++ 日期 & 时间 | 菜鸟教程

作者

echo

发布于

2024-06-30

更新于

2024-08-10

许可协议

评论