Compare commits
2 Commits
08d45faa83
...
bdbe959e23
| Author | SHA1 | Date | |
|---|---|---|---|
| bdbe959e23 | |||
| 94b051e852 |
@@ -17,18 +17,21 @@ void foo(){
|
|||||||
#if defined(__GNUC__) || defined(__TINYC__)
|
#if defined(__GNUC__) || defined(__TINYC__)
|
||||||
|
|
||||||
#define Deferral(MAX_DEFER_STATEMENTS) \
|
#define Deferral(MAX_DEFER_STATEMENTS) \
|
||||||
unsigned char _num_deferrals = 0; \
|
int _num_deferrals = 0; \
|
||||||
void *_defer_return_loc = 0, *_deferrals[MAX_DEFER_STATEMENTS] = {0};
|
ATTRIBUTE_UNUSED void *_defer_return_loc = 0;\
|
||||||
|
ATTRIBUTE_UNUSED void *_deferrals[MAX_DEFER_STATEMENTS] = {0};
|
||||||
|
|
||||||
# define Defer(block) _Defer(block, __LINE__)
|
# define Defer(block) _Defer(block, __LINE__)
|
||||||
# define Return _Return(__LINE__)
|
# define Return _Return(__LINE__)
|
||||||
|
|
||||||
#define _defer_token_cat(a, b) a ## b
|
#ifndef CAT2
|
||||||
|
#define CAT2(a, b) a ## b
|
||||||
|
#endif
|
||||||
|
|
||||||
#define _Defer(block, n) do { \
|
#define _Defer(block, n) do { \
|
||||||
_deferrals[_num_deferrals++] = && _defer_token_cat(_defer_ini, n); \
|
_deferrals[_num_deferrals++] = && CAT2(_defer_ini, n); \
|
||||||
if (0) { \
|
if (0) { \
|
||||||
_defer_token_cat(_defer_ini, n): \
|
CAT2(_defer_ini, n): \
|
||||||
block; \
|
block; \
|
||||||
if (_num_deferrals) { \
|
if (_num_deferrals) { \
|
||||||
goto *_deferrals[--_num_deferrals]; \
|
goto *_deferrals[--_num_deferrals]; \
|
||||||
@@ -39,13 +42,11 @@ void foo(){
|
|||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define _Return(n) \
|
#define _Return(n) \
|
||||||
if (_num_deferrals \
|
if (_num_deferrals) \
|
||||||
/* this nonsense disables warning Wunused-but-set-variable */ \
|
|
||||||
&& _defer_return_loc == _defer_return_loc ) \
|
|
||||||
{ \
|
{ \
|
||||||
_defer_return_loc = && _defer_token_cat(_defer_fini_, n); \
|
_defer_return_loc = && CAT2(_defer_fini_, n); \
|
||||||
goto *_deferrals[--_num_deferrals]; \
|
goto *_deferrals[--_num_deferrals]; \
|
||||||
} else _defer_token_cat(_defer_fini_, n): \
|
} else CAT2(_defer_fini_, n): \
|
||||||
return
|
return
|
||||||
|
|
||||||
#else /* !__GNUC__ && !__TINYCC__ */
|
#else /* !__GNUC__ && !__TINYCC__ */
|
||||||
@@ -59,7 +60,7 @@ void foo(){
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define Deferral(MAX_DEFER_STATEMENTS) \
|
#define Deferral(MAX_DEFER_STATEMENTS) \
|
||||||
volatile unsigned char _num_deferrals = 0; \
|
volatile int _num_deferrals = 0; \
|
||||||
jmp_buf _defer_return_loc = {0}, _deferrals[MAX_DEFER_STATEMENTS] = {0};
|
jmp_buf _defer_return_loc = {0}, _deferrals[MAX_DEFER_STATEMENTS] = {0};
|
||||||
|
|
||||||
#define Defer(block) do { \
|
#define Defer(block) do { \
|
||||||
|
|||||||
@@ -149,7 +149,8 @@ typedef struct Result_ {
|
|||||||
int _rname(N) = CALL;\
|
int _rname(N) = CALL;\
|
||||||
if(_rname(N) != 0){\
|
if(_rname(N) != 0){\
|
||||||
Error_printAndExit(Error_create(\
|
Error_printAndExit(Error_create(\
|
||||||
strerror_malloc(_rname(N)), true, ErrorCallPos_here(), \
|
str_from_cstr(strerror_malloc(_rname(N))), true, \
|
||||||
|
ErrorCallPos_here(), \
|
||||||
ErrorCodePage_name(LIBC_ERRNO), _rname(N)\
|
ErrorCodePage_name(LIBC_ERRNO), _rname(N)\
|
||||||
));\
|
));\
|
||||||
}\
|
}\
|
||||||
|
|||||||
@@ -59,6 +59,8 @@ typedef void (*Destructor_t)(void* self);
|
|||||||
#define dbg(N) printf("\e[95m%d\n",N)
|
#define dbg(N) printf("\e[95m%d\n",N)
|
||||||
|
|
||||||
#define nameof(V) #V
|
#define nameof(V) #V
|
||||||
|
#define CAT2(a, b) a ## b
|
||||||
|
#define CAT3(a, b, c) a ## b ## c
|
||||||
|
|
||||||
#define ARRAY_LEN(A) (sizeof(A)/sizeof(A[0]))
|
#define ARRAY_LEN(A) (sizeof(A)/sizeof(A[0]))
|
||||||
#define ALIGN_TO(_SIZE,_ALIGN) (((_SIZE) + ((_ALIGN) - 1)) & ~((_ALIGN) - 1))
|
#define ALIGN_TO(_SIZE,_ALIGN) (((_SIZE) + ((_ALIGN) - 1)) & ~((_ALIGN) - 1))
|
||||||
@@ -122,6 +124,24 @@ typedef void (*Destructor_t)(void* self);
|
|||||||
#define ATTRIBUTE_THREAD_LOCAL __thread
|
#define ATTRIBUTE_THREAD_LOCAL __thread
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define PRAGMA_WARNING_PUSH __pragma(warning( push ))
|
||||||
|
#define PRAGMA_WARNING_POP __pragma(warning( pop ))
|
||||||
|
#define PRAGMA_WARNING_DISABLE(wNumber) __pragma(warning( disable : wNumber ))
|
||||||
|
#define W_RETURN_TYPE
|
||||||
|
#else
|
||||||
|
#define _PRAGMA(P) _Pragma(#P)
|
||||||
|
#define PRAGMA_WARNING_PUSH _PRAGMA(GCC diagnostic push)
|
||||||
|
#define PRAGMA_WARNING_POP _PRAGMA(GCC diagnostic pop)
|
||||||
|
#define PRAGMA_WARNING_DISABLE(wName) _PRAGMA(GCC diagnostic ignored wName)
|
||||||
|
#define W_RETURN_TYPE "-Wreturn-type"
|
||||||
|
#endif
|
||||||
|
#define WARNING_DISABLE(WARNING, CODE...) \
|
||||||
|
PRAGMA_WARNING_PUSH \
|
||||||
|
PRAGMA_WARNING_DISABLE(WARNING) \
|
||||||
|
CODE; \
|
||||||
|
PRAGMA_WARNING_POP
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "std.h"
|
#include "errors.h"
|
||||||
|
|
||||||
/// nanoseconds
|
/// nanoseconds
|
||||||
typedef u64 nsec_t;
|
typedef u64 nsec_t;
|
||||||
@@ -52,10 +52,15 @@ void DateTime_get(DateTime* dt, bool utc_time);
|
|||||||
static inline void DateTime_getLocal(DateTime* dt) { DateTime_get(dt, false); }
|
static inline void DateTime_getLocal(DateTime* dt) { DateTime_get(dt, false); }
|
||||||
static inline void DateTime_getUTC(DateTime* dt) { DateTime_get(dt, true); }
|
static inline void DateTime_getUTC(DateTime* dt) { DateTime_get(dt, true); }
|
||||||
|
|
||||||
// yyyy.MM.dd_HH-mm-ss
|
/// yyyy.MM.dd HH:mm:ss_float
|
||||||
|
Result(void) DateTime_parse(cstr src, DateTime* dt);
|
||||||
|
|
||||||
|
/// yyyy.MM.dd_HH-mm-ss
|
||||||
#define FMT_DateTime_fileName "%04i.%02i.%02i_%02i-%02i-%02i"
|
#define FMT_DateTime_fileName "%04i.%02i.%02i_%02i-%02i-%02i"
|
||||||
// yyyy.MM.dd HH:mm:ss
|
/// yyyy.MM.dd HH:mm:ss
|
||||||
#define FMT_DateTime_text "%04i.%02i.%02i %02i:%02i:%02i"
|
/// yyyy.MM.dd HH:mm:ss_float
|
||||||
|
#define FMT_DateTime_text "%04i.%02i.%02i-%02i:%02i:%02i"
|
||||||
|
#define FMT_DateTime_text_subsec FMT_DateTime_text".%09i"
|
||||||
/*
|
/*
|
||||||
USAGE:
|
USAGE:
|
||||||
DateTime dt;
|
DateTime dt;
|
||||||
@@ -63,7 +68,7 @@ USAGE:
|
|||||||
printf(FMT_DateTime_text, DT_expand(dt));
|
printf(FMT_DateTime_text, DT_expand(dt));
|
||||||
*/
|
*/
|
||||||
#define DT_expand(dt) dt.d.year, dt.d.month, dt.d.month_day, dt.t.hour, dt.t.min, dt.t.sec
|
#define DT_expand(dt) dt.d.year, dt.d.month, dt.d.month_day, dt.t.hour, dt.t.min, dt.t.sec
|
||||||
|
#define DT_expand_subsec(dt) dt.d.year, dt.d.month, dt.d.month_day, dt.t.hour, dt.t.min, dt.t.sec, dt.t.nsec
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/time.c
14
src/time.c
@@ -71,3 +71,17 @@ void DateTime_get(DateTime* dt, bool utc_time){
|
|||||||
dt->d.week_day = c_tm.tm_wday + 1;
|
dt->d.week_day = c_tm.tm_wday + 1;
|
||||||
dt->d.year_day = c_tm.tm_yday + 1;
|
dt->d.year_day = c_tm.tm_yday + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Result(void) DateTime_parse(cstr src, DateTime* dt){
|
||||||
|
zeroStruct(dt);
|
||||||
|
f64 sec_f = 0;
|
||||||
|
i32 r = sscanf(src, "%"SCNi16".%"SCNi8".%"SCNi8"-%"SCNi8":%"SCNi8":%lf",
|
||||||
|
&dt->d.year, &dt->d.month, &dt->d.month_day,
|
||||||
|
&dt->t.hour, &dt->t.min, &sec_f);
|
||||||
|
if(r != 6){
|
||||||
|
return RESULT_ERROR_FMT("attepmted to parse DateTime, got %i fields out of 6", r);
|
||||||
|
}
|
||||||
|
dt->t.sec = (i32)sec_f;
|
||||||
|
dt->t.nsec = (sec_f - (i32)sec_f) * 1e9;
|
||||||
|
return RESULT_VOID;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user