zoukankan      html  css  js  c++  java
  • localtime()方法的疑惑

    在做一个时间管理的APP中遇到一些问题
    windows linux mac下time.h中都有关于localtime()的定义。
    它不是一个保险可靠的方法,使用的时候需要小心。
    参考 http://blog.csdn.net/maocl1983/article/details/6221810
    在此对原作者表示感谢。
    localtime是个静态的定义,每次得到同一个地址,
    不保证得到真确的时间,看具体的代码:
     1 dev-mini:cronc devone$ cat localtime.cpp         
     2 #include <time.h>
     3 #include <cstdio>
     4 #include <cstdlib>
     5 
     6 int main(int argc, char *argv[])
     7 {
     8     time_t tNow =time(NULL);
     9     time_t tEnd = tNow + 1800;
    10 
    11     struct tm* ptm = localtime(&tNow);
    12     struct tm* ptmEnd = localtime(&tEnd);
    13 
    14     char szTmp[50] = {0};
    15     strftime(szTmp,50,"%H:%M:%S",ptm);
    16     char szEnd[50] = {0};
    17     strftime(szEnd,50,"%H:%M:%S",ptmEnd);
    18 
    19     printf("%s  ",szTmp);
    20     printf("%s  ",szEnd);
    21 
    22     return EXIT_SUCCESS;
    23 }
    24 dev-mini:cronc devone$ g++ -o localtime localtime.cpp        
    25 dev-mini:cronc devone$ ./localtime                     
    26 14:34:37 
    27 14:34:37 
    28 dev-mini:cronc devone$ 

    time_t 类型时间 + 1800 ,但是结果输出都是后者。

    修改代码如下:

     1 hzsx@hzsx-server:~/file/test$ cat localtime.cpp 
     2 #include <time.h>
     3 #include <cstdio>
     4 #include <cstdlib>
     5 
     6 using namespace std;
     7 
     8 int main(int argc, char *argv[])
     9 {
    10     time_t tNow =time(NULL);
    11     time_t tEnd = tNow + 1800;
    12     
    13     //struct tm* ptm = localtime(&tNow);
    14     //struct tm* ptmEnd = localtime(&tEnd);
    15     struct tm ptm = { 0 };
    16     struct tm ptmEnd = { 0 };
    17     localtime_r(&tNow, &ptm);
    18     localtime_r(&tEnd, &ptmEnd);
    19     
    20     char szTmp[50] = {0};
    21     strftime(szTmp,50,"%H:%M:%S",&ptm);
    22     char szEnd[50] = {0};
    23     strftime(szEnd,50,"%H:%M:%S",&ptmEnd);
    24     printf("%s  ",szTmp);
    25     printf("%s  ",szEnd);
    26     
    27     return EXIT_SUCCESS;
    28 }
    29 hzsx@hzsx-server:~/file/test$ g++ -o localtime localtime.cpp       
    30 hzsx@hzsx-server:~/file/test$ ./localtime                    
    31 14:07:45 
    32 14:37:45 
    33 hzsx@hzsx-server:~/file/test$ 

     两段代码在linux和mac下通过。

    localtime()函数是静态分配的,共享同一个结构体。所以下一次调用会覆盖上次的结果。


    libc里提供了一个可重入版的函数localtime_r(),如同windows下的localtime_s(),这是一个
    安全版本的函数。但是两者的参数位置正好相反。

    下面这段代码 使用了安全版本的LOCALTIME_R(),在win linux mac下都具有通用性。 

     1 #include <iostream>
     2 #include <time.h>
     3 #include <cstdlib>
     4 #ifdef WIN32
     5 #define LOCALTIME_R(tm,ti)  localtime_s(tm,ti)
     6 #else
     7 #define LOCALTIME_R(tm,ti)  localtime_r(ti,tm)
     8 #endif
     9 int main()
    10 {
    11         struct tm tmnow = {0};
    12         time_t long_time;
    13         time(&long_time);
    14         LOCALTIME_R(&tmnow,&long_time);
    15         char buftime[80];
    16         strftime(buftime, 80"time is : %Y-%m-%d %H:%M:%S", &tmnow);
    17         std::cout << buftime << std::endl;
    18         return EXIT_SUCCESS;

    19 } 

  • 相关阅读:
    Docker 系列(四):Docker 容器数据卷简单使用
    【QML 动态对象】使用JS中的语句动态创建和销毁组件
    【QML 动态对象】Loader动态加载组件
    vue-cli2.0全局使用sass变量
    两边+居中 布局
    跳转子路由后左侧菜单跳转为空白页,路由地址出错
    el-tree可搜索单选
    el-tree固定高度加滚动条
    前端 权限控制 方式
    综合分析类
  • 原文地址:https://www.cnblogs.com/zzugyl/p/3796471.html
Copyright © 2011-2022 走看看