/*-------------------------------------------------------------------------------------------------
* Include files
*------------------------------------------------------------------------------------------------*/
#include "time.h"
#include "stdint.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
/*-------------------------------------------------------------------------------------------------
* Macros definition for interanl usage
*------------------------------------------------------------------------------------------------*/
#define TIME_IS_LEAP_YEAR(year) (((year)%4 == 0 && (year)%100 != 0) || (year)%400 == 0)
/*===================================================================
* FUNCTION NAME:
* Time_Time_Judge_Leap_Sec_Moment
* DESCRIPTION:
* N/A
* PARAMETERS:
* pTime :
* RETURN:
* N/A
* NOTES:
* N/A
* HISTORY:
* Version 1.0 - 2018.10.10
*=================================================================*/
static uint8_t Time_Time_Judge_Leap_Sec_Moment(S_TIME_T *pTime)
{
uint8_t mLeapFlag = 0;
if( (pTime->month == 12 && pTime->date == 31 )
|| (pTime->month == 6 && pTime->date == 30) )
{
if( pTime->hour == 23 && pTime->minute == 59 )
{
mLeapFlag = 1;
}
}
return mLeapFlag;
}
/*===================================================================
* FUNCTION NAME:
* Time_Time_Add1minute
* DESCRIPTION:
* N/A
* PARAMETERS:
* pTime :
* RETURN:
* N/A
* NOTES:
* N/A
* HISTORY:
* Version 1.0 - 2018.10.10
*=================================================================*/
void Time_Time_Add1minute(S_TIME_T *pTime)
{
E_TIME_YEAR_T mYearType;
mYearType = TIME_IS_LEAP_YEAR(pTime->year) ? E_TIME_YEAR_LEAP : E_TIME_YEAR_NOLEAP;
pTime->minute++;
if (pTime->minute >= 60)
{
pTime->minute = 0;
pTime->hour++;
if (pTime->hour >= 24)
{
pTime->hour = 0x00;
pTime->date++;
if (pTime->month == 2)
{
if (mYearType)
{
if (pTime->date>= 30)
{
pTime->date = 1;
pTime->month = 3;
}
}
else
{
if (pTime->date == 29)
{
pTime->date = 1;
pTime->month = 3;
}
}
}
else if ( (pTime->month == 4) || (pTime->month == 6) \
|| (pTime->month == 9) || (pTime->month == 11))
{
if (pTime->date >= 31)
{
pTime->date = 1;
pTime->month++;
}
}
else if ( (pTime->month == 1) || (pTime->month == 3) \
|| (pTime->month == 5) || (pTime->month == 7) \
|| (pTime->month == 8) || (pTime->month == 10) \
|| (pTime->month == 12))
{
if (pTime->date >= 32)
{
pTime->date = 1;
pTime->month++;
}
}
}
if (pTime->month >= 13)
{
pTime->month = 1;
pTime->year++;
}
}
}
/*===================================================================
* FUNCTION NAME:
* Time_Time_Add1second
* DESCRIPTION:
* N/A
* PARAMETERS:
* pTime :
* RETURN:
* N/A
* NOTES:
* N/A
* HISTORY:
* Version 1.0 - 2018.10.10
*=================================================================*/
void Time_Time_Add1second(S_TIME_T *pTime)
{
E_TIME_LEAP_SEC_T mLeapAdjustType = E_TIME_LEAP_SEC_NONE;
pTime->second++;
if(Time_Time_Judge_Leap_Sec_Moment(pTime))
{
if(mLeapAdjustType == E_TIME_LEAP_SEC_POSITIVE)
{
if(pTime->second >= 61)
{
pTime->second = 0;
Time_Time_Add1minute(pTime);
}
}
else if(mLeapAdjustType == E_TIME_LEAP_SEC_NEGATIVE)
{
if(pTime->second >= 59)
{
pTime->second = 0;
Time_Time_Add1minute(pTime);
}
}
else
{
if(pTime->second >= 60)
{
pTime->second = 0;
Time_Time_Add1minute(pTime);
}
}
}
else
{
if(pTime->second >= 60)
{
pTime->second = 0;
Time_Time_Add1minute(pTime);
}
}
}
/*===================================================================
* FUNCTION NAME:
* Time_Time_Add_Hours
* DESCRIPTION:
* N/A
* PARAMETERS:
* pTime :
* addHour :
* RETURN:
* N/A
* NOTES:
* N/A
* HISTORY:
* Version 1.0 - 2018.10.10
*=================================================================*/
void Time_Time_Add_Hours (S_TIME_T *pTime, uint8_t vHour)
{
pTime->hour += vHour % 24;
if (pTime->hour >= 24)
{
pTime->hour -= 24;
++pTime->date;
if (pTime->month == 2)
{
if (TIME_IS_LEAP_YEAR (pTime->year))
{
if (pTime->date>= 30)
{
pTime->date = 1;
pTime->month = 3;
}
}
else
{
if (pTime->date == 29)
{
pTime->date = 1;
pTime->month = 3;
}
}
}
else if (pTime->month == 4 || pTime->month == 6 \
|| pTime->month == 9 || pTime->month == 11)
{
if (pTime->date>= 31)
{
pTime->date = 1;
++pTime->month;
}
}
else if (pTime->month == 1 || pTime->month == 3 || pTime->month == 5\
|| pTime->month == 7 || pTime->month == 8\
|| pTime->month == 10 || pTime->month == 12)
{
if (pTime->date >= 32)
{
pTime->date = 1;
++pTime->month;
}
}
}
if (pTime->month == 13)
{
pTime->month = 1;
++pTime->year;
}
}
/*===================================================================
* FUNCTION NAME:
* Time_Time_Minus_Hours
* DESCRIPTION:
* N/A
* PARAMETERS:
* pTime :
* vHour :
* RETURN:
* N/A
* NOTES:
* N/A
* HISTORY:
* Version 1.0 - 2018.10.10
*=================================================================*/
void Time_Time_Minus_Hours (S_TIME_T *pTime, uint8_t vHour)
{
if (pTime->hour >= vHour)
{
pTime->hour -= vHour;
}
else
{
pTime->hour += 24 - (vHour % 24);
if (pTime->date >= 2)
{
pTime->date -= 1;
}
else
{
if (pTime->month == 3)
{
pTime->month = 2;
if (TIME_IS_LEAP_YEAR (pTime->year))
{
pTime->date = 29;
}
else
{
pTime->date = 28;
}
}
else if (pTime->month == 5 || pTime->month == 7 \
|| pTime->month == 10 || pTime->month == 12)
{
pTime->month -= 1;
pTime->date = 30;
}
else if (pTime->month == 2 || pTime->month == 4 \
|| pTime->month == 6 || pTime->month == 8 \
|| pTime->month == 9 || pTime->month == 11)
{
pTime->month -= 1;
pTime->date = 31;
}
else if (pTime->month == 1)
{
pTime->month = 12;
pTime->date = 31;
pTime->year -= 1;
}
}
} // ti->hour >= 8
}
/*===================================================================
* FUNCTION NAME:
* Time_Format_TimeInf2String
* DESCRIPTION:
* N/A
* PARAMETERS:
* pTime :
* str :
* RETURN:
* N/A
* NOTES:
* N/A
* HISTORY:
* Version 1.0 - 2018.10.10
*=================================================================*/
void Time_Format_TimeInf2String(S_TIME_T *pTime,char *pPromptMsg,char *pStr)
{
sprintf(pStr,"%s:%04d-%02d-%02d %02d:%02d:%02d\r\n",pPromptMsg,\
pTime->year,\
pTime->month,\
pTime->date,\
pTime->hour,\
pTime->minute,\
pTime->second);
}
/*===================================================================
* FUNCTION NAME:
* Time_Check_Time_Is_Same
* DESCRIPTION:
* N/A
* PARAMETERS:
* pLocTime :
* pRefTime :
* RETURN:
* N/A
* NOTES:
* N/A
* HISTORY:
* Version 1.0 - 2018.10.10
*=================================================================*/
uint8_t Time_Check_Time_Is_Same(S_TIME_T *pLocTime,S_TIME_T *pRefTime)
{
return ((pLocTime->second == pRefTime->second)\
&&(pLocTime->minute == pRefTime->minute)\
&&(pLocTime->hour == pRefTime->hour)\
&&(pLocTime->date == pRefTime->date)\
&&(pLocTime->month == pRefTime->month)\
&&(pLocTime->year == pRefTime->year));
}
/*===================================================================
* FUNCTION NAME:
* Time_Sync_LocalTime_To_RefTime
* DESCRIPTION:
* N/A
* PARAMETERS:
* pLocTime :
* pRefTime :
* pAuxTime :
* RETURN:
* N/A
* NOTES:
* N/A
* HISTORY:
* Version 1.0 - 2018.10.10
*=================================================================*/
uint8_t Time_Sync_LocalTime_To_RefTime(S_TIME_T *pLocTime,S_TIME_T *pRefTime,S_TIME_T *pAuxTime)
{
static uint8_t mContinueCounter = 0;
if(Time_Check_Time_Is_Same(pLocTime,pRefTime))
{
mContinueCounter = 0;
return 1;
}
else
{
if(Time_Check_Time_Is_Same(pRefTime,pAuxTime))
{
mContinueCounter++;
if(mContinueCounter>=3)
{
mContinueCounter = 0;
memcpy(pLocTime,pRefTime,sizeof(S_TIME_T));
return 1;
}
else
{
return 0;
}
}
else
{
mContinueCounter = 0;
memcpy(pAuxTime,pRefTime,sizeof(S_TIME_T));
memcpy(pLocTime,pRefTime,sizeof(S_TIME_T));
return 0;
}
}
}
头文件
#ifndef __TIME_H__
#define __TIME_H__
#include "stdint.h"
typedef enum
{
E_TIME_YEAR_LEAP,
E_TIME_YEAR_NOLEAP,
E_YEAR_TYPE_END,
}E_TIME_YEAR_T;
typedef enum
{
E_TIME_LEAP_SEC_POSITIVE,
E_TIME_LEAP_SEC_NEGATIVE,
E_TIME_LEAP_SEC_NONE
}E_TIME_LEAP_SEC_T;
typedef struct
{
uint16_t year;
uint16_t month;
uint16_t date;
uint16_t hour;
uint16_t minute;
uint16_t second;
}S_TIME_T;
void Time_Time_Add1second(S_TIME_T *pTime);
void Time_Time_Add_Hours (S_TIME_T *pTime, uint8_t vHour);
void Time_Time_Minus_Hours (S_TIME_T *pTime, uint8_t vHour);
void Time_Format_TimeInf2String(S_TIME_T *pTime,char *pPromptMsg,char *pStr);
uint8_t Time_Sync_LocalTime_To_RefTime(S_TIME_T *pLocTime,S_TIME_T *pRefTime,S_TIME_T *pAuxTime);
#endif /* __TIME_H__ */