linux系统对时间函数的操作需要注意的问题
来源:互联网 发布:nosql数据库入门 pdf 编辑:程序博客网 时间:2024/06/07 07:28
简单点说,就是,在使用gmtime() 和 localtime()操作的时候,不能多次调用这些对struct tm结构体操作的函数,他们不是线程安全的。这些函数返回的指针实际上是指向同一个变量,多次操作会改变对应的struct tm的结构体中的数据。详见man gmtime
程序的输出:
process begin at [0x400594]
current time is [1498122880]
address of struct tm *p_stm is [0x7fffb9ebfbb0]. pointer to object is [0x390f193420]
struct tm *p_stm is [2017-06-22 09:14:40] time_zone is [GMT]
address of struct tm *p_stm2 is [0x7fffb9ebfba8]. pointer to object is [0x390f193420]
struct tm *p_stm2 is [2017-06-22 17:14:40] time_zone is [CST]
(*p_stm) after calling localtime() is [2017-06-22 17:14:40] time_zone is [CST]
说明:
根据日志,可以看到,上述程序中的gmtime()和localtime()所返回的结构体的地址是同一个,也就是说,定义的两个指针实际上是指向的同一个对象。当使用localtime()修改了struct tm的结构体指针的时候,gmtime()返回的指针所指向的对象也发生改变。
程序举例
#include <stdio.h>#include <time.h>int main(int argc, char ** argv){printf("process begin at \t[%p]\n", (void*)&main);time_t currentTime;time(¤tTime);printf("current time is \t[%d]\n", currentTime);struct tm *p_stm = gmtime(¤tTime);printf("address of struct tm *p_stm is \t[%p]. pointer to object is \t[%p]\n", &p_stm, p_stm);printf("struct tm *p_stm is \t[%d-%.2d-%.2d %.2d:%.2d:%.2d] time_zone is [%s]\n", p_stm->tm_year+1900, p_stm->tm_mon+1, p_stm->tm_mday, p_stm->tm_hour,p_stm->tm_min, p_stm->tm_sec, p_stm->tm_zone);struct tm *p_stm2 = localtime(¤tTime);printf("address of struct tm *p_stm2 is \t[%p]. pointer to object is \t[%p]\n", &p_stm2, p_stm2);printf("struct tm *p_stm2 is \t[%d-%.2d-%.2d %.2d:%.2d:%.2d] time_zone is [%s]\n", p_stm2->tm_year+1900, p_stm2->tm_mon+1, p_stm2->tm_mday, p_stm2->tm_hour,p_stm2->tm_min, p_stm2->tm_sec, p_stm2->tm_zone);printf("(*p_stm) after calling localtime() is \t[%d-%.2d-%.2d %.2d:%.2d:%.2d] time_zone is [%s]\n", p_stm->tm_year+1900, p_stm->tm_mon+1, p_stm->tm_mday, p_stm->tm_hour,p_stm->tm_min, p_stm->tm_sec, p_stm->tm_zone);return 0;}
程序的输出:
process begin at [0x400594]
current time is [1498122880]
address of struct tm *p_stm is [0x7fffb9ebfbb0]. pointer to object is [0x390f193420]
struct tm *p_stm is [2017-06-22 09:14:40] time_zone is [GMT]
address of struct tm *p_stm2 is [0x7fffb9ebfba8]. pointer to object is [0x390f193420]
struct tm *p_stm2 is [2017-06-22 17:14:40] time_zone is [CST]
(*p_stm) after calling localtime() is [2017-06-22 17:14:40] time_zone is [CST]
说明:
根据日志,可以看到,上述程序中的gmtime()和localtime()所返回的结构体的地址是同一个,也就是说,定义的两个指针实际上是指向的同一个对象。当使用localtime()修改了struct tm的结构体指针的时候,gmtime()返回的指针所指向的对象也发生改变。
阅读全文
0 0
- linux系统对时间函数的操作需要注意的问题
- Hibernate操作时间需要注意的问题
- Hibernate操作时间需要注意的问题(转)
- 关于php的时间相关函数需要注意时区问题
- Linux对系统时间的操作
- LINUX、centos 系统安装需要注意的问题
- 逻辑操作需要注意的问题
- 64bit 系统需要注意的问题
- 使用系统照相机需要注意的问题
- scanf()函数--需要注意的问题2
- 使用header()函数需要注意的问题
- ASP.net c#下对Excel文件进行操作需要注意的问题
- pymongo 对游标进行for循环操作需要注意的问题
- 安装linux需要注意的问题
- linux自定义信号需要注意的问题
- Linux系统安全需要注意的一些问题
- 用异或操作实现的交换函数用以实现数组逆置中需要注意的问题
- 需要注意的问题
- SplitContainer 控件作用
- EF实现考勤管理---(MVC,签到签退)
- 递归求简单交错幂级数的部分和
- (4.2.39)内存泄漏检测LeakCanary源码分析
- FrameLayout 帧布局
- linux系统对时间函数的操作需要注意的问题
- ORACLE EXPDP ORA-31626 问题
- 浏览器访问网页的详细内部过程
- 虚函数和纯虚函数的区别
- IOS的GCD基本用法
- 1、(一)学习如何Debug
- DMABUF, DMA mapping,IOMMU的区别
- 简单的JS实现猜数游戏
- /bin/sh: 1: arm-v5t_le-gcc: not found Try 'dirname --help' for more information