forked from gary/ems
2
0
Fork 0
sun_ems/ems_c/kernel/kit_time2.c

66 lines
1.6 KiB
C
Raw Permalink Normal View History

2025-05-13 17:49:49 +08:00
/*****************************************************************************
* @copyright 2024-202, . POWER SUPPLY CO., LTD.
* @file xxx.c
* @brief xxxx
* @author xx
* @date 2024/08/30
* @remark
*****************************************************************************/
#include "kit_time2.h"
uint32_t kit_time_diff_ms(struct timeval nowtime, struct timeval lasttime)
{
uint32_t scount = (nowtime.tv_sec - lasttime.tv_sec) * 1000;
uint32_t mscount = (nowtime.tv_usec - lasttime.tv_usec) / 1000;
return scount + mscount;
}
/**
* @brief: timeout_mslasttime
* @param {timeval} *lasttime
* @param {uint32_t} timeout_ms
* @return {*}
* @author: XX
* @Date: 2024-03-10
*/
uint8_t kit_isTimeout_by_now(struct timeval *lasttime, uint32_t timeout_ms)
{
struct timeval now;
gettimeofday(&now, NULL);
if (kit_time_diff_ms(now, *lasttime) >= timeout_ms)
{
*lasttime = now;
return true;
}
else
{
return false;
}
}
/**
* @brief: tick
* @param {timeval} *tv
* @return {*} ms的值
* @author: XX
* @Date: 2024-07-07
*/
uint64_t get_systicks(struct timeval *tv)
{
struct timespec ts;
// 获取自系统启动以来的滴答数
if (clock_gettime(1, &ts) == -1) // CLOCK_MONOTONIC
{
perror("clock_gettime");
return 0;
}
if (tv != NULL)
{
tv->tv_sec = ts.tv_sec;
tv->tv_usec = ts.tv_nsec / 1000;
}
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}