//------------------------------------------------------------------------------
//
// This routine converts number of days to a date structure.
//
//------------------------------------------------------------------------------
void TIMEDaysToDate(const uint16_t numOfDays,
DATE_T* pData)
{
uint32_t date, year;
int ddd, mi;
// Normalize the date from 0/3/1 to 1972/1/1
date = numOfDays + 720198;
// The formula (10000 * date + 14780) / 3652425 could be more than 7.8*10^9,
// outside uint32_t range. But all members are divisible by 5!
year = (2000 * date + 2956) / 730485; // And we are in unsigned int range :)
// Now the formula cannot reach more than 1.6*10^9!
ddd = date - (365 * year + year / 4 - year / 100 + year / 400);
if (ddd < 0)
{
year--;
ddd = date - (365 * year + year / 4 - year / 100 + year / 400);
}
mi = (100*ddd + 52) / 3060;
pData->month = (mi + 2) % 12 + 1;
pData->year = year + (mi + 2) / 12;
pData->day = ddd - (mi * 306 + 5) / 10 + 1;
} // End: EIP_Util_DaysToDate()
//------------------------------------------------------------------------------
//
// This routine converts number of milliseconds to a time structure.
//
//------------------------------------------------------------------------------
void TIMEMsToTime(const uint64_t numOfMs,
TIME_OF_DAT_T* pData)
{
pData->millisecond = numOfMs % OSAL_MILLISECONDS_PER_SECOND;
pData->second = (numOfMs / OSAL_MILLISECONDS_PER_SECOND) % SECONDS_PER_MINUTE;
pData->minute = ((numOfMs / OSAL_MILLISECONDS_PER_SECOND) / SECONDS_PER_MINUTE) %
MINUTES_PER_HOUR;
pData->hour = (((numOfMs / OSAL_MILLISECONDS_PER_SECOND) / SECONDS_PER_MINUTE) /
MINUTES_PER_HOUR) % HOURS_PER_DAY;
} // End: EIP_Util_MsToTime()
//------------------------------------------------------------------------------
//
// This routine converts a date structure to a number of days.
//
//------------------------------------------------------------------------------
uint16_t TIMEDateToDays(const DATE_T* pData)
{
uint16_t month, year;
month = (pData->month + 9) % 12;
year = pData->year - (month / 10);
// Compute date and normalize it from 1972/1/1 to 0/3/1
return 365 * year + year / 4 - year / 100 +
year / 400 + (month * 306 + 5) / 10 + (pData->day - 1) - 720198;
} // End: EIP_Util_DateToDays()
//------------------------------------------------------------------------------
//
// This routine converts a time structure to a number of milliseconds.
//
//------------------------------------------------------------------------------
uint64_t TIMETimeToMs(const TIME_DU_T* pData)
{
return pData->hour * MINUTES_PER_HOUR * SECONDS_PER_MINUTE *
OSAL_MILLISECONDS_PER_SECOND +
pData->minute * SECONDS_PER_MINUTE * OSAL_MILLISECONDS_PER_SECOND +
pData->second * OSAL_MILLISECONDS_PER_SECOND +
pData->millisecond;
} // End: EIP_Util_TimeToMs()

1万+

被折叠的 条评论
为什么被折叠?



