linux 中的gmtime和localtime函数

一、 gmtime和localtime前后使用会有影响

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

int main(int argc, char **argv)
{
 time_t now;
 struct tm *tmutc, *tmlocal;

 /*获取日历时间*/
 time(&now);

 /*转换成tm时间*/
 tmutc = gmtime(&now);

 tmlocal = localtime(&now);


 /*输出时间*/
 printf("%s标准时间为:\t%s", tmutc->tm_zone, asctime(tmutc));
 
 printf("%s时间为:\t%s", tmlocal->tm_zone, asctime(tmlocal));

 return 0;
}

*******************************************

CST标准时间为: Tue Jan 31 09:23:17 2012
CST时间为:     Tue Jan 31 09:23:17 2012

******************************************

二、 gmtime和localtime分开使用

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

int main(int argc, char **argv)
{
 time_t now;
 struct tm *tmutc, *tmlocal;

 /*获取日历时间*/
 time(&now);

 /*转换成tm时间*/
 tmutc = gmtime(&now); 

 /*输出时间*/
 printf("%s标准时间为:\t%s", tmutc->tm_zone, asctime(tmutc));

 tmlocal = localtime(&now);
 
 printf("%s时间为:\t%s", tmlocal->tm_zone, asctime(tmlocal));

 return 0;
}

*************************************************

GMT标准时间为: Tue Jan 31 01:24:40 2012
CST时间为:     Tue Jan 31 09:24:40 2012

************************************************

原文地址:https://www.cnblogs.com/Neddy/p/2332576.html