(原创)GMT时间转本地时间

代码直接上了,其中timezone是C runtime libary的全局变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
CString GMTToLocalTime(const char *pGMTTimeToLocalTime)
{
struct tm newtime;
CString szTime = "";
if (strlen(pGMTTimeToLocalTime) > 0)
{
int nYear,nMonth,nDay,nHour,nMinute,nSecode,nMs;
sscanf(pGMTTimeToLocalTime,"%04d-%02d-%02dT%02d:%02d:%02d.%dZ",
&nYear,&nMonth,&nDay,&nHour,&nMinute,&nSecode,&nMs);
newtime.tm_year = nYear - 1900;
newtime.tm_mon = nMonth - 1;
newtime.tm_mday = nDay;
newtime.tm_hour = nHour;
newtime.tm_min = nMinute;
newtime.tm_sec = nSecode;
newtime.tm_wday = 0;
newtime.tm_yday =0;
newtime.tm_isdst = 0;
long time = mktime(&newtime) - timezone;
struct tm *pnewtime;
pnewtime = localtime(&time);
if (NULL != pnewtime)
{
szTime.Format("%04d-%02d-%02dT%02d:%02d:%02d.%dZ",
pnewtime->tm_year + 1900 ,pnewtime->tm_mon + 1,
pnewtime->tm_mday,pnewtime->tm_hour,pnewtime->tm_min,pnewtime->tm_sec,nMs);
}
}
return szTime;
}

多谢打赏
-------------本文结束感谢您的阅读-------------
0%