您的位置:首页 > 编程语言 > C语言/C++

localtime和gmtime函数使用

2016-04-17 12:38 453 查看
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
time_t timep;
struct tm *tm_local,*tm_gmt;

//get calendar time
time(&timep);
printf("%d\n",timep );

//convert to local time
tm_local=localtime(&timep);

//convert to gmt time
tm_gmt=gmtime(&timep);

printf("%s\n", asctime(tm_local));
printf("%s\n", asctime(tm_gmt));
printf("tmlocal point to 0X%x\n",tm_local);
printf("tmgmt point to 0X%x\n",tm_gmt);

return 0;
}


大致介绍一下

先获取日历时间(从1970到现在的走的秒数)

然后将这个时间转换为本地时间和格林威治时间

然后打印两个时间



打印的时间是一样的

why?打印两个指针指向的内存地址,发现也是一样的,也就是说他俩用的一块内存。那么他是如何实现的呢?



Return Value

A pointer to a tm structure with the time information filled in.

This structure is statically allocated and shared by the functions gmtime and localtime. Each time either one of these functions is called the contents of this structure is overwritten.

“`

意思就是这块内存是静态分配的,两个函数共享使用。

那么他是在那里得到分配呢?

全局变量还是其它?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C语言