/*
 * Created by hongbo.he@lnxall.com
 *
 * 2025/09/01
 */
#ifndef LNXUTILS_TIME_H
#define LNXUTILS_TIME_H

#include <time.h>

typedef struct {
	unsigned char   second;
	unsigned char   min;
	unsigned char   hour;
	unsigned char   day;
	unsigned char   month;
	unsigned short  year;
} p_clock_t;


void lnxutil_time2clock(time_t *time, p_clock_t *clock);

time_t lnxutil_clock2time(p_clock_t *clock);

void lnxutil_get_clock(p_clock_t *clock);

/*
 * check whether timeout happend
 *  if timeout, update the base time: prev
 *
 * RETURN VALUE
 *  if timeout, return 1
 *  else return 0
 *
 */
int lnxutil_check_timeout(struct timespec *prev, int timeout_ms);

int lnxutil_check_timeout_second(time_t now, time_t *prev, int timeout_s);

int lnxutil_check_timeout_msecond(long long now, long long *prev, int timeout_ms);

unsigned int lnxutil_clock_get_second(void);

long long lnxutil_clock_get_ms(void);

int lnxutil_clock_diff_ms(struct timespec *now, struct timespec *prev);

long long lnxutil_uptime_stamp_ms(void);

long long lnxutil_uptime_stamp_us(void);

long long lnxutil_get_time_stamp_ms(void);

long long lnxutil_get_time_stamp_us(void);

/*
 * lnxutil_busy_wait - Microsecond-level high-precision busy wait
 *  implements exact timing by continuous polling, high CPU usage but excellent precision
 *
 * lnxutil_msleep - Millisecond-level interruptible sleep
 *	uses system call to sleep and yield CPU, with configurable signal handling
 *
 */
void lnxutil_busy_wait(unsigned int us);

int lnxutil_msleep(unsigned int msec, int noint);

#endif
