C++时间戳与时间日期格式互相转换
2021/9/30 20:11:20
本文主要是介绍C++时间戳与时间日期格式互相转换,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
C语言实现时间转换
- localtime、localtime_s、localtime_r都是用于获取系统时间;
- localtime_r用于Linux平台下获取系统时间,
- localtime_s用于Windows平台获取系统时间
显示当地日期与时间主要通过localtime()
函数来实现,该函数的原型在time.h
头文件中,其语法格式如下:
struct tm *localtime(const time_t *timep)
该函数的作用是把timep所指的时间(如函数time返回的时间)转换为当地标准时间,并以tm结构形式返回。其中,参数timer为主要获取当前时间的传递参数,格式为time_t指针类型。
//把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,而gmtimes函数转换后的时间没有经过时区变换,是UTC时间 //windows vs2010++ errno_t localtime_s( struct tm* _tm, //时间结构体的指针 const time_t *time //存储时间变量 ); struct tm{ int tm_sec, //几秒 (0-59)。 int tm_min, //分钟 (0-59)。 int tm_hour, //小时 (0-23) int tm_mday, //月 (1-31) 天 int tm_mon, // 月 (0 – 11) int tm_year,// 年份 (当前年份减去1970年或1900) int tm_wday,// 星期几 (0 – 6;星期日 = 0 int tm_yday, // (0-365) int tm_isdst, }
如果成功,返回值则为零。 如果失败,返回值将是错误代码。 错误代码是在 Errno.h 中定义的。
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h> #include <cstring> #include <time.h> /**************** // system local timeStamp convert to stardard time format // CST可以同时表示美国,澳大利亚,中国,古巴四个国家的标准时间 *******/ void getCSTTimeFormat(char* pStdTimeFormat) { time_t nTimeStamp; time(&nTimeStamp); char pTmpString[256] = {0}; tm *pTmStruct = localtime(&nTimeStamp); sprintf(pTmpString, "%04d-%02d-%02d %02d:%02d:%02d", pTmStruct->tm_year + 1900, pTmStruct->tm_mon + 1, pTmStruct->tm_mday, \ pTmStruct->tm_hour, pTmStruct->tm_min, pTmStruct->tm_sec); strcpy(pStdTimeFormat, pTmpString); return; } /**************** // GMT(Greenwich Mean Time)代表格林尼治标准时间 // convert to stardard time format *******/ void getGMTTimeFormat(char* pStdTimeFormat) { time_t ltime; time(<ime); //ltime += 8*3600; //北京时区 tm* gmt = gmtime(<ime); char s[128] = { 0 }; strftime(s, 80, "%Y-%m-%d %H:%M:%S", gmt); strcpy(pStdTimeFormat, s); // char* asctime_remove_nl = asctime(gmt); // asctime_remove_nl[24] = 0; // std::cout << std::string("Date: ") + asctime_remove_nl + " GMT" << std::endl; return; } /**************** // timeStamp convert to stardard time format *******/ void timeFormat2Timestamp(const char* strTimeFormat, time_t& timeStamp) { // strTimeFormat should be such as "2001-11-12 18:31:01" struct tm *timeinfo; memset( timeinfo, 0, sizeof(struct tm)); strptime(strTimeFormat, "%Y-%m-%d %H:%M:%S", timeinfo); //strptime("1970:01:01 08:00:00", "%Y:%m:%d %H:%M:%S", timeinfo); timeStamp = mktime(timeinfo); return; } int main(int argc, char** argv) { char s[128] = { 0 }; getGMTTimeFormat(s); printf("%s\n", s); char pStdTimeFormat[128] = { 0 }; getCSTTimeFormat(pStdTimeFormat); printf("%s\n", pStdTimeFormat); time_t timeStamp; timeFormat2Timestamp(s, timeStamp); std::cout << timeStamp << std::endl; printf("return code is %d \ngenerate done, enter any key to quit! ", 0); //getchar(); return 0; }
- https://blog.csdn.net/u010087712/article/details/50731222
这篇关于C++时间戳与时间日期格式互相转换的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-23AI大模型企业应用实战(14)-langchain的Embedding
- 2024-06-23AI大模型企业应用实战(15)-langchain核心组件
- 2024-06-23AI大模型企业应用实战(16)-langchain核心组件
- 2024-06-23AI 大模型企业应用实战(06)-初识LangChain
- 2024-06-19EntBot.ai: AI Website Chatbot for Product Guides and Development Doc
- 2024-06-17zero-shot-learning-definition-examples-comparison
- 2024-06-06Package Easy(基于 NSIS 的打包exe安装包工具)使用方法-icode9专业技术文章分享
- 2024-06-06基于 casdoor 的 ELK 开源登录认证解决方案: elk-auth-casdoor-icode9专业技术文章分享
- 2024-05-29Elasticsearch慢查询日志配置
- 2024-05-29揭秘华为如此多成功项目的产品关键——Charter模板