上程序:

#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;
}

最后出来的结果是:

21:18:39

21:18:39

和最初想法不一致。

查阅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。

修改程序:

#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);
struct tm ptm = { };
struct tm ptmEnd = { };
localtime_r(&tNow, &ptm);
localtime_r(&tEnd, &ptmEnd); 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;
}

最后出来的结果是:

10:29:06 
10:59:06

localtime和localtime_r的更多相关文章

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

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

  2. localtime 和 localtime_r

    #include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h> ...

  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. Socket 入门

    int socket(int domain, int type,int protocol) domain :说明我们网络程序所在的主机采用的通讯协族(AF_UNIX和AF_INET等). AF_UNI ...

  2. OFBiz之SVN下载地址

    trunk: $ svn co http://svn.apache.org/repos/asf/ofbiz/trunk ofbiz release13.07: $ svn co http://svn. ...

  3. windows下使用eclipse调试C程序

    一.环境描述 Eclipse IDE for C/C++ Developers version 4.4.0 MinGW  gcc/g++ version 4.8.1;gdb version 7.6.1 ...

  4. JQ+AJAX实现多级联动

    利用JQ与AJAX实现三级联动实现的效果: 当前两级改变时,后边一级或两级都会改变: 使用的数据库: html代码: <!doctype html> <html lang=" ...

  5. poj 3415 Common Substrings(后缀数组+单调栈)

    http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Sub ...

  6. 李洪强漫谈iOS开发[C语言-023]-取余数运算符

  7. mysql select count(filed) 问题(where条件没有数据匹配的话也有数据返回)。

    问题: SELECT count(*),user_id FROM tb_rp_logintrace WHERE id=-1 返回结果: count(*), user_id 0             ...

  8. 学习笔记-[Maven实战]-第三章:Maven使用入门(1)

    说明:[Maven实战]一书还介绍了怎么样手工创建Maven工程,学习这本书是为了能尽快在工作中使用,就忽略了手工建工程的部分 如果想了解这部分的内容,可以自己看看书 开始: 1.新建一个maven工 ...

  9. 【HDOJ】2054 A == B ?

    这道题目起初看,so easy.再看一下ac率,注意到没有说明变量类型.显然是一道字符串的题.需要考虑+/-符号位,+.1.-.1.00010.0.+0.-00.00等情况,同时数组开到100000以 ...

  10. access to modified closure 闭包的问题

    ; i < listBoxDevices.Items.Count; i++) { var tempDeviceId = listBoxDevices.Items[i].ToString(); i ...