当前位置: 首页 > news >正文

C++打印当前时间

要在 C++ 中打印当前时间并格式化为 YYYY-MM-DD HH:MM:SS,你可以使用 <chrono>和 <iomanip> 头文件

#include <string>
#include <chrono>
#include <iomanip>
#include <sstream>// 获取当前时间点
auto now = std::chrono::system_clock::now();
// 将时间点转换为时间_t
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
// 使用 ostringstream 格式化时间并转换为 string
//创建一个输出字符串流对象
std::ostringstream oss;
//将格式化时间插入到 ostringstream 中
oss << std::put_time(std::localtime(&now_c), "%Y-%m-%d %H:%M:%S");
//将 ostringstream 内容转换为 std::string
std::string time_str = oss.str();
// 打印时间
LogUtil::info(TAG,("time_str = " + time_str).c_str());
  1. std::chrono::system_clock::now():获取当前系统时间点。
  2. std::chrono::system_clock::to_time_t(now):将系统时间点转换为 std::time_t 类型。
  3. std::localtime(&now_c):将 std::time_t 转换为本地时间结构体。
  4. std::put_time(…, “%Y-%m-%d %H:%M:%S”):格式化时间并打印为 YYYY-MM-DD HH:MM:SS。

http://www.mrgr.cn/news/12888.html

相关文章:

  • Git的初级学习
  • 精彩管道不会梦到深沉蓝调
  • 【目标检测】YOLOV3
  • CSS3视图过渡动画
  • 树莓派制成的 — 带运动检测和摄像头的安防系统
  • CUDA编程之CUDA Sample-5_Domain_Specific-volumeFiltering(光线追踪)
  • 数据库概述
  • 【Python】读取excel的不同工作簿的内容和获取工作簿的名称
  • 适合运动佩戴的蓝牙耳机推荐?四款开放式运动耳机推荐
  • 如何使用IDEA搭建Mybatis框架环境
  • 交通流量监测检测系统源码分享 # [一条龙教学YOLOV8标注好的数据集一键训练_70+全套改进创新点发刊_Web前端展示]
  • 【ansible】ansible roles
  • 【JS】使用MessageChannel实现深度克隆
  • 来自OpenAI官网的Function calling介绍与最佳实践
  • 为什么你的RAG不起作用?如何解决语义失调
  • 游戏开发设计模式之桥接模式
  • flutter路由之flutro配置
  • 【Linux篇】三分钟速通Linux基础命令
  • list底层详解
  • 初识C++(8.27)