localtime 和 localtime_r 的区别
转自:http://blog.csdn.net/maocl1983/article/details/6221810
#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;
} 最后出来的结果是::: :: 和最初想法不一致。查阅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;
} 最后出来的结果是::: ::
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
#include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h> ...
- Linux下用C获取当前时间
Linux下用C获取当前时间,具体如下: 代码(可以把clock_gettime换成time(NULL)) ? 1 2 3 4 5 6 7 8 9 10 void getNowTime() { ti ...
- Linux获取当前时间
代码(可以把clock_gettime换成time(NULL)) void getNowTime() { timespec time; clock_gettime(CLOCK_REALTIME, &a ...
- 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程序中对时间的处理——time库函数详解
包含文件:<sys/time.h> <time.h> 一.在C语言中有time_t, tm, timeval等几种类型的时间 1.time_t time_t实际上是长整数类型, ...
- Linux系统中时间区域和API
1.问题 在开发云平台程序的时候,经常会碰到时间区域转换的问题.比如,任何网络存储的文档的metadata都自己记录了编辑时间.但是,云平台记录时需要把这个时间转成标准时间,便于管理.但是用户使用的时 ...
随机推荐
- PyQt4 调用串口API pySerial API说明
pySerial API官方介绍链接 http://pyserial.readthedocs.io/en/latest/pyserial_api.html
- 剑指offer 面试56题
面试56题: 题目:数组中数字出现的次数 题:一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 解题思路: 方法一:异或运算,详见<剑指offer&g ...
- IT basic knowledge
LAMP: Linux Apache MySQL PHP URL & URI: 统一资源定位符是统一资源标志符的一个下种.统一资源标志符确定一个资源,而统一资源定位符不但确定一个资源, 而且还 ...
- 用数据池来实现socket并发
最终目标:启动服务后可以有无数个访问,并且可以随时输入,服务端使用进程池. 服务端 from socket import * import os,time from concurrent.future ...
- PAT 天梯赛 L1-030. 一帮一 【STL】
题目链接 https://www.patest.cn/contests/gplt/L1-030 思路 用三个 Vector 来分别存放 整个排名,以及男生的单独排名和女生的单独排名 从整个的排名 从上 ...
- Android 上Camera分析
http://blog.csdn.net/u010503912/article/details/52315721 Android Camera 系统架构源码分析(1)---->Camera的初始 ...
- lvds(800*600)
static struct fb_videomode ldb_modedb[] = { 107 107 { 108 + "LDB-SGA", 60, 800, 600, 25132 ...
- Go CSP模型
CSP 是 Communicating Sequential Process 的简称,中文可以叫做通信顺序进程,是一种并发编程模型,由 Tony Hoare 于 1977 年提出.简单来说,CSP 模 ...
- log4j2.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!--日志级别以及优先级排序: OFF > FATAL ...
- linux 上传scp 压缩tar命令
1.Linux 上传scp 1)上传文件与文件夹 scp file weblogic@xx.xxx.xxx.xxx:~/songjd/ scp -r filefolder weblogic@xxx.x ...