在看这篇文章之前,先看看#include <ctime>的简介。
ctimehttps://blog.youkuaiyun.com/NOIP1ding_c/article/details/144276592
这篇文章我来给大家一个ctime的应用。
应用
#include <iostream>
#include <ctime>
#include <iomanip>
#include <sstream>
int main() {
// 获取当前时间
time_t now = time(NULL);
// 将当前时间转换为本地时间
struct tm *local_tm = localtime(&now);
// 使用 strftime 格式化时间
char buffer[80];
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", local_tm);
// 输出当前时间
std::cout << "Current local time: " << buffer << std::endl;
// 将当前时间转换为UTC时间
struct tm *utc_tm = gmtime(&now);
// 格式化UTC时间
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", utc_tm);
// 输出UTC时间
std::cout << "Current UTC time: " << buffer << std::endl;
return 0;
}