时间,给了我们起点,从此,我们开始创造历史

来源:互联网 发布:2011年nba总决赛 知乎 编辑:程序博客网 时间:2024/06/11 00:37

时间,给了我们起点,从此,我们开始创造历史

转载请注明出处。

世纪秒:2000年1月1日0时0分0秒到现在的秒数


datetime.h文件:

#ifnded _DATETIME_H_#define _DATETIME_H_typedef   unsigned char               z_u8;typedef     signed char               z_s8;typedef   unsigned short              z_u16;typedef     signed short              z_s16;typedef   unsigned int                z_u32;typedef     signed int                z_s32;typedef   enum { z_false,  z_true, }  z_bool;enum{    TIME_BCD_FORMAT,    TIME_HEX_FORMAT,};enum{    BEFORE_BASE_TIME,    AFTER_BASE_TIME,};enum{    TIME_DATA_LEN    = 6,};typedef struct{    z_u8  u8Year;    z_u8  u8Month;    z_u8  u8Day;}Date_TypeDef;typedef struct{    z_u8  u8Hour;    z_u8  u8Minute;    z_u8  u8Second;}Time_TypeDef;//计算pDate日期距离2000年1月1日共有多少天z_u32 GetDayNumByDate(Date_TypeDef *pDate);//计算pTime时间共有多少秒z_u32 GetSecondNumByTime(Time_TypeDef *pTime);//计算pDate日期pTime时间距离共有多少秒z_u32 GetSecondNumByDateTime(Date_TypeDef *pDate, Time_TypeDef *pTime);//将u32DayNum天数转换成年月日z_bool DayNumToDate(z_u32 u32DayNum, Date_TypeDef *pDate);//将u32SecondNum秒数转换成时分秒z_bool SecondNumToTime(z_u32 u32SecondNum, Time_TypeDef *pTime);//将u32SecondNum秒数转换成年月日时分秒z_bool SecondNumToDateTime(z_u32 u32SecondNum, Date_TypeDef *pDate, Time_TypeDef *pTime);//将BCD时间格式转换成十六进制时间格式z_u8 TimeFormat_BcdToHex(z_uchar *pBcd, z_uchar *pHex);//将成十六进制时间格式BCD时间格式转换z_u8 TimeFormat_HexToBcd(z_uchar *pHex, z_uchar *pBcd);//将十六进制时间转化为日期时间格式z_bool HexDateTimeToDateTimeType(z_uchar *pHex, Date_TypeDef *pDate, Time_TypeDef *pTime);//将BCD时间转化为日期时间格式z_bool BcdDateTimeToDateTimeType(z_uchar *pBcd, Date_TypeDef *pDate, Time_TypeDef *pTime);//将日期时间格式转化为十六进制格式z_bool DateTimeTypeToHexDateTime(Date_TypeDef *pDate, Time_TypeDef *pTime, z_uchar *pHex);//将日期时间格式转化为BCD时间格式z_bool DateTimeTypeToBcdDateTime(Date_TypeDef *pDate, Time_TypeDef *pTime, z_uchar *pBcd);//计算Base秒数前/后Inter秒的秒数z_u32 GetSecondsWithInter(z_u8 u8AfterOrBefore, z_u32 u32BaseSeconds, z_u32 u32InterSeconds);//计算Base日期时间前/后Inter时间间隔后的时间,存放于Result中z_bool GetDateTimeTypeWithInter(z_u8 u8AfterOrBefore, Date_TypeDef *pBaseDate, Time_TypeDef *pBaseTime,\       Date_TypeDef *pInterDate, Time_TypeDef *pInterTime,Date_TypeDef *pResultDate, Time_TypeDef *pResultTime);//====================================================================================================//函 数 名 : GetDateTimeWithInter//函数功能 : 计算得到u8CodeType编码方式的pNow时间的u8BeforOrAfter的pInterval的时间,存储于pTime中//输    入 : z_u8 u8CodeType --- 时间编码方式//         : z_uchar *pBase --- 基准时间//         : z_u8 u8BeforOrAfter --- 前后标志//         : z_uchar *pInterTime --- 间隔时间//输    出 : z_uchar *pTime --- 获得的结果时间//返 回 值 : z_u8 --- 成功与否 1 成功, 0失败//====================================================================================================z_bool GetDateTimeWithInter(z_u8 u8CodeType, z_uchar *pBase, z_u8 u8BeforOrAfter, z_uchar *pInter, z_uchar *pTime);#endif

datetime.c文件

/************************************************************************************************************** * 时间,给了我们起点,从此,我们开始创造历史                                                                 * **************************************************************************************************************/ /************************************************************************************************************** * 重定义数据类型                                                                                             * **************************************************************************************************************/#include <stdio.h>#include "datetime.h"static  z_u32 YearTable[100] = {0,     366,   731,   1096,  1461,  1827,  2192,  2557,  2922,  3288,                                3653,  4018,  4383,  4749,  5114,  5479,  5844,  6210,  6575,  6940,                                7305,  7671,  8036,  8401,  8766,  9132,  9497,  9862,  10227, 10593,                                10958, 11323, 11688, 12054, 12419, 12784, 13149, 13515, 13880, 14245,                                14610, 14976, 15341, 15706, 16071, 16437, 16802, 17167, 17532, 17898,                                18263, 18628, 18993, 19359, 19724, 20089, 20454, 20820, 21185, 21550,                                21915, 22281, 22646, 23011, 23376, 23742, 24107, 24472, 24837, 25203,                                25568, 25933, 26298, 26664, 27029, 27394, 27759, 28125, 28490, 28855,                                29220, 29586, 29951, 30316, 30681, 31047, 31412, 31777, 32142, 32508,                                32873, 33238, 33603, 33969, 34334, 34699, 35064, 35430, 35795, 36160 }; //非闰年日历天表static z_u32 gMonthTable1[13] = {0,31,59,90,120,151,181,212,243,273,304,334, 365}; //闰年日历天表static z_u32 gMonthTable2[13] = {0,31,60,91,121,152,182,213,244,274,305,335, 366}; //判断某年是否是闰年#define isLeapYear(u16Year) (((((u16Year)%4==0)&&((u16Year)%100!=0))||((u16Year)%400==0)) ? z_true : z_false)//计算pDate日期距离2000年1月1日共有多少天z_u32 GetDayNumByDate(Date_TypeDef *pDate){    z_u32 u32DayNum = 0;        u32DayNum += YearTable[pDate->u8Year];    u32DayNum += isLeapYear(2000 + pDate->u8Year) ? gMonthTable2[pDate->u8Month-1] : gMonthTable1[pDate->u8Month-1];     u32DayNum += pDate->u8Day;        return u32DayNum;}//计算pTime时间共有多少秒z_u32 GetSecondNumByTime(Time_TypeDef *pTime){    z_u32 u32SecondNum = 0;        u32SecondNum += pTime->u8Hour * 60 * 60;    u32SecondNum += pTime->u8Minute * 60;    u32SecondNum += pTime->u8Second;        return u32SecondNum;}//计算pDate日期pTime时间距离共有多少秒z_u32 GetSecondNumByDateTime(Date_TypeDef *pDate, Time_TypeDef *pTime){    z_u32 u32DayNum = 0;    z_u32 u32SecondNum = 0;        u32DayNum = GetDayNumByDate(pDate);        u32SecondNum += u32DayNum * 24 * 60 * 60;        u32SecondNum += GetSecondNumByTime(pTime);        return u32SecondNum;}//将u32DayNum天数转换成年月日z_bool DayNumToDate(z_u32 u32DayNum, Date_TypeDef *pDate){    z_bool bRetVal = z_false;    z_u8 u8Loop = 0;        if((0 < u32DayNum) && (NULL != pDate))    {        for(u8Loop = u32DayNum/367; u32DayNum > YearTable[u8Loop]; u8Loop++);        pDate->u8Year = u8Loop;                u32DayNum -= YearTable[pDate->u8Year - 1];                if(isLeapYear(2000 + pDate->u8Year))        {            for(u8Loop = u32DayNum/32; u32DayNum > gMonthTable2[u8Loop]; u8Loop++);            pDate->u8Month = u8Loop;            pDate->u8Day = u32DayNum - gMonthTable2[pDate->u8Month - 1];        }        else        {            for(u8Loop = u32DayNum/32; u32DayNum > gMonthTable1[u8Loop]; u8Loop++);            pDate->u8Month = u8Loop;            pDate->u8Day = u32DayNum - gMonthTable1[pDate->u8Month - 1];        }                bRetVal = z_true;    }        return bRetVal;}//将u32SecondNum秒数转换成时分秒z_bool SecondNumToTime(z_u32 u32SecondNum, Time_TypeDef *pTime){    if((0 < u32SecondNum) && (NULL != pTime))    {        pTime->u8Hour = u32SecondNum / (60 * 60);        u32SecondNum = u32SecondNum % (60 * 60);                pTime->u8Minute = u32SecondNum / 60;        pTime->u8Second = u32SecondNum % 60;    }        return z_true;}//将u32SecondNum秒数转换成年月日时分秒z_bool SecondNumToDateTime(z_u32 u32SecondNum, Date_TypeDef *pDate, Time_TypeDef *pTime){    z_bool bRetVal = z_false;        if ((0 < u32SecondNum) && (NULL != pDate) && (NULL != pTime))    {        bRetVal = DayNumToDate(u32SecondNum / (24 * 60 * 60), pDate);        bRetVal = SecondNumToTime(u32SecondNum % (24 * 60 * 60), pTime);                bRetVal = z_true;    }        return bRetVal;}//将BCD时间格式转换成十六进制时间格式z_u8 TimeFormat_BcdToHex(z_uchar *pBcd, z_uchar *pHex){    z_u8 u8Loop = 0;        for(u8Loop = 0; u8Loop < TIME_DATA_LEN; u8Loop++)    {        pHex[u8Loop] = (pBcd[u8Loop] / 16 * 10) + (pBcd[u8Loop] % 16);    }        return 1;}//将成十六进制时间格式BCD时间格式转换z_u8 TimeFormat_HexToBcd(z_uchar *pHex, z_uchar *pBcd){    z_u8 u8Loop = 0;        for(u8Loop = 0; u8Loop < TIME_DATA_LEN; u8Loop++)    {        pBcd[u8Loop] = (pHex[u8Loop] / 10 * 16) + (pHex[u8Loop] % 10);    }        return 1;}//将十六进制时间转化为日期时间格式z_bool HexDateTimeToDateTimeType(z_uchar *pHex, Date_TypeDef *pDate, Time_TypeDef *pTime){    z_bool bRetVal = z_true;        pDate->u8Year = pHex[0];    pDate->u8Month = pHex[1];    pDate->u8Day = pHex[2];    pTime->u8Hour = pHex[3];    pTime->u8Minute = pHex[4];    pTime->u8Second = pHex[5];        return bRetVal;}//将BCD时间转化为日期时间格式z_bool BcdDateTimeToDateTimeType(z_uchar *pBcd, Date_TypeDef *pDate, Time_TypeDef *pTime){    z_bool bRetVal = z_true;    z_uchar aucHex[6] = {0};        TimeFormat_BcdToHex(pBcd, aucHex);    bRetVal = HexDateTimeToDateTimeType(aucHex, pDate, pTime);        return bRetVal;}//将日期时间格式转化为十六进制格式z_bool DateTimeTypeToHexDateTime(Date_TypeDef *pDate, Time_TypeDef *pTime, z_uchar *pHex){    z_bool bRetVal = z_true;        pHex[0] = pDate->u8Year;    pHex[1] = pDate->u8Month;    pHex[2] = pDate->u8Day;    pHex[3] = pTime->u8Hour;    pHex[4] = pTime->u8Minute;    pHex[5] = pTime->u8Second;        return bRetVal;}//将日期时间格式转化为BCD时间格式z_bool DateTimeTypeToBcdDateTime(Date_TypeDef *pDate, Time_TypeDef *pTime, z_uchar *pBcd){    z_bool bRetVal = z_true;    z_uchar aucHex[6] = {0};        bRetVal = DateTimeTypeToHexDateTime(pDate, pTime, aucHex);    TimeFormat_HexToBcd(aucHex, pBcd);        return bRetVal;}//计算Base秒数前/后Inter秒的秒数z_u32 GetSecondsWithInter(z_u8 u8AfterOrBefore, z_u32 u32BaseSeconds, z_u32 u32InterSeconds){    z_u32 u32ResultSeconds = 0xFFFFFFFF;        switch(u8AfterOrBefore)    {        case BEFORE_BASE_TIME:            if(u32BaseSeconds > u32InterSeconds)            {                u32ResultSeconds = u32BaseSeconds - u32InterSeconds;            }            break;        case AFTER_BASE_TIME:            if(u32BaseSeconds + u32InterSeconds < 0xFFFFFFFF)            {                u32ResultSeconds = u32BaseSeconds + u32InterSeconds;            }            break;        default:            break;    }        return u32ResultSeconds;}//计算Base日期时间前/后Inter时间间隔后的时间,存放于Result中z_bool GetDateTimeTypeWithInter(z_u8 u8AfterOrBefore, Date_TypeDef *pBaseDate, Time_TypeDef *pBaseTime,\       Date_TypeDef *pInterDate, Time_TypeDef *pInterTime,Date_TypeDef *pResultDate, Time_TypeDef *pResultTime){    z_bool bRetVal = z_false;    z_u32 u32BaseSeconds = 0;    z_u32 u32InterSeconds = 0;    z_u32 u32ResultSeconds = 0;        u32BaseSeconds = GetSecondNumByDateTime(pBaseDate, pBaseTime);    u32InterSeconds = GetSecondNumByDateTime(pInterDate, pInterTime);    u32ResultSeconds = GetSecondsWithInter(u8AfterOrBefore, u32BaseSeconds, u32InterSeconds);        if(u32ResultSeconds != 0xFFFFFFFF)    {        bRetVal = SecondNumToDateTime(u32ResultSeconds, pResultDate, pResultTime);    }        return bRetVal;}//====================================================================================================//函 数 名 : GetDateTimeWithInter//函数功能 : 计算得到u8CodeType编码方式的pNow时间的u8BeforOrAfter的pInterval的时间,存储于pTime中//输    入 : z_u8 u8CodeType --- 时间编码方式//         : z_uchar *pBase --- 基准时间//         : z_u8 u8BeforOrAfter --- 前后标志//         : z_uchar *pInterTime --- 间隔时间//输    出 : z_uchar *pTime --- 获得的结果时间//返 回 值 : z_u8 --- 成功与否 1 成功, 0失败//====================================================================================================z_bool GetDateTimeWithInter(z_u8 u8CodeType, z_uchar *pBase, z_u8 u8BeforOrAfter, z_uchar *pInter, z_uchar *pTime){    z_bool bRetVal = z_false;        Date_TypeDef tBaseDate = {0,0,0};    Time_TypeDef tBaseTime = {0,0,0};    Date_TypeDef tInterDate = {0,0,0};    Time_TypeDef tInterTime = {0,0,0};    Date_TypeDef tResultDate = {0,0,0};    Time_TypeDef tResultTime = {0,0,0};        switch(u8CodeType)    {    case TIME_BCD_FORMAT:        if( ! BcdDateTimeToDateTimeType(pBase, &tBaseDate, &tBaseTime)) break;        if( ! BcdDateTimeToDateTimeType(pInter, &tInterDate, &tInterTime)) break;        if( ! GetDateTimeTypeWithInter(u8BeforOrAfter, &tBaseDate, &tBaseTime,\                     &tInterDate, &tInterTime, &tResultDate, &tResultTime)) break;        if( ! DateTimeTypeToBcdDateTime(&tResultDate, &tResultTime, pTime)) break;        bRetVal = z_true;        break;    case TIME_HEX_FORMAT:        if( ! HexDateTimeToDateTimeType(pBase, &tBaseDate, &tBaseTime)) break;        if( ! HexDateTimeToDateTimeType(pInter, &tInterDate, &tInterTime)) break;        if( ! GetDateTimeTypeWithInter(u8BeforOrAfter, &tBaseDate, &tBaseTime,\                     &tInterDate, &tInterTime, &tResultDate, &tResultTime)) break;        if( ! DateTimeTypeToHexDateTime(&tResultDate, &tResultTime, pTime)) break;        bRetVal = z_true;        break;    default:        break;    }        return bRetVal;}#if 1 //测试函数z_bool DateTimeTest(void){    z_bool bRetVal = z_false;         static z_u8 u8CodeType = TIME_BCD_FORMAT;    static z_uchar tpBase[6] = {0x14, 0x05, 0x29, 0x11, 0x09, 0x45};    static z_u8 u8BeforOrAfter = BEFORE_BASE_TIME;    static z_uchar tInter[6] = {0x00, 0x00, 0x02, 0x00, 0x00, 0x00};    static z_uchar tTime[6] = {0};        bRetVal = GetDateTimeWithInter(u8CodeType, tpBase, u8BeforOrAfter, tInter, tTime);        return bRetVal;}

0 0