ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++时间戳与时间日期格式互相转换

2021-09-30 19:03:23  阅读:282  来源: 互联网

标签:int C++ char tm 时间 time 格式 pStdTimeFormat localtime


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(&ltime);
	//ltime += 8*3600; //北京时区
	tm* gmt = gmtime(&ltime);
	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;
}


标签:int,C++,char,tm,时间,time,格式,pStdTimeFormat,localtime
来源: https://blog.csdn.net/qikaihuting/article/details/110565051

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有