linux系统对时间函数的操作需要注意的问题
2017-06-23 09:40
369 查看
简单点说,就是,在使用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()返回的指针所指向的对象也发生改变。
相关文章推荐
- Hibernate操作时间需要注意的问题
- 关于php的时间相关函数需要注意时区问题
- Hibernate操作时间需要注意的问题(转)
- 用异或操作实现的交换函数用以实现数组逆置中需要注意的问题
- inet_addr函数处理IP地址需要注意的问题 (转)
- 更改文件时间属性需要注意的问题
- Qt 多线程与数据库操作需要注意的几点问题
- C++中派生类重写基类重载函数时需要注意的问题:派生类函数屏蔽基类中同名函数
- inet_addr函数处理IP地址需要注意的问题
- scanf()函数--需要注意的问题2
- 实例解析使用指针作为函数参数传递需要注意的问题
- linux系统装windows时需要注意的问题
- 关于时间函数localtime()返回值的注意问题
- C# 调用 C++ dll 函数 时传递字符串 需要注意的问题
- JS代码获取当前日期时支持IE,不兼容FF和chrome,解决这个问题,我们需要把获取时间的getYear()函数换成getFullYear()
- 自定义排序函数实现时需要注意的问题
- 关于操作主机角色FSMO需要注意的问题
- C++学习总结_成员函数的声明和实现需要注意问题
- C++指针作为函数的参数进行传递时需要注意的一些问题