Bugfixes for moments mixing up microseconds/nanoseconds, plus adding

accessor fields for them
This commit is contained in:
Bruce Hill 2024-11-30 17:25:36 -05:00
parent 919e47a418
commit 07dd1894b7
6 changed files with 20 additions and 4 deletions

View File

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

View File

@ -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)"},

View File

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

View File

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

View File

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

View File

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