ems/ems_c/kernel/kit_time2.c

66 lines
1.6 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*****************************************************************************
* @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_ms返回真并更新lasttime
* @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;
}