DateTime_parse
This commit is contained in:
@@ -149,7 +149,8 @@ typedef struct Result_ {
|
||||
int _rname(N) = CALL;\
|
||||
if(_rname(N) != 0){\
|
||||
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)\
|
||||
));\
|
||||
}\
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "std.h"
|
||||
#include "errors.h"
|
||||
|
||||
/// nanoseconds
|
||||
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_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"
|
||||
// yyyy.MM.dd HH:mm:ss
|
||||
#define FMT_DateTime_text "%04i.%02i.%02i %02i:%02i:%02i"
|
||||
/// yyyy.MM.dd HH:mm:ss
|
||||
/// 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:
|
||||
DateTime dt;
|
||||
@@ -63,7 +68,7 @@ USAGE:
|
||||
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_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
|
||||
}
|
||||
|
||||
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.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