localtime 和 localtime_r
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <stdio.h> using namespace std; int main(int argc, char *argv[])
{
time_t tNow =time(NULL);
time_t tEnd = tNow + ;
//注意下面两行的区别
struct tm* ptm = localtime(&tNow);
struct tm* ptmEnd = localtime(&tEnd); char szTmp[] = {};
strftime(szTmp,,"%H:%M:%S",ptm);
char szEnd[] = {};
strftime(szEnd,,"%H:%M:%S",ptmEnd); printf("%s /n",szTmp);
printf("%s /n",szEnd); system("PAUSE");
return EXIT_SUCCESS;
}
最后出来的结果是:
16:49:49
16:49:49
和最初想法不一致。
查阅localtime的文档,发现这段话:
This structure is statically allocated and shared by the functions gmtime and localtime. Each time either one of these functions is called the content of this structure is overwritten.
也就是说每次只能同时使用localtime()函数一次,要不就会被重写!
The localtime() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe.
因此localtime()不是可重入的。同时libc里提供了一个可重入版的函数localtime_r();
Unlike localtime(), the reentrant version is not required to set tzname。
将location修改为location_r后,输出结果:
16:22:02
16:52:02
localtime是直接返回strcut tm*指针(如果成功的话);这个指针是指向一个静态变量的;因此,返回的指针所指向的静态变量有可能被其他地方调用的localtime改掉,例如多线程使用的时候。
localtime_r则是由调用者在第二个参数传入一个struct tm result指针,该函数会把结果填充到这个传入的指针所指内存里面;成功的返回值指针也就是struct tm result。
其他的时间函数,如asctime,asctime_r;ctime,ctime_r;gmtime,gmtime_r都是类似的,所以,时间函数的 _r 版本都是线程安全的。
localtime 和 localtime_r的更多相关文章
- localtime和localtime_r
上程序: #include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h ...
- libc中的标准函数 localtime和localtime_r 的用法
http://baike.baidu.com/view/1080853.htm 随便一查,就可以查到基本用法,但是... http://blog.csdn.net/maocl1983/article/ ...
- localtime 和 localtime_r 的区别
转自:http://blog.csdn.net/maocl1983/article/details/6221810 #include <cstdlib> #include <iost ...
- localtime、localtime_s、localtime_r的使用
(1).localtime用来获取系统时间,精度为秒 #include <stdio.h>#include <time.h>int main(){ time_t time ...
- localtime死锁——多线程下fork子进程
近期測试我们自己改进的redis,发如今做rdb时,子进程会一直hang住.gdb attach上.堆栈例如以下: (gdb) bt #0 0x0000003f6d4f805e in __lll_lo ...
- C++中的时间函数
C++获取时间函数众多,何时该用什么函数,拿到的是什么时间?该怎么用?很多人都会混淆. 本文是本人经历了几款游戏客户端和服务器开发后,对游戏中时间获取的一点总结. 最早学习游戏客户端时,为了获取最精确 ...
- Visual studio 2015程序转Eclipse gun编译出现的问题总结
Visual studio 2015程序转Eclipse gun编译出现的问题总结 1.C++11支持 1)Project settings project右键-> c/c++ build -& ...
- Linux下用C获取当前时间
Linux下用C获取当前时间,具体如下: 代码(可以把clock_gettime换成time(NULL)) ? 1 2 3 4 5 6 7 8 9 10 void getNowTime() { ti ...
- Linux系统中时间区域和API
1.问题 在开发云平台程序的时候,经常会碰到时间区域转换的问题.比如,任何网络存储的文档的metadata都自己记录了编辑时间.但是,云平台记录时需要把这个时间转成标准时间,便于管理.但是用户使用的时 ...
随机推荐
- 使用 ODP.NET 访问 Oracle(.net如何访问Oracle)详解
1,什么是ODF .NE,?就是Oracle 为 .NET (ODP.NET) 专门编写了 Oracle Data Provider,一个用于 Microsoft .NET 环境下的 Oracle 数 ...
- kubelet Pod status的状态分析
CrashLoopBackOff: 容器退出,kubelet正在将它重启 InvalidImageName: 无法解析镜像名称 ImageInspectError: 无法校验镜像 ErrImageNe ...
- SQL之group by 和 having
转自:mysql必知必会——GROUP BY和HAVING GROUP BY语法可以根据给定数据列的每个成员对查询结果进行分组统计,最终得到一个分组汇总表. select子句中的列名必须为分组列或列函 ...
- hihocoder第196周
此题解法:动态规划,倒骑毛驴. 在使用动态规划的时候,如果正着求难求,可以考虑倒着来. 这道题坑不少,自己代码能力太弱了,写代码的过程中总是容易犯细节错误.虽然大的方向是对的,但是小坑非常致命! 比如 ...
- java struts2入门学习---异常处理和类型转换
一.struts2对异常的处理 1.自定义局部异常: <action> <exception-mapping result="sonException" exce ...
- HDU 3999 The order of a Tree (先序遍历)
The order of a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 从前端的UI开始
MVC分离的比较好,开发顺序没有特别要求,先开发哪一部分都可以,这次我们主要讲解前端UI的部分. ASP.NET MVC抛弃了WebForm的一些特有的习惯,例如服务器端控件,ViewState这些东 ...
- 【HTML】网页中如何让DIV在网页滚动到特定位置时出现
用js或者jquery比较好实现.但你要知道,滚动到哪个特定位置,例如滚动到一个标题h3那显示这个div,那么可以用jquery算这个h3距离网页顶部的距离:$("h3").off ...
- 【C语言】练习3-3
题目来源:<The C programming language>中的习题P49 练习2-9: 编写函数expand(s1, s2),将字符串s1中类似于a-z一类的速记符号在字符串s ...
- 【Algorithm】九种常用排序的性能分析
最近间间断断的将9种排序算法用C实现,并且将其以博客笔记的形式记录下来,其中各个排序算法的描述部分特别参考了CSDN上太阳落雨的博客!现在就该来综合的分析这九种排序,让我们先来看看其算法复杂度和稳定性 ...