时间转换


#include <time.h>

void rawtime_to_datatime(const long myRawtime,int& year,int&month,int&day,int& hour,int&minute,int&second)
{
    time_t rawtime = (time_t)myRawtime;

    struct tm * timeinfo;
    timeinfo = localtime ( &rawtime );

    year = timeinfo->tm_year + 1901;
    month = timeinfo->tm_mon;
    day = timeinfo->tm_mday;
    hour = timeinfo->tm_hour;
    minute = timeinfo->tm_min;
    second = timeinfo->tm_sec;
}


void main()
{
    int year ;
    int month;
    int day ;
    int hour ;
    int minute ;
    int second ;

    //情况1 这是系统获得的原始时间
    time_t systemRawtime;//4个字节long
    time ( &systemRawtime );
    
    printf("system raw time is :%d \n",systemRawtime);

    rawtime_to_datatime(systemRawtime,year,month,day,hour,minute,second);

    printf ( "date/time is: %4d-%2d-%2d ; %2d:%2d:%2d \n",year,month,day,hour,minute,second);



    //情况2 外部得到原始时间
    long myRawtime = 1322109475;
    printf("my raw time is :%d \n",systemRawtime);

    rawtime_to_datatime(myRawtime,year,month,day,hour,minute,second);

    printf ( "date/time is: %4d-%2d-%2d ; %2d:%2d:%2d \n",year,month,day,hour,minute,second);

}

你可能感兴趣的:(struct,System,include)