rework external irq system, introduce hierarchical irq
[lunaix-os.git] / lunaix-os / includes / lunaix / time.h
1 #ifndef __LUNAIX_TIME_H
2 #define __LUNAIX_TIME_H
3
4 #include <lunaix/types.h>
5
6 #define TIME_MS -1000
7 #define TIME_SEC 1
8 #define TIME_MIN (TIME_SEC * 60)
9 #define TIME_HOUR (TIME_MIN * 60)
10
11 typedef unsigned int ticks_t;
12 typedef u32_t time_t;
13
14 typedef struct
15 {
16     u32_t year; // use int32 as we need to store the 4-digit year
17     u8_t month;
18     u8_t day;
19     u8_t weekday;
20     u8_t hour;
21     u8_t minute;
22     u8_t second;
23 } datetime_t;
24
25 static inline ticks_t
26 ticks_seconds(unsigned int seconds)
27 {
28     return seconds * 1000;
29 }
30
31 static inline ticks_t
32 ticks_minutes(unsigned int min)
33 {
34     return ticks_seconds(min * 60);
35 }
36
37 static inline ticks_t
38 ticks_msecs(unsigned int ms)
39 {
40     return ms;
41 }
42
43 static inline time_t
44 datetime_tounix(datetime_t* dt)
45 {
46     return (dt->year - 1970) * 31556926u + (dt->month - 1) * 2629743u +
47            (dt->day - 1) * 86400u + (dt->hour - 1) * 3600u +
48            (dt->minute - 1) * 60u + dt->second;
49 }
50
51 static inline time_t
52 time_tounix(u32_t yyyy, u32_t mm, u32_t dd, u32_t hh, u32_t MM, u32_t ss)
53 {
54     return (yyyy - 1970) * 31556926u + (mm - 1) * 2629743u + (dd - 1) * 86400u +
55            (hh - 1) * 3600u + (MM - 1) * 60u + ss;
56 }
57
58 static inline int
59 datatime_eq(datetime_t* a, datetime_t* b)
60 {
61     return a->year == b->year && a->month == b->month && a->day == b->day &&
62            a->weekday == b->weekday && a->minute == b->minute &&
63            a->second == b->second;
64 }
65
66 #endif /* __LUNAIX_TIME_H */