Compare commits

...

2 commits

Author SHA1 Message Date
d97bb83dea Add gmtime, mktime to LibC 2025-06-10 09:26:01 -04:00
f1fe3a6a97 Add kludge for sprintf -> npf_snprintf in mujs 2025-06-10 09:25:26 -04:00
4 changed files with 83 additions and 26 deletions

View file

@ -51,6 +51,30 @@ _LONGJMP::
_extern _SETJMP U64 _setjmp(U64 jmp_buf);
_extern _LONGJMP U64 longjmp(U64 jmp_buf, U64 ret);
class libc_tm {
I32 tm_sec; /* seconds, range 0 to 59 */
I32 tm_min; /* minutes, range 0 to 59 */
I32 tm_hour; /* hours, range 0 to 23 */
I32 tm_mday; /* day of the month, range 1 to 31 */
I32 tm_mon; /* month, range 0 to 11 */
I32 tm_year; /* The number of years since 1900 */
I32 tm_wday; /* day of the week, range 0 to 6 */
I32 tm_yday; /* day in the year, range 0 to 365 */
I32 tm_isdst; /* daylight saving time */
};
#define NIST_TIME_OFFSET -9488
I64 CDate2Unix(CDate dt)
{ // TempleOS datetime to Unix timestamp.
return ToI64((dt - Str2Date("1/1/1970")) / CDATE_FREQ + NIST_TIME_OFFSET);
}
public CDate Unix2CDate(I64 timestamp)
{//Unix timestamp to TempleOS datetime.
return (timestamp-NIST_TIME_OFFSET)*CDATE_FREQ+Str2Date("1/1/1970");
}
U0 free()
{
PUSH_SYSV_REGS
@ -82,6 +106,56 @@ U0 localtime()
POP_SYSV_REGS
}
U64 @gmtime(U64* timep)
{
CDateStruct ds;
Date2Struct(&ds, Unix2CDate(*timep));
libc_tm* tm = CAlloc(sizeof(libc_tm));
tm->tm_sec = ds.sec;
tm->tm_min = ds.min;
tm->tm_hour = ds.hour;
tm->tm_mday = ds.day_of_mon;
tm->tm_mon = ds.mon;
tm->tm_year = ds.year;
tm->tm_wday = ds.day_of_week;
return tm;
}
U0 gmtime()
{
PUSH_SYSV_REGS
GET_SYSV_ARGS
@gmtime(p0);
POP_SYSV_REGS
}
U64 @mktime(libc_tm* tm)
{
CDateStruct ds;
MemSet(&ds, 0, sizeof(CDateStruct));
if (tm) {
ds.sec = tm->tm_sec;
ds.min = tm->tm_min;
ds.hour = tm->tm_hour;
ds.day_of_mon = tm->tm_mday;
ds.mon = tm->tm_mon;
ds.year = tm->tm_year;
ds.day_of_week = tm->tm_wday;
} else {
Date2Struct(&ds, Now);
}
return CDate2Unix(Struct2Date(&ds));
}
U0 mktime()
{
PUSH_SYSV_REGS
GET_SYSV_ARGS
@mktime(p0);
POP_SYSV_REGS
}
I64 @strncmp(U8* s1, U8* s2, I32 n)
{
U64 u1, u2;
@ -241,7 +315,6 @@ U0 strcmp()
{
PUSH_SYSV_REGS
GET_SYSV_ARGS
//"strcmp: '%s', '%s'\n", p0, p1;
StrCmp(p0, p1);
POP_SYSV_REGS
}
@ -291,14 +364,6 @@ U0 realloc()
POP_SYSV_REGS
}
#define MY_TIME_OFFSET 9488
public
I64 CDate2Unix(CDate dt)
{ // TempleOS datetime to Unix timestamp.
return ToI64((dt - Str2Date("1/1/1970")) / CDATE_FREQ) - MY_TIME_OFFSET;
}
I64 @time(I64* ptr)
{
no_warn ptr;
@ -385,11 +450,3 @@ U0 strstr()
POP_SYSV_REGS
}
U0 sprintf()
{
PUSH_SYSV_REGS
GET_SYSV_ARGS
StrPrint(p0, p1, p2, p3, p4, p5);
POP_SYSV_REGS
}

View file

@ -323,7 +323,7 @@ static char *fmtdate(char *buf, double t)
int d = DateFromTime(t);
if (!isfinite(t))
return "Invalid Date";
sprintf(buf, "%04d-%02d-%02d", y, m+1, d);
npf_snprintf(buf, 1024, "%04d-%02d-%02d", y, m+1, d);
return buf;
}
@ -338,11 +338,11 @@ static char *fmttime(char *buf, double t, double tza)
if (!isfinite(t))
return "Invalid Date";
if (tza == 0)
sprintf(buf, "%02d:%02d:%02d.%03dZ", H, M, S, ms);
npf_snprintf(buf, 1024, "%02d:%02d:%02d.%03dZ", H, M, S, ms);
else if (tza < 0)
sprintf(buf, "%02d:%02d:%02d.%03d-%02d:%02d", H, M, S, ms, tzh, tzm);
npf_snprintf(buf, 1024, "%02d:%02d:%02d.%03d-%02d:%02d", H, M, S, ms, tzh, tzm);
else
sprintf(buf, "%02d:%02d:%02d.%03d+%02d:%02d", H, M, S, ms, tzh, tzm);
npf_snprintf(buf, 1024, "%02d:%02d:%02d.%03d+%02d:%02d", H, M, S, ms, tzh, tzm);
return buf;
}
@ -353,7 +353,7 @@ static char *fmtdatetime(char *buf, double t, double tza)
return "Invalid Date";
fmtdate(dbuf, t);
fmttime(tbuf, t, tza);
sprintf(buf, "%sT%s", dbuf, tbuf);
npf_snprintf(buf, 1024, "%sT%s", dbuf, tbuf);
return buf;
}

View file

@ -116,11 +116,11 @@ static void numtostr(js_State *J, const char *fmt, int w, double n)
{
/* buf needs to fit printf("%.20f", 1e20) */
char buf[50], *e;
sprintf(buf, fmt, w, n);
npf_snprintf(buf, 1024, fmt, w, n);
e = strchr(buf, 'e');
if (e) {
int exp = atoi(e+1);
sprintf(e, "e%+d", exp);
npf_snprintf(e, 1024, "e%+d", exp);
}
js_pushstring(J, buf);
}

View file

@ -919,11 +919,11 @@ const char *js_ref(js_State *J)
s = v->u.boolean ? "_True" : "_False";
break;
case JS_TOBJECT:
sprintf(buf, "%p", (void*)v->u.object);
npf_snprintf(buf, 1024, "%p", (void*)v->u.object);
s = js_intern(J, buf);
break;
default:
sprintf(buf, "%d", J->nextref++);
npf_snprintf(buf, 1024, "%d", J->nextref++);
s = js_intern(J, buf);
break;
}