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都自己记录了编辑时间.但是,云平台记录时需要把这个时间转成标准时间,便于管理.但是用户使用的时 ...
随机推荐
- MySQL basics
@1: MySQL有三大类数据类型, 分别为数字.日期\时间.字符串, 这三大类中又更细致的划分了许多子类型: 数字类型 整数: tinyint.smallint.mediumint.int.bigi ...
- Python基础(16)_面向对象程序设计(类、继承、派生、组合、接口)
一.面向过程程序设计与面向对象程序设计 面向过程的程序设计:核心是过程,过程就解决问题的步骤,基于该思想设计程序就像是在设计一条流水线,是一种机械式的思维方式 优点:复杂的问题的简单化,流程化 缺点: ...
- position:absolute width
position:absolute; left:0px; right:0px; top:0px; bottom:0px; 设置布满整个父范围,设置了absolute的元素可以通过以上4个属性来展开面, ...
- Ubuntu 16.04 php卸载
1.卸载 apache2 sudo apt-get --purge remove apache2* sudo apt-get autoremove apache2 (--purge 是完全删除并且不保 ...
- windows通过ftp下载linux文件
# windows 下载 linux的文件>> ftp <domain_or_ip>>> <input_username>>> <in ...
- hostname -f 失败解决办法
$ hostname fzk $ uname -n fzk 当 hostname -f 时报错:未搜索到主机名 产生这个原因时因为 /etc/hosts和/etc/sysconfig/network ...
- PHPExcel常用属性使用
PHPExcel常用属性使用 前景: 需先实例化几个变量: $this->objExcel = new PHPExcel(); //实例化一个PHPExcel变量 $this->objE ...
- 20165101刘天野 2017-2018-2 《Java程序设计》第4周学习总结
#20165101刘天野 2017-2018-2 <Java程序设计>第4周学习总结 教材学习内容总结 第五章:子类与继承 面向对象程序设计语言有三大特性:封装.继承和多态性.继承是面向对 ...
- iOS_KVC与KVO
目 录: 一.KVC 二.KVO ...
- Go 文件操作
一.读取文件 普通版 ioutil版 bufio版 二.文件写入 普通版 ioutil版 bufio版 三.文件复制 ioCopy 1.普通版读取文件 package main import ( &q ...