C++实现时间转换及格式化

 更新时间:2023年11月22日 09:21:49   作者:紫云星  
这篇文章主要为大家详细介绍了C++中实现时间转换及格式化的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下

写了一些时间转换及格式化相关的函数,经测试能够跨平台使用,记录一下。

#include <cstdio>
#include <chrono>
#include <iostream>
 
 
using namespace std;
 
time_t GetTime()
{
	chrono::system_clock::time_point now = chrono::system_clock::now();
	return chrono::system_clock::to_time_t(now);
}
tm* GetUtcTm()
{
	time_t t = GetTime();
	return gmtime(&t);
}
tm* GetLocalTm()
{
	time_t t = GetTime();
	return localtime(&t);
}
string GetUtcDateTime()
{
	auto t = GetTime();
	auto localTm = gmtime(&t);
	char buff[32];
	strftime(buff, 32, "%Y%m%d-%H:%M:%S", localTm);
	return string(buff);
}
string GetUtcDate()
{
	auto t = GetTime();
	auto localTm = gmtime(&t);
	char buff[32];
	strftime(buff, 32, "%Y%m%d", localTm);
	return string(buff);
}
string GetUtcTime()
{
	auto t = GetTime();
	auto localTm = gmtime(&t);
	char buff[32];
	strftime(buff, 32, "%H:%M:%S", localTm);
	return string(buff);
}
string GetUtcDateTimeWithMilliSecond()
{
	auto now = chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now());
	time_t t = chrono::system_clock::to_time_t(now);
	int milliSecond = now.time_since_epoch().count() % 1000;
	auto localTm = gmtime(&t);
	char buff[32];
	int len = strftime(buff, 32, "%Y%m%d-%H:%M:%S", localTm);
	sprintf(buff + len, ".%03u", milliSecond);
	return string(buff);
}
string GetLocalDateTime()
{
	auto t = GetTime();
	auto localTm = localtime(&t);
	char buff[32];
	strftime(buff, 32, "%Y%m%d-%H:%M:%S", localTm);
	return string(buff);
}
string GetLocalDate()
{
	auto t = GetTime();
	auto localTm = localtime(&t);
	char buff[32];
	strftime(buff, 32, "%Y%m%d", localTm);
	return string(buff);
}
string GetLocalTime()
{
	auto t = GetTime();
	auto localTm = localtime(&t);
	char buff[32];
	strftime(buff, 32, "%H:%M:%S", localTm);
	return string(buff);
}
string GetLocalDateTimeWithMilliSecond()
{
	auto now = chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now());
	time_t t = chrono::system_clock::to_time_t(now);
	int milliSecond = now.time_since_epoch().count() % 1000;
	auto localTm = localtime(&t);
	char buff[32];
	int len = strftime(buff, 32, "%Y%m%d-%H:%M:%S", localTm);
	sprintf(buff + len, ".%03u", milliSecond);
	return string(buff);
}
 
string GetLocalDateFromUnixTimeStamp(long long timeStamp)
{
	time_t time = timeStamp / 1000000000LL;
	static char buff[16];
	int len = strftime(buff, 16, "%Y%m%d", localtime(&time));
	return string(buff);
}
string GetLocalTimeFromUnixTimeStamp(long long timeStamp)
{
	time_t time = timeStamp / 1000000000LL;
	static char buff[16];
	int len = strftime(buff, 16, "%H:%M:%S", localtime(&time));
	return string(buff);
}
 
time_t GetTimeFromString(string dateTime, string format = "%04d%02d%02d-%02d:%02d:%02d")
{
	tm t;
	int len = sscanf(dateTime.c_str(), format.c_str(), &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec);
	t.tm_year -= 1900;
    t.tm_mon -= 1;
 
	return mktime(&t);
}
string ToUtcDateTime(time_t* time)
{
	char buff[32];
	strftime(buff, 32, "%Y%m%d-%H:%M:%S", gmtime(time));
	return string(buff);
}
string ToUtcDate(time_t* time)
{
	char buff[32];
	strftime(buff, 32, "%Y%m%d", gmtime(time));
	return string(buff);
}
string ToUtcTime(time_t* time)
{
	char buff[32];
	strftime(buff, 32, "%H:%M:%S", gmtime(time));
	return string(buff);
}
string ToLocalDateTime(time_t* time)
{
	char buff[32];
	strftime(buff, 32, "%Y%m%d-%H:%M:%S", localtime(time));
	return string(buff);
}
string ToLocalDate(time_t* time)
{
	char buff[32];
	strftime(buff, 32, "%Y%m%d", localtime(time));
	return string(buff);
}
string ToLocalTime(time_t* time)
{
	char buff[32];
	strftime(buff, 32, "%H:%M:%S", localtime(time));
	return string(buff);
}
 
int main()
{
	char buff[32];
	strftime(buff, 32, "%Y%m%d %H:%M:%S", GetUtcTm());
	cout << buff << endl;
	strftime(buff, 32, "%Y%m%d %H:%M:%S", GetLocalTm());
	cout << buff << endl << endl;
 
    cout << GetUtcDateTime() << endl;
    cout << GetUtcDate() << endl;
    cout << GetUtcTime() << endl;
    cout << GetUtcDateTimeWithMilliSecond() << endl << endl;
 
    cout << GetLocalDateTime() << endl;
    cout << GetLocalDate() << endl;
    cout << GetLocalTime() << endl;
    cout << GetLocalDateTimeWithMilliSecond() << endl << endl;
 
    cout << GetLocalDateFromUnixTimeStamp(1635754321199409000L) << endl;
    cout << GetLocalTimeFromUnixTimeStamp(1635754321199409000L) << endl << endl;
 
    auto time = GetTimeFromString("20211101-08:12:01.224");
    cout << ToUtcDateTime(&time) << endl;
    cout << ToUtcDate(&time) << endl;
    cout << ToUtcTime(&time) << endl;
    cout << ToLocalDateTime(&time) << endl;
    cout << ToLocalDate(&time) << endl;
    cout << ToLocalTime(&time) << endl << endl;
 
 
    return 0;
}

输出:

20211103 05:46:16
20211103 13:46:16
 
20211103-05:46:16
20211103
05:46:16
20211103-05:46:16.218
 
20211103-13:46:16
20211103
13:46:16
20211103-13:46:16.219
 
20211101
16:12:01
 
20211101-00:12:01
20211101
00:12:01
20211101-08:12:01
20211101
08:12:01

到此这篇关于C++实现时间转换及格式化的文章就介绍到这了,更多相关C++时间转换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C++常用的#include头文件总结

    C++常用的#include头文件总结

    这篇文章主要介绍了C++常用的#include头文件,对初学者理解C++程序设计大有好处的相关资料
    2014-07-07
  • C++ 命名空间详解

    C++ 命名空间详解

    这篇文章主要介绍了C++ 命名空间的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下
    2021-11-11
  • C++实现简单版通讯录管理系统

    C++实现简单版通讯录管理系统

    这篇文章主要为大家详细介绍了C++实现简单版通讯录管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • c语言中使用BF-KMP算法实例

    c语言中使用BF-KMP算法实例

    这篇文章主要介绍了c语言中使用BF-KMP算法,大家参考使用
    2013-11-11
  • C++常见异常处理原理及代码示例解析

    C++常见异常处理原理及代码示例解析

    这篇文章主要介绍了C++常见异常处理原理及代码示例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • Cocos2d-x中获取系统时间和随机数实例

    Cocos2d-x中获取系统时间和随机数实例

    这篇文章主要介绍了Cocos2d-x中获取系统时间和随机数实例,本文代码含有大量注释来讲解获取系统时间和随机数的方法,需要的朋友可以参考下
    2014-09-09
  • 深入解析C++中的虚函数与多态

    深入解析C++中的虚函数与多态

    对C++ 了解的人都应该知道虚函数(Virtual Function)是通过一张虚函数表(Virtual Table)和一个指向虚函数表的指针(vptr)来实现的
    2013-09-09
  • C++中LibCurl库的使用教程分享

    C++中LibCurl库的使用教程分享

    LibCurl是一个开源的免费的多协议数据传输开源库,该框架具备跨平台性,开源免费,这篇文章主要为大家介绍了如何在C++中使用LibCurl库,需要的可以参考下
    2023-08-08
  • 纯C++实现PP-OCRv5文字识别的全流程

    纯C++实现PP-OCRv5文字识别的全流程

    你是不是也遇到过这种情况:想在C++项目里加个OCR功能,结果光装OpenCV就折腾半天?今天教你零OpenCV依赖,用Paddle Inference + stb_image,纯C++实现PP-OCRv5文字识别全流程(检测+识别),代码可直接跑,需要的朋友可以参考下
    2026-02-02
  • c++使用regex报错regex_error两种解决方案

    c++使用regex报错regex_error两种解决方案

    C++正则表达式是一个非常强大和实用的工具,但是使用它们时需要注意仔细检查代码是否符合语法规则,这篇文章主要给大家介绍了关于c++使用regex报错regex_error的两种解决方案,需要的朋友可以参考下
    2024-03-03

最新评论