Add gmtime, mktime to LibC
This commit is contained in:
parent
f1fe3a6a97
commit
d97bb83dea
1 changed files with 74 additions and 8 deletions
|
@ -51,6 +51,30 @@ _LONGJMP::
|
||||||
_extern _SETJMP U64 _setjmp(U64 jmp_buf);
|
_extern _SETJMP U64 _setjmp(U64 jmp_buf);
|
||||||
_extern _LONGJMP U64 longjmp(U64 jmp_buf, U64 ret);
|
_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()
|
U0 free()
|
||||||
{
|
{
|
||||||
PUSH_SYSV_REGS
|
PUSH_SYSV_REGS
|
||||||
|
@ -82,6 +106,56 @@ U0 localtime()
|
||||||
POP_SYSV_REGS
|
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)
|
I64 @strncmp(U8* s1, U8* s2, I32 n)
|
||||||
{
|
{
|
||||||
U64 u1, u2;
|
U64 u1, u2;
|
||||||
|
@ -290,14 +364,6 @@ U0 realloc()
|
||||||
POP_SYSV_REGS
|
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)
|
I64 @time(I64* ptr)
|
||||||
{
|
{
|
||||||
no_warn ptr;
|
no_warn ptr;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue