diff --git a/include/tlibc/time.h b/include/tlibc/time.h index a891a40..e21f897 100644 --- a/include/tlibc/time.h +++ b/include/tlibc/time.h @@ -27,6 +27,31 @@ void sleepNsec(nsec_t time); void sleepUsec(usec_t time); void sleepMsec(msec_t time); + +typedef struct Time { + i32 nsec; + i8 sec; + i8 min; + i8 hour; +} Time; + +typedef struct Date { + i8 month_day; + i8 month; + i16 year; + i8 week_day; + i16 year_day; +} Date; + +typedef struct DateTime { + Date d; + Time t; +} DateTime; + +void DateTime_get(DateTime* dt, bool utc_time); +static inline void DateTime_getLocal(DateTime* dt) { DateTime_get(dt, false); } +static inline void DateTime_getUTC(DateTime* dt) { DateTime_get(dt, true); } + #if __cplusplus } #endif diff --git a/project.config b/project.config index f341d40..e65132c 100644 --- a/project.config +++ b/project.config @@ -4,7 +4,7 @@ CBUILD_VERSION=2.3.0 PROJECT="tlibc" CMP_C="gcc" CMP_CPP="g++" -STD_C="c99" +STD_C="c11" STD_CPP="c++11" WARN_C="-Wall -Wextra -Wduplicated-branches diff --git a/src/time.c b/src/time.c index 369bbc7..a3423ed 100644 --- a/src/time.c +++ b/src/time.c @@ -44,3 +44,28 @@ void sleepUsec(usec_t time){ void sleepMsec(msec_t time){ sleepNsec(time * M); } + +#define portable_localtime_s(time_t_ptr, tm_ptr) IFWIN(localtime_s(tm_ptr, time_t_ptr), localtime_s(time_t_ptr, tm_ptr)) +#define portable_gmtime_s(time_t_ptr, tm_ptr) IFWIN(gmtime_s(tm_ptr, time_t_ptr), gmtime_s(time_t_ptr, tm_ptr)) + +void DateTime_get(DateTime* dt, bool utc_time){ + time_t c_time = time(NULL); + struct tm c_tm; + if(utc_time){ + portable_gmtime_s(&c_time, &c_tm); + } + else { + portable_localtime_s(&c_time, &c_tm); + } + struct timespec c_timespec; + clock_gettime(CLOCK_REALTIME, &c_timespec); + dt->t.nsec = c_timespec.tv_nsec; + dt->t.sec = c_timespec.tv_sec; + dt->t.min = c_tm.tm_min; + dt->t.hour = c_tm.tm_hour; + dt->d.month_day = c_tm.tm_mday; + dt->d.month = c_tm.tm_mon; + dt->d.year = c_tm.tm_year; + dt->d.week_day = c_tm.tm_wday; + dt->d.year_day = c_tm.tm_yday; +}