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 + 1800;
    //注意下面两行的区别
    struct tm* ptm = localtime(&tNow);
    struct tm* ptmEnd = localtime(&tEnd);

    char szTmp[50] = {0};
    strftime(szTmp,50,"%H:%M:%S",ptm);
    char szEnd[50] = {0};
    strftime(szEnd,50,"%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 + 1800;

    //在这里修改程序
    //struct tm* ptm = localtime(&tNow);
    //struct tm* ptmEnd = localtime(&tEnd);
    struct tm ptm = { 0 };
    struct tm ptmEnd = { 0 };
    localtime_r(&tNow, &ptm);
    localtime_r(&tEnd, &ptmEnd);

    char szTmp[50] = {0};
    strftime(szTmp,50,"%H:%M:%S",&ptm);
    char szEnd[50] = {0};
    strftime(szEnd,50,"%H:%M:%S",&ptmEnd);
    printf("%s /n",szTmp);
    printf("%s /n",szEnd);

    system("PAUSE");
    return EXIT_SUCCESS;
}

最后出来的结果是:10:29:06  10:59:06  
时间: 2024-08-06 09:05:41

localtime 和 localtime_r 的区别的相关文章

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 + 1800; //注意下面两行的区别 struct tm* ptm = localtime(&tNo

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 + 2500; //在这里修改程序 //struct tm* ptm = localtime(&tNo

Linux下用C获取当前时间

Linux下用C获取当前时间,具体如下: 代码(可以把clock_gettime换成time(NULL)) ? 1 2 3 4 5 6 7 8 9 10 void getNowTime() {  timespec time;  clock_gettime(CLOCK_REALTIME, &time); //获取相对于1970到现在的秒数  tm nowTime;  localtime_r(&time.tv_sec, &nowtime);  char current[1024];  

localtime、localtime_s、localtime_r的使用

(1).localtime用来获取系统时间,精度为秒 #include <stdio.h>#include <time.h>int main(){    time_t time_seconds = time(0);    struct tm* now_time = localtime(&time_seconds);    printf("%d-%d-%d %d:%d:%d/n", now_time->tm_year + 1900, now_time

localtime死锁——多线程下fork子进程

最近测试我们自己改进的redis,发现在做rdb时,子进程会一直hang住,gdb attach上,堆栈如下: (gdb) bt #0 0x0000003f6d4f805e in __lll_lock_wait_private () from /lib64/libc.so.6 #1 0x0000003f6d49dcad in _L_lock_2164 () from /lib64/libc.so.6 #2 0x0000003f6d49da67 in __tz_convert () from /l

C程序中对时间的处理——time库函数详解

包含文件:<sys/time.h> <time.h> 一.在C语言中有time_t, tm, timeval等几种类型的时间 1.time_t time_t实际上是长整数类型,定义为:typedef long time_t; /* time value */ 2.timeval timeval是一个结构体,在time.h中定义为:struct timeval{     __time_t tv_sec;                /* Seconds. */     __suse

C++中的时间函数

C++获取时间函数众多,何时该用什么函数,拿到的是什么时间?该怎么用?很多人都会混淆. 本文是本人经历了几款游戏客户端和服务器开发后,对游戏中时间获取的一点总结. 最早学习游戏客户端时,为了获取最精确的时间,使用两个函数 BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency); BOOL QueryPerformanceCounter(LARGE_INTEGER *lpCount); 这两个函数分别是获取CPU的时钟频率和CPU计数器

Visual studio 2015程序转Eclipse gun编译出现的问题总结

1.C++11支持 1)Project settings project右键-> c/c++ build ->Settings -> GCC C++ Compiler -> Miscellaneous -> Other flags后面加上 -std=c++11 2)Workspace settings Window-> Preference -> Build -> Settings ->Discovery -> CDT GCC Built-in

《unix环境高级编程》 读书笔记 (5)

近来读书,做些笔记,来年好翻翻. 本文所使用的操作系统为 CentOS7.0,如果不想装双系统的可以装虚拟机,可以参考这里: http://blog.csdn.net/alex_my/article/details/38142229 date and time 涉及到的函数列出如下,然后再举例运行,输出结果,比较直观. 时间这块资料有限,如果有误,还望指正. #include <time.h> time_t time(time_t*tloc); int clock_getres(clockid