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

GetCurrentTime

在实际编程中难免要获取当前时间并且进行格式化,本文给出了多种 GetCurrentTime() 方法以供选择。

C语言下使用strftime

C 语言中可以使用 <time.h> 中的函数来获取和格式化时间

#include <stdio.h>
#include <time.h>

char* getCurrentTime() {
    static char buffer[100];
    time_t now_time = time(NULL);
    struct tm* local_time = localtime(&now_time);
    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", local_time);
    return buffer;
}

int main() {
    printf("Current Time: %s\n", getCurrentTime());
    return 0;
}

优点
• 简单直接,兼容性好。
• 无需额外依赖。

缺点
• 需要手动管理缓冲区。

C++11 方法

C++11 引入了 <chrono><iomanip>,提供了更现代化的时间处理方式。

#include <iostream>
#include <iomanip>
#include <sstream>
#include <chrono>
#include <ctime>

std::string getCurrentTime() {
    auto now = std::chrono::system_clock::now();
    std::time_t now_time = std::chrono::system_clock::to_time_t(now);
    std::tm local_time = *std::localtime(&now_time);

    std::ostringstream oss;
    oss << std::put_time(&local_time, "%Y-%m-%d %H:%M:%S");
    return oss.str();
}

int main() {
    std::cout << "Current Time: " << getCurrentTime() << std::endl;
    return 0;
}

优点
• 类型安全,无需手动管理缓冲区。
• 使用标准库函数,代码更简洁。

缺点
• 代码稍显冗长。

c++20

C++20 引入了 std::formatstd::chrono::format,提供了更简洁的格式化方式。

#include <iostream>
#include <chrono>
#include <format>

std::string getCurrentTime() {
    auto now = std::chrono::system_clock::now();
    return std::format("{:%Y-%m-%d %H:%M:%S}", now);
}

int main() {
    std::cout << "Current Time: " << getCurrentTime() << std::endl;
    return 0;
}

优点
• 代码简洁,现代化。
• 支持多种格式化方式。

缺点
• 需要 C++20 支持。

使用第三方库fmt

fmt 是一个功能强大的格式化库,支持 C++11 及以上版本。

#include <iostream>
#include <chrono>
#include <fmt/core.h>
#include <fmt/chrono.h>

std::string getCurrentTime() {
    auto now = std::chrono::system_clock::now();
    return fmt::format("{:%Y-%m-%d %H:%M:%S}", now);
}

int main() {
    std::cout << "Current Time: " << getCurrentTime() << std::endl;
    return 0;
}

优点
• 功能强大,支持 C++11 及以上版本。
• 代码简洁,类似于 C++20 的 std::format

缺点
• 需要引入第三方库。

使用 Boost 库

Boost 是一个功能丰富的 C++ 库,提供了时间处理工具。

#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>

std::string getCurrentTime() {
    boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
    return boost::posix_time::to_simple_string(now);
}

int main() {
    std::cout << "Current Time: " << getCurrentTime() << std::endl;
    return 0;
}

优点
• 功能强大,支持多种时间操作。

缺点
• 需要引入 Boost 库。

**

方法优点缺点
C 语言 strftime简单直接,兼容性好需要手动管理缓冲区
C++11 std::put_time类型安全,无需额外依赖代码稍显冗长
C++20 std::format简洁、现代化,支持多种格式化方式需要 C++20 支持
fmt功能强大,支持 C++11 及以上版本,类似 C++20需要引入第三方库
Boost 库功能丰富,支持多种时间操作需要引入 Boost 库

根据你的项目需求和编译器支持情况,选择最适合的方案。如果可以使用 C++20,std::format 是最佳选择;否则,std::put_timefmt 库都是很好的替代方案。


http://www.kler.cn/a/587271.html

相关文章:

  • 25年教师资格认定材料及认定详细流程‼
  • centos Supported Java versions are: [17, 21]
  • 【数据分析大屏】基于Django+Vue汽车销售数据分析可视化大屏(完整系统源码+数据库+开发笔记+详细部署教程+虚拟机分布式启动教程)✅
  • 《灵珠觉醒:从零到算法金仙的C++修炼》卷三·天劫试炼(55)聚宝盆装区间 - 合并区间(排序贪心)
  • 【Erdas实验教程】015:哨兵二号卫星数据简介及下载方法
  • PyTorch系列教程:基于LSTM构建情感分析模型
  • Spring Boot应用首次请求性能优化实战:从数据库连接池到JVM调优
  • OSPF | LSDB 链路状态数据库 / SPF 算法 / 实验
  • 爬虫逆向:Hook 技术原理与实战
  • Java 学习记录:基础到进阶之路(二)
  • Mac中nvm切换node版本失败,关闭终端再次打开还是之前的node
  • Rubick:基于 Electron 的开源插件化桌面效率工具箱
  • C++之list类(超详细)
  • Cursor的使用感受,帮你使用好自动化编程工具,整理笔记
  • Unity开发——点击事件/射线检测
  • LeetCode 热题 100_前 K 个高频元素(73_347_中等_C++)(堆)(哈希表+排序;哈希表+优先队列(小根堆))
  • 【机器人】复现 ASGrasp 通用透明物体重建、6-DoF抓取预测
  • 基于VM的CentOS 7.4系统安装与配置说明系统环境主机系统
  • windows,修改svn密码之后,没法认证
  • Gemini 2.0 Flash:AI 图像生成的革命性突破!