implemented DateTime struct

This commit is contained in:
Timerix 2025-11-13 05:11:35 +05:00
parent 1775b27980
commit af36bab444
3 changed files with 51 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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;
}