time convert


//------------------------------------------------------------------------------
//
// 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()

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值