“yyyymmddhhmiss"、tm、time_t、ASN1_TIME 之间的类型转换

来源:互联网 发布:小爱同学 知乎 编辑:程序博客网 时间:2024/06/04 00:43

总结一下“yyyymmddhhmiss"、tm、time_t、ASN1_TIME 之间的类型转换:

      

 1、yyyymmddhhmiss到tm再到time_t

       time_t FormatTime2(char * szTime)

  1. {  
  2.     struct tm tm1;  
  3.     time_t time1;  
  4.   
  5.   
  6.     sscanf(szTime, "%4d%2d%2d%2d%2d%2d",      
  7.           &tm1.tm_year,   
  8.           &tm1.tm_mon,   
  9.           &tm1.tm_mday,   
  10.           &tm1.tm_hour,   
  11.           &tm1.tm_min,  
  12.           &tm1.tm_sec);  
  13.           
  14.     tm1.tm_year -= 1900;  
  15.      tm1.tm_mon --;  
  16.   
  17.   
  18.     tm1.tm_isdst=-1;  
  19.     
  20.     time1 = mktime(&tm1);  
  21.     return time1;  
  22. }  


 2、time_t到tm再到yyyymmddhhmiss

  1. void FormatTime(time_t time1, char *szTime)  
  2. {  
  3.     struct tm tm1;  
  4.   
  5.   
  6. #ifdef WIN32  
  7.     tm1 = *localtime(&time1);  
  8. #else  
  9.     localtime_r(&time1, &tm1 );  
  10. #endif  
  11.     sprintf( szTime, "%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d",  
  12.            tm1.tm_year+1900, tm1.tm_mon+1, tm1.tm_mday,  
  13.              tm1.tm_hour, tm1.tm_min,tm1.tm_sec);  
  14. }            

 3、time_t到ASN1_TIME

                ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)


4、ASN1_TIME     到   time_t

                参看链接3


另:以“Sat Sep  8 21:46:40 2001”类型输出

               asctime(struct  tm  *timeinfo);


自己写的代码(有点乱):

               #include <stdio.h>

#include <time.h>
#include <stdlib.h>

#include <openssl/pem.h>
#include <openssl/conf.h>
#include <openssl/x509v3.h>
#ifndef OPENSSL_NO_ENGINE
#include <openssl/engine.h>
#endif
time_t FormatTime2(const char * szTime) 

        struct tm tm1; 
        time_t time1; 
       
        sscanf(szTime, "%4d%2d%2d%2d%2d%2d",
               &tm1.tm_year,  
               &tm1.tm_mon,  
               &tm1.tm_mday,  
               &tm1.tm_hour,  
               &tm1.tm_min, 
               &tm1.tm_sec); 
       
        tm1.tm_year -= 1900; 
        tm1.tm_mon --; 
       
        tm1.tm_isdst=-1; 
        time1 = mktime(&tm1); 
       
        return time1; 
}

int FormatTime(const char * szTime, time_t *b, time_t *a)
{
        struct tm before, after;
        sscanf(szTime, "%4d%2d%2d%4d%2d%2d",
               &before.tm_year,
               &before.tm_mon,
               &before.tm_mday,
               &after.tm_year,
               &after.tm_mon,
               &after.tm_mday);
        before.tm_hour = before.tm_min = before.tm_sec = 0;
        after.tm_hour = after.tm_min = after.tm_sec = 0;
              
        before.tm_year -= 1900;
        before.tm_mon --; 
        before.tm_isdst=-1; 
       
        after.tm_year -= 1900;
        after.tm_mon --;
        after.tm_isdst=-1;
       
        *b = mktime(&before); 
        *a = mktime(&after);
       
        return 0;
}


int main()
{
        time_t rawtime = 1000000000;
        const char tttt[16] = "2014112420151124";
        time_t a, b;
        struct tm * timeinfo;
        //time ( &rawtime ); // 或把你的时间赋给rawtime
        timeinfo = localtime ( &rawtime ); // 转为当地时间,输出 tm  结构
        printf("timeinfo->tm_year : %d \n", (timeinfo->tm_year));
        printf("timeinfo->tm_mon : %d \n", (timeinfo->tm_mon));
        printf("timeinfo->tm_mday : %d \n", (timeinfo->tm_mday));
        printf("timeinfo->tm_wday : %d \n", (timeinfo->tm_wday));
        printf("timeinfo->tm_yday : %d \n", (timeinfo->tm_yday));
        printf("timeinfo->tm_isdst : %d \n", (timeinfo->tm_isdst));
        printf ( "Current local time and date: %s\n", asctime (timeinfo) ); // 用asctime 打印
        char str[100];
        sprintf(str,"%s\0",asctime (timeinfo)); // 或转字符串存str。
        printf("iiiiiiiiiiiiiiiiiiii : %s\n", str);


        rawtime = FormatTime(tttt, &b, &a);
        printf(" b = %ld, a = %ld\n", b, a);


        /*
        tm 结构成员
        int tm_sec; // 0-61
        int tm_min;  // 0-59int
        tm_hour; // 0-23
        int tm_mday; // 1-31
        int tm_mon;  // 0-11
        int tm_year; // 1900年起
        int tm_wday; // 0-6
        int tm_yday; // 0-365
        int tm_isdst;
        */

        return 0;
}


自己的一些代码:


static int FormatTime(const char * szTime, ASN1_TIME **tmb, ASN1_TIME **tma)


{


        time_t b, a;


        struct tm before, after;


        const char *hms = "000000000000";


        sscanf(szTime, "%4d%2d%2d%4d%2d%2d",


               &before.tm_year,


               &before.tm_mon,


               &before.tm_mday,


               &after.tm_year,


               &after.tm_mon,


               &after.tm_mday);



        sscanf(hms, "%2d%2d%2d%2d%2d%2d",


               &before.tm_hour,


               &before.tm_min,


               &before.tm_sec,


               &after.tm_hour,


               &after.tm_min,


               &after.tm_sec);



        before.tm_year -= 1900;


        before.tm_mon --;


        before.tm_isdst=-1;



        after.tm_year -= 1900;


        after.tm_mon --;


        after.tm_isdst=-1;



        b = mktime(&before);


        a = mktime(&after);



        *tmb = ASN1_TIME_set(*tmb, b);


        *tma = ASN1_TIME_set(*tma, a);


        return 0;


}



static long long FormatTime2(const char * szTime)


{


        double b, a;


        struct tm before, after;


        const char *hms = "000000000000";


        sscanf(szTime, "%4d%2d%2d%4d%2d%2d",


               &before.tm_year,


               &before.tm_mon,


               &before.tm_mday,


               &after.tm_year,


               &after.tm_mon,


               &after.tm_mday);



        sscanf(hms, "%2d%2d%2d%2d%2d%2d",


               &before.tm_hour,


               &before.tm_min,


               &before.tm_sec,


               &after.tm_hour,


               &after.tm_min,


               &after.tm_sec);



        before.tm_year -= 1900;


        before.tm_mon --;


        before.tm_isdst=-1;



        after.tm_year -= 1900;


        after.tm_mon --;


        after.tm_isdst=-1;



        b = mktime(&before);


        a = mktime(&after);



        printf("b = %ld\n", b);


        printf("a = %ld\n", a);



        a = 5000000000;



        printf("a = %ld\n",a);



        //*tmb = ASN1_TIME_set(*tmb, b);


        //*tma = ASN1_TIME_set(*tma, a);


        return (a-b);


}



int leap(int year)


{


    if((year%4==0&&year%100!=0)||(year%400==0))


        return 1;


    else


        return 0;


}



int number(int year,int month ,int day)


{


    int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};


    int b[12]={31,29,31,30,31,30,31,31,30,31,30,31};


    int i,sum=0;


    if(leap(year))


        for(i=0;i<month-1;i++)


            sum+=b[i];


    else


        for(i=0;i<month-1;i++)


            sum+=a[i];


    sum+=day;


    return sum;


}



static long FormatTime3(const char * szTime)


{


    long days = 0;


    long i,btm_year, btm_mon, btm_mday, atm_year, atm_mon, atm_mday, bseq, aseq;


    sscanf(szTime, "%4ld%2ld%2ld%4ld%2ld%2ld",


               &btm_year,


               &btm_mon,


               &btm_mday,


               &atm_year,


               &atm_mon,


               &atm_mday);



        bseq = number(btm_year, btm_mon, btm_mday);


        aseq = number(atm_year, atm_mon, atm_mday);



        for(i = btm_year + 1; i < atm_year; i++)


        {


            if(leap(i))


                days += 366;


            else


                days += 365;


        }



        if(leap(btm_year))


            days = days + (366 - bseq) + aseq;


        else


            days = days + (365 - bseq) + aseq;



        return days;


}






链接:

          http://blog.csdn.net/nanhaizhixin/article/details/8349668

          http://www.eefocus.com/xuefu2009/blog/10-03/187348_f456a.html

          http://www.blogjava.net/Archangelsy/articles/112830.html

          http://zhidao.baidu.com/link?url=65o1q3T71ulItE3-aED6wnoRBtJKd_qe8NGr25t9F0wfSuyjBVV8ERP6Nril7jaE-Zx8EXVsuiLlwmmFz3wCiK





0 0