Compare commits

..

2 Commits

Author SHA1 Message Date
adaf5cc311 cstr_copy 2025-11-13 06:16:57 +05:00
af36bab444 implemented DateTime struct 2025-11-13 05:11:35 +05:00
5 changed files with 63 additions and 1 deletions

View File

@@ -1,6 +1,8 @@
#pragma once #pragma once
#include "../std.h" #include "../std.h"
char* cstr_copy(cstr self);
#define strcat_malloc(STR0, ...) _strcat_malloc(count_args(__VA_ARGS__), STR0, __VA_ARGS__) #define strcat_malloc(STR0, ...) _strcat_malloc(count_args(__VA_ARGS__), STR0, __VA_ARGS__)
char* _strcat_malloc(u64 n, cstr str0, ...); char* _strcat_malloc(u64 n, cstr str0, ...);
char* _vstrcat_malloc(u64 n, cstr str0, va_list args); char* _vstrcat_malloc(u64 n, cstr str0, va_list args);

View File

@@ -27,6 +27,31 @@ void sleepNsec(nsec_t time);
void sleepUsec(usec_t time); void sleepUsec(usec_t time);
void sleepMsec(msec_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 #if __cplusplus
} }
#endif #endif

View File

@@ -4,7 +4,7 @@ CBUILD_VERSION=2.3.0
PROJECT="tlibc" PROJECT="tlibc"
CMP_C="gcc" CMP_C="gcc"
CMP_CPP="g++" CMP_CPP="g++"
STD_C="c99" STD_C="c11"
STD_CPP="c++11" STD_CPP="c++11"
WARN_C="-Wall -Wextra WARN_C="-Wall -Wextra
-Wduplicated-branches -Wduplicated-branches

View File

@@ -1,5 +1,15 @@
#include "tlibc/string/cstr.h" #include "tlibc/string/cstr.h"
char* cstr_copy(cstr self){
if(self == NULL)
return NULL;
u64 len_with_zero = strlen(self) + 1;
char* copy = (char*)malloc(len_with_zero);
memcpy(copy, self, len_with_zero);
return copy;
}
char* _strcat_malloc(u64 n, cstr str0, ...){ char* _strcat_malloc(u64 n, cstr str0, ...){
va_list args; va_list args;
va_start(args, str0); va_start(args, str0);

View File

@@ -44,3 +44,28 @@ void sleepUsec(usec_t time){
void sleepMsec(msec_t time){ void sleepMsec(msec_t time){
sleepNsec(time * M); 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;
}