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;
}
最后出来的结果是:
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的更多相关文章
- 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> ...
- 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都自己记录了编辑时间.但是,云平台记录时需要把这个时间转成标准时间,便于管理.但是用户使用的时 ...
随机推荐
- 原生js在IE7下 向dom添加节点的一个bug, (本例为添加hidden input)
需求是要用js向dom结构增加1个hidden用来存放要post到服务器的数据 var aspnetForm = document.getElementById("aspnetForm&qu ...
- MVC+EF 随笔小计————Html Helpers
理论基础 -- Html Helpers 主要分成输入类和显示类. 输入类: TextArea, TextBox Password Hidden DropDownList ListBox (与Drop ...
- MVC-各种传值方式
[转自]:QLeelulu示例一:ViewData传值.HomeController.cs Co de: public ActionResult Index(){ ViewData[" ...
- SDC(4)–set_clock_groups 与–add选项
1,set_clock_groups -exclusive 有多个时钟,但是多个时钟不会同时生效 例如: 2,-add 只有一个时钟输入源,但是始终的频率等可能变 例如:
- ubuntu - sudo in php exec
最近写防火墙的WEB版,需要在PHP中调用linux系统命令,但是防火墙有关的执行都需要管理员权限才能执行. 在ubuntu下,Apache2的运行账户默认是www-data,默认是不能通过sudo来 ...
- Automotive Security的一些资料和心得(6):AUTOSAR
1.1 Introduction AUTOSAR(汽车开放系统架构)是一个开放的,标准化的汽车软件架构,由汽车制造商,供应商和开发工具共同开发.它联合了汽车OEM ,供应商和开发工具供应商,其目标是创 ...
- RPM包制作最简单样例
相关开发RPM的包要安装 Summary: the Firt RPM of Sky Name: hellow Version: 0.1 Release: Vendor: PA soft(aguncn@ ...
- Loadrunner负载机agent
记录下来备用,若要一台服务器充当负载机,windows下必须启动magentproc.exe
- 禁用nginx的access日志
修改nginx.conf 找到access_log: access_log /dev/null; 或者access_log off
- 获取GridView的BoundField值
我们在使用 GridView 控件展示列表时,如果需要获取 BoundField 的值,可以使用 GridView1.Rows[i].Cells[j].Text. 举个例子: 前台: <html ...