refactor: replace all stdint::uint32_t into short and more manageable u32_t
[lunaix-os.git] / lunaix-os / includes / lunaix / clock.h
index dbfdfe5d8b13a79bf40430c0a2f9e6f1730bde7f..889490e9af0fb54d14fa5668ca3f422c549931a4 100644 (file)
@@ -1,13 +1,13 @@
 #ifndef __LUNAIX_CLOCK_H
 #define __LUNAIX_CLOCK_H
 
 #ifndef __LUNAIX_CLOCK_H
 #define __LUNAIX_CLOCK_H
 
-#include <stdint.h>
+#include <lunaix/types.h>
 
 
-typedef uint32_t time_t;
+typedef u32_t time_t;
 
 typedef struct
 {
 
 typedef struct
 {
-    uint32_t year;      // use int32 as we need to store the 4-digit year
+    u32_t year; // use int32 as we need to store the 4-digit year
     uint8_t month;
     uint8_t day;
     uint8_t weekday;
     uint8_t month;
     uint8_t day;
     uint8_t weekday;
@@ -19,7 +19,7 @@ typedef struct
 void
 clock_init();
 
 void
 clock_init();
 
-void 
+void
 clock_walltime(datetime_t* datetime);
 
 int
 clock_walltime(datetime_t* datetime);
 
 int
@@ -27,10 +27,28 @@ clock_datatime_eq(datetime_t* a, datetime_t* b);
 
 /**
  * @brief 返回当前系统时间,即自从开机到当前时刻的毫秒时。
 
 /**
  * @brief 返回当前系统时间,即自从开机到当前时刻的毫秒时。
- * 
- * @return time_t 
+ *
+ * @return time_t
  */
  */
-time_t 
+time_t
 clock_systime();
 
 clock_systime();
 
+time_t
+clock_unixtime();
+
+static inline time_t
+clock_tounixtime(datetime_t* dt)
+{
+    return (dt->year - 1970) * 31556926u + (dt->month - 1) * 2629743u +
+           (dt->day - 1) * 86400u + (dt->hour - 1) * 3600u +
+           (dt->minute - 1) * 60u + dt->second;
+}
+
+static inline time_t
+time_tounix(u32_t yyyy, u32_t mm, u32_t dd, u32_t hh, u32_t MM, u32_t ss)
+{
+    return (yyyy - 1970) * 31556926u + (mm - 1) * 2629743u + (dd - 1) * 86400u +
+           (hh - 1) * 3600u + (MM - 1) * 60u + ss;
+}
+
 #endif /* __LUNAIX_CLOCK_H */
 #endif /* __LUNAIX_CLOCK_H */