#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. A. Kyoya and Photobooks(Codeforces Round #309 (Div. 2))

    A. Kyoya and Photobooks   Kyoya Ootori is selling photobooks of the Ouran High School Host Club. He ...

  2. 收到新信息,弹出popup窗口提示

     

  3. java正则表达式去除html中所有的标签和特殊HTML字符(以&开头的)

    来源于:https://www.androiddev.net/java%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%8E%BB%E9%99%A4ht ...

  4. BigDecimal去除末尾多余的0

    Java有自带的 stripTrailingZeros() 方法用于去除末尾多余的0 BigDecimal num = new BigDecimal("100.000"); Big ...

  5. 阿里云ecs配置辅助网卡绑定公网ip地址

    EIP直通车 前置条件:1.大家的实例是从经典迁移到VPC里面的,上古时期,经典实例大家购买实例的时候都是买了带宽的.而这种带宽一般情况下都是包年包月的,而且这种绑定在实例上的IP,我们把它叫做公网I ...

  6. los中预览文件

    #import <UIKit/UIKit.h> #import <QuickLook/QuickLook.h> @interface ViewController : UIVi ...

  7. hihocoder234周 计算不包含黑点的矩形个数

    题目链接 问题描述 一个棋盘有n条横线,m条竖线,上面有k个黑点,问有多少个不包含黑点的矩形. 数据范围: n和m最大为1000,k最大为10 方法一:动态规划 复杂度n*m*k. import ja ...

  8. [转] mysql --prompt介绍

    mysql --prompt修改命令行链接mysql时的提示符,shell脚本示例如下 #!/bin/bash case $1 in crm) cmd='mysql -h192.168.1.2 -ur ...

  9. Yslow---一款很实用的web性能测试插件

    YSlow可以对网站的页面进行分析,并告诉你为了提高网站性能,如何基于某些规则而进行优化. YSlow可以分析任何网站,并为每一个规则产生一个整体报告,如果页面可以进行优化,则YSlow会列出具体的修 ...

  10. Mac MySQL 数据库管理(关系型数据库管理系统)

    1.管理准备工作 1)管理数据库准备工作 下载相关软件 mysql-workbench-community-6.3.10-macos-x86_64.dmg Oracle 官网 MySQL 官网 MyS ...