mirror of http://git.sairate.top/sairate/doc.git
44 lines
2.1 KiB
Markdown
44 lines
2.1 KiB
Markdown
|
1. `time_t time(time_t* timer)`: 返回当前的日历时间作为一个 `time_t` 对象。如果 `timer` 不是 NULL,则结果也存储在 `timer` 指向的变量中。
|
|||
|
2. `struct tm *localtime(const time_t* timer)`: 将日历时间 `timer` 转换为本地时间表示(`struct tm`),包括年、月、日、时、分和秒等字段。
|
|||
|
3. `struct tm *gmtime(const time_t* timer)`: 类似于 `localtime`,但它将日历时间 `timer` 转换为协调世界时(UTC)。
|
|||
|
4. `time_t mktime(struct tm* timeptr)`: 将表示日历时间的 `struct tm` 对象转换为 `time_t` 对象。
|
|||
|
5. `char* asctime(const struct tm* timeptr)`: 将 `struct tm` 对象转换为人类可读的字符串,表示日期和时间的格式为 "Www Mmm dd hh:mm:ss yyyy\n",其中 Www 是星期几,Mmm 是月份,dd 是日期,hh:mm:ss 是时间,yyyy 是年份。
|
|||
|
6. `char* ctime(const time_t* timer)`: 等同于 `asctime(localtime(timer))`。它将 `time_t` 对象转换为人类可读的字符串,表示本地时间。
|
|||
|
7. `clock_t clock(void)`: 返回程序自执行开始以来或上一次调用 `clock()` 以来消耗的处理器时间。返回的值以时钟滴答数表示,可以使用 `CLOCKS_PER_SEC` 将其转换为秒。
|
|||
|
|
|||
|
```c++
|
|||
|
#include <iostream>
|
|||
|
#include <ctime>
|
|||
|
|
|||
|
int main() {
|
|||
|
// 获取当前时间
|
|||
|
time_t now = time(0);
|
|||
|
std::cout << "当前时间为:" << ctime(&now);
|
|||
|
|
|||
|
// 将当前时间转换为本地时间
|
|||
|
struct tm *localTime = localtime(&now);
|
|||
|
std::cout << "本地时间为:" << asctime(localTime);
|
|||
|
|
|||
|
// 将当前时间转换为UTC时间
|
|||
|
struct tm *utcTime = gmtime(&now);
|
|||
|
std::cout << "UTC时间为:" << asctime(utcTime);
|
|||
|
|
|||
|
// 将本地时间结构体转换为时间戳
|
|||
|
time_t localTimestamp = mktime(localTime);
|
|||
|
std::cout << "本地时间的时间戳为:" << localTimestamp << std::endl;
|
|||
|
|
|||
|
// 测量程序执行时间
|
|||
|
clock_t start = clock();
|
|||
|
for (int i = 0; i < 1000000; ++i) {
|
|||
|
// 一些计算任务
|
|||
|
}
|
|||
|
clock_t end = clock();
|
|||
|
double elapsedSeconds = double(end - start) / CLOCKS_PER_SEC;
|
|||
|
std::cout << "程序执行时间为:" << elapsedSeconds << " 秒" << std::endl;
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
```
|
|||
|
|