#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的更多相关文章

  1. localtime和localtime_r

    上程序: #include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h ...

  2. libc中的标准函数 localtime和localtime_r 的用法

    http://baike.baidu.com/view/1080853.htm 随便一查,就可以查到基本用法,但是... http://blog.csdn.net/maocl1983/article/ ...

  3. localtime 和 localtime_r 的区别

    转自:http://blog.csdn.net/maocl1983/article/details/6221810 #include <cstdlib> #include <iost ...

  4. localtime、localtime_s、localtime_r的使用

    (1).localtime用来获取系统时间,精度为秒 #include <stdio.h>#include <time.h>int main(){    time_t time ...

  5. localtime死锁——多线程下fork子进程

    近期測试我们自己改进的redis,发如今做rdb时,子进程会一直hang住.gdb attach上.堆栈例如以下: (gdb) bt #0 0x0000003f6d4f805e in __lll_lo ...

  6. C++中的时间函数

    C++获取时间函数众多,何时该用什么函数,拿到的是什么时间?该怎么用?很多人都会混淆. 本文是本人经历了几款游戏客户端和服务器开发后,对游戏中时间获取的一点总结. 最早学习游戏客户端时,为了获取最精确 ...

  7. Visual studio 2015程序转Eclipse gun编译出现的问题总结

    Visual studio 2015程序转Eclipse gun编译出现的问题总结 1.C++11支持 1)Project settings project右键-> c/c++ build -& ...

  8. Linux下用C获取当前时间

    Linux下用C获取当前时间,具体如下: 代码(可以把clock_gettime换成time(NULL)) ? 1 2 3 4 5 6 7 8 9 10 void getNowTime() {  ti ...

  9. Linux系统中时间区域和API

    1.问题 在开发云平台程序的时候,经常会碰到时间区域转换的问题.比如,任何网络存储的文档的metadata都自己记录了编辑时间.但是,云平台记录时需要把这个时间转成标准时间,便于管理.但是用户使用的时 ...

随机推荐

  1. Ubuntu x86-64汇编(6)

    寻址方式 Addressing Modes 地址和值 Addresses vs Values 在64bit架构中, 地址是64bit. 访问内存内容的唯一方式就是通过方括号, 不加方括号读取的只是地址 ...

  2. linux c server and client 简单的通信

    server.c #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <st ...

  3. 导致线程死锁容易忽略的一点 SendMessage

    假如主线程 某一个按钮 点击的 响应要操作与另一个线程共享的 变量. 在这个点击响应里先lock 之后,假如另一个线程的变量正在 “使用”状态,并且内部又调用了SendMessage试图更新界面的某些 ...

  4. 【CI】CN.一种多尺度协同变异的微粒群优化算法

    [论文标题]一种多尺度协同变异的微粒群优化算法 (2010) [论文作者]陶新民,刘福荣, 刘  玉 , 童智靖 [论文链接]Paper(14-pages // Single column) [摘要] ...

  5. 树莓派进阶之路 (020) - 基于24位AD转换模块HX711的重量称量实验

    参考文档:http://www.geek-workshop.com/thread-2315-1-1.html 参考文档:https://wenku.baidu.com/view/e5d5e4e2652 ...

  6. VC对话框使用OnEraseBkgnd函数位图背景并透明

    1.使用OnEraseBkgnd函数实现对话框位图背景 BOOL CDisplayBmpBackGroundDlg::OnEraseBkgnd(CDC *pDC) { CRect rect; GetC ...

  7. SpringMVC整合Mongodb开发,高级操作

    开发环境: 操作系统:windows xpMongodb:2.0.6依 赖 包:Spring3.2.2 + spring-data-mongodb-1.3.0 + Spring-data-1.5 +  ...

  8. MySQL的timestamp类型自动更新问题

    今天建了一个表,里面有一个列是timestamp类型,我本意是在数据更新时,这个字段的时间能自动更新.岂知对这个类型的值还不甚了解,导致出错.发现这个字段只是在这行数据建立的时候有值,在更新的却无变化 ...

  9. gitlab hook declined错误

    在向gitlab提交工程的时候,出现错误提示: remote: GitLab: You are not allowed to access master!remote: error: hook dec ...

  10. Easyui combobox 始终选择第一个的问题

    //必须指定 id 和 text $('#contact_city').combobox({ valueField:'id', textField:'text', });