#include <iostream>
#include <ctime>
int main() {
// 获取当前时间的时间戳
time_t now = time(nullptr);
// 将时间戳转换为本地时间
struct tm *localTime = localtime(&now);
// 打印原始时间戳
std::cout << "Current time as time_t: " << now << std::endl;
// 打印转换后的本地时间
std::cout << "Local time: "
<< localTime->tm_year + 1900 << "-" // 年份从1900年开始计算
<< localTime->tm_mon + 1 << "-" // 月份从0开始计算
<< localTime->tm_mday << " "
<< localTime->tm_hour << ":"
<< localTime->tm_min << ":"
<< localTime->tm_sec << std::endl;
return 0;
}
time_t now = time(nullptr);:获取当前时间的时间戳(以秒为单位,自1970年1月1日以来的秒数)。**:将时间戳转换为本地时间结构体tm`,该结构体包含年、月、日、时、分、秒等信息。std::cout << "Current time as time_t: " << now << std::endl;:打印原始的时间戳。std::cout << "Local time: " ...:根据 tm 结构体中的各个字段,格式化输出本地时间。这个示例展示了如何使用 C++ 中的 time_t 和相关函数来获取和处理时间。
上一篇:c++ http请求
下一篇:c++基础框架
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站