浅谈时间戳与日期时间互转C语言
浅谈时间戳与日期时间互转C语言
/* *ctime.h * *Createdon:May19,2016 * */ #ifndefCTIME_H_ #defineCTIME_H_ #include"common/micro_type.h" #defineOFFSET_SECOND946684800/*��1970/1/1/0/0/0��2000/1/1/0/0/0֮���������*/ //#defineOFFSET_SECOND0/*��2000/1/1/0/0/0��2000/1/1/0/0/0֮���������*/ #defineSECOND_OF_DAY86400/*1�������*/ typedefstructdate_time { uint16iYear; uint16iMon; uint16iDay; uint16iHour; uint16iMin; uint16iSec; uint16iMsec; }DATE_TIME; voidGetDateTimeFromSecond(unsignedlonglSec,DATE_TIME*tTime); #endif/*CTIME_H_*/ /* *ctime.c * *Createdon:May19,2016 *Author:root */ #include"common/micro_type.h" #include"ctime.h" uint8DayOfMon[12]={31,28,31,30,31,30,31,31,30,31,30,31}; /*������1970/1/1/0/0/0�����ʱ*/ unsignedlongGetSecondTime(DATE_TIME*date_time) { uint16iYear,iMon,iDay,iHour,iMin,iSec; iYear=date_time->iYear; iMon=date_time->iMon; iDay=date_time->iDay; iHour=date_time->iHour; iMin=date_time->iMin; iSec=date_time->iSec; uint16i,Cyear=0; unsignedlongCountDay=0; for(i=1970;i<iYear;i++)/*ͳ��1970�굽����֮ǰ��������*/ { if(((i%4==0)&&(i%100!=0))||(i%400==0))Cyear++; } CountDay=Cyear*366+(iYear-1970-Cyear)*365; for(i=1;i<iMon;i++) { if((i==2)&&(((iYear%4==0)&&(iYear%100!=0))||(iYear%400==0))) CountDay+=29; else CountDay+=DayOfMon[i-1]; } CountDay+=(iDay-1); CountDay=CountDay*SECOND_OF_DAY+(unsignedlong)iHour*3600+(unsignedlong)iMin*60+iSec; returnCountDay; } /*����ʱ�������1970/1/1/0/0������*/ voidGetDateTimeFromSecond(unsignedlonglSec,DATE_TIME*tTime) { uint16i,j,iDay; unsignedlonglDay; lDay=lSec/SECOND_OF_DAY;/*תΪ�������ʱ��*/ lSec=lSec%SECOND_OF_DAY; i=1970; while(lDay>365) { if(((i%4==0)&&(i%100!=0))||(i%400==0))/*����*/ lDay-=366; else lDay-=365; i++; } if((lDay==365)&&!(((i%4==0)&&(i%100!=0))||(i%400==0)))/*ƽ��*/ { lDay-=365; i++; } tTime->iYear=i;/*�õ����*/ for(j=0;j<12;j++)/*�����·�*/ { if((j==1)&&(((i%4==0)&&(i%100!=0))||(i%400==0))) iDay=29; else iDay=DayOfMon[j]; if(lDay>=iDay)lDay-=iDay; elsebreak; } tTime->iMon=j+1; tTime->iDay=lDay+1; tTime->iHour=((lSec/3600)+8)%24;//这里注意,世界时间已经加上北京时间差8, tTime->iMin=(lSec%3600)/60; tTime->iSec=(lSec%3600)%60; }
以上这篇浅谈时间戳与日期时间互转C语言就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。