| | |
| | | * Version: 1.0.0(07/23/2020) |
| | | * Author: Guo Wenxue <guowenxue@gmail.com> |
| | | * ChangeLog: 1, Release initial version on "07/23/2020 07:46:37 AM" |
| | | * |
| | | * |
| | | ********************************************************************************/ |
| | | #ifndef __UTIL_TIME_H_ |
| | | #define __UTIL_TIME_H_ |
| | |
| | | #include <sys/time.h> |
| | | |
| | | |
| | | #ifndef offsetof |
| | | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) |
| | | #endif |
| | | |
| | | #define container_of(ptr, type, member) ({ \ |
| | | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| | | (type *)( (char *)__mptr - offsetof(type,member) );}) |
| | | |
| | | typedef struct date_time_s |
| | | { |
| | | int year; |
| | | int month; |
| | | int day; |
| | | int hour; |
| | | int hour; |
| | | int minute; |
| | | int second; |
| | | int dayofweek; |
| | |
| | | /* sleep for micro second */ |
| | | static inline void msleep(unsigned long ms) |
| | | { |
| | | struct timespec timeout; |
| | | struct timespec timeout; |
| | | unsigned long tmp; |
| | | |
| | | timeout.tv_sec = ms / 1000; |
| | |
| | | else |
| | | { |
| | | timeout.tv_nsec = 0; |
| | | } |
| | | |
| | | } |
| | | |
| | | nanosleep(&timeout, 0); |
| | | } |
| | | |
| | | /* call gettimeofday() to get current micro second */ |
| | | static inline unsigned long time_now() |
| | | { |
| | | struct timeval now; |
| | | struct timeval now; |
| | | |
| | | gettimeofday(&now, 0); |
| | | |
| | |
| | | /* timep has elapsed since $start, unit as micro second*/ |
| | | static inline uint32_t time_elapsed(uint32_t start) |
| | | { |
| | | uint32_t current = time_now(); |
| | | uint32_t current = time_now(); |
| | | |
| | | if(current >= start) |
| | | return current-start; |
| | | else |
| | | else |
| | | return current+0xFFFFFFFF-start; |
| | | } |
| | | |
| | | /* call gettimeofday() to get current micro second */ |
| | | static inline unsigned long time_second() |
| | | { |
| | | struct timeval now; |
| | | struct timeval now; |
| | | |
| | | gettimeofday(&now, 0); |
| | | return now.tv_sec; |
| | |
| | | /* timep has elapsed since $start, unit as micro second*/ |
| | | static inline uint32_t seconds_elapsed(uint32_t start) |
| | | { |
| | | uint32_t current = time_second(); |
| | | uint32_t current = time_second(); |
| | | |
| | | if(current >= start) |
| | | return current-start; |
| | | else |
| | | else |
| | | return current+0xFFFFFFFF-start; |
| | | } |
| | | |
| | |
| | | #define time_after(a,b) \ |
| | | (typecheck(unsigned long, a) && typecheck(unsigned long, b) && ((long)(b) - (long)(a) < 0)) |
| | | #define time_before(a,b) time_after(b,a) |
| | | |
| | | |
| | | #define time_after_eq(a,b) \ |
| | | (typecheck(unsigned long, a) && typecheck(unsigned long, b) && ((long)(a) - (long)(b) >= 0)) |
| | | #define time_before_eq(a,b) time_after_eq(b,a) |
| | |
| | | #define time_after64(a,b) \ |
| | | (typecheck(__u64, a) && typecheck(__u64, b) && ((__s64)(b) - (__s64)(a) < 0)) |
| | | #define time_before64(a,b) time_after64(b,a) |
| | | |
| | | |
| | | #define time_after_eq64(a,b) \ |
| | | (typecheck(__u64, a) && typecheck(__u64, b) && ((__s64)(a) - (__s64)(b) >= 0)) |
| | | #define time_before_eq64(a,b) time_after_eq64(b,a) |
| | | |
| | | |
| | | |
| | | static inline void get_sys_time(date_time_t *date) |
| | | { |
| | | time_t now = time(NULL); |
| | | struct tm *tnow = localtime(&now); |
| | | |
| | | memset(date, 0, sizeof(*date)); |
| | | struct tm *tnow = localtime(&now); |
| | | |
| | | memset(date, 0, sizeof(*date)); |
| | | date->year = 1900 + tnow->tm_year; |
| | | date->month = 1 + tnow->tm_mon; |
| | | date->day = tnow->tm_mday; |
| | | |
| | | date->hour = tnow->tm_hour; |
| | | date->minute = tnow->tm_min; |
| | | date->second = tnow->tm_sec; |
| | | date->dayofweek = tnow->tm_wday; |
| | | date->second = tnow->tm_sec; |
| | | date->dayofweek = tnow->tm_wday; |
| | | return; |
| | | } |
| | | |
| | | static inline int get_rtc_time(date_time_t *date) |
| | | { |
| | | int rv, fd = -1; |
| | | struct rtc_time rtc_tm; |
| | | struct rtc_time rtc_tm; |
| | | |
| | | memset(date, 0, sizeof(*date)); |
| | | |