From 07dd1894b70f537bff13e8f9cb2613e62947c006 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 30 Nov 2024 17:25:36 -0500 Subject: [PATCH] Bugfixes for moments mixing up microseconds/nanoseconds, plus adding accessor fields for them --- compile.c | 8 ++++++++ environment.c | 2 +- stdlib/integers.h | 3 +++ stdlib/moments.c | 4 ++-- stdlib/moments.h | 2 +- types.c | 5 +++++ 6 files changed, 20 insertions(+), 4 deletions(-) diff --git a/compile.c b/compile.c index ee06e95..c423d67 100644 --- a/compile.c +++ b/compile.c @@ -3554,6 +3554,14 @@ CORD compile(env_t *env, ast_t *ast) env_t *module_env = Table$str_get(*env->imports, name); return compile(module_env, WrapAST(ast, Var, f->field)); } + case MomentType: { + if (streq(f->field, "seconds")) { + return CORD_all("I64((", compile_to_pointer_depth(env, f->fielded, 0, false), ").tv_sec)"); + } else if (streq(f->field, "microseconds")) { + return CORD_all("I64((", compile_to_pointer_depth(env, f->fielded, 0, false), ").tv_usec)"); + } + code_err(ast, "There is no '%s' field on Moments", f->field); + } default: code_err(ast, "Field accesses are not supported on %T values", fielded_t); } diff --git a/environment.c b/environment.c index a3e117a..b5edc20 100644 --- a/environment.c +++ b/environment.c @@ -306,7 +306,7 @@ env_t *new_compilation_unit(CORD libname) {"minute", "Moment$minute", "func(moment:Moment,timezone=NONE:Text -> Int)"}, {"minutes_till", "Moment$minutes_till", "func(now,then:Moment -> Num)"}, {"month", "Moment$month", "func(moment:Moment,timezone=NONE:Text -> Int)"}, - {"nanosecond", "Moment$nanosecond", "func(moment:Moment,timezone=NONE:Text -> Int)"}, + {"microsecond", "Moment$microsecond", "func(moment:Moment,timezone=NONE:Text -> Int)"}, {"new", "Moment$new", "func(year,month,day:Int,hour,minute=0,second=0.0,timezone=NONE:Text -> Moment)"}, {"parse", "Moment$parse", "func(text:Text, format=\"%Y-%m-%dT%H:%M:%S%z\" -> Moment?)"}, {"relative", "Moment$relative", "func(moment:Moment,relative_to=Moment.now(),timezone=NONE:Text -> Text)"}, diff --git a/stdlib/integers.h b/stdlib/integers.h index 5556c19..1ebc30a 100644 --- a/stdlib/integers.h +++ b/stdlib/integers.h @@ -360,6 +360,9 @@ CONVERSION_FUNC(16, 8) if (__builtin_expect(!truncate && (num##_t)(int_type##_t)rounded != rounded, 0)) \ fail("Cannot truncate the " #num " %g to an " #int_type, (double)rounded); \ return (int_type##_t)rounded; \ + } \ + MACROLIKE PUREFUNC num##_t int_type##_to_##num(int_type##_t n) { \ + return (num##_t)n; \ } CONVERSION_FUNC(Num, Int64) diff --git a/stdlib/moments.c b/stdlib/moments.c index e83bae5..e1f5960 100644 --- a/stdlib/moments.c +++ b/stdlib/moments.c @@ -54,7 +54,7 @@ public Moment_t Moment$now(void) struct timespec ts; if (clock_gettime(CLOCK_REALTIME, &ts) != 0) fail("Couldn't get the time!"); - return (Moment_t){.tv_sec=ts.tv_sec, .tv_usec=ts.tv_nsec}; + return (Moment_t){.tv_sec=ts.tv_sec, .tv_usec=ts.tv_nsec/1000}; } public Moment_t Moment$new(Int_t year, Int_t month, Int_t day, Int_t hour, Int_t minute, double second, OptionalText_t timezone) @@ -180,7 +180,7 @@ public Int_t Moment$second(Moment_t moment, OptionalText_t timezone) return I(info.tm_sec); } -public Int_t Moment$nanosecond(Moment_t moment, OptionalText_t timezone) +public Int_t Moment$microsecond(Moment_t moment, OptionalText_t timezone) { (void)timezone; return I(moment.tv_usec); diff --git a/stdlib/moments.h b/stdlib/moments.h index 48899b8..1ae3326 100644 --- a/stdlib/moments.h +++ b/stdlib/moments.h @@ -26,7 +26,7 @@ Int_t Moment$day_of_year(Moment_t moment, OptionalText_t timezone); Int_t Moment$hour(Moment_t moment, OptionalText_t timezone); Int_t Moment$minute(Moment_t moment, OptionalText_t timezone); Int_t Moment$second(Moment_t moment, OptionalText_t timezone); -Int_t Moment$nanosecond(Moment_t moment, OptionalText_t timezone); +Int_t Moment$microsecond(Moment_t moment, OptionalText_t timezone); Text_t Moment$format(Moment_t moment, Text_t fmt, OptionalText_t timezone); Text_t Moment$date(Moment_t moment, OptionalText_t timezone); Text_t Moment$time(Moment_t moment, bool seconds, bool am_pm, OptionalText_t timezone); diff --git a/types.c b/types.c index 83effd0..78d52f5 100644 --- a/types.c +++ b/types.c @@ -638,6 +638,11 @@ type_t *get_field_type(type_t *t, const char *field_name) if (streq(field_name, "length")) return INT_TYPE; return NULL; } + case MomentType: { + if (streq(field_name, "seconds")) return Type(IntType, .bits=TYPE_IBITS64); + else if (streq(field_name, "microseconds")) return Type(IntType, .bits=TYPE_IBITS64); + return NULL; + } case ChannelType: { if (streq(field_name, "max_size")) return INT_TYPE; return NULL;