From 02745b0b83f9ea4e1fc98a44f753d513dfebfe62 Mon Sep 17 00:00:00 2001 From: Nikolay Nikolov Date: Fri, 1 Oct 2021 06:34:52 +0300 Subject: [PATCH] + implemented GetDate, GetTime and WeekDay in the WASI dos unit. They return UTC time (I don't know how to obtain the local time zone from within WASI). --- rtl/wasi/dos.pp | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/rtl/wasi/dos.pp b/rtl/wasi/dos.pp index 7442940bfd..849b4d86d6 100644 --- a/rtl/wasi/dos.pp +++ b/rtl/wasi/dos.pp @@ -45,7 +45,7 @@ Type {$i dosh.inc} {Extra Utils} -//function weekday(y,m,d : longint) : longint; platform; +function weekday(y,m,d : longint) : longint; platform; Procedure WasiDateToDt(NanoSecsPast: UInt64; Var Dt: DateTime); platform; //Function DTToUnixDate(DT: DateTime): LongInt; platform; @@ -88,7 +88,7 @@ Function DosVersion:Word; Begin End; -(*function WeekDay (y,m,d:longint):longint; +function WeekDay (y,m,d:longint):longint; { Calculates th day of the week. returns -1 on error } @@ -110,11 +110,31 @@ begin end; WeekDay:=(d+2*u+((3*(u+1)) div 5)+v+(v div 4)-(v div 100)+(v div 400)+1) mod 7; end; -end;*) +end; Procedure GetDate(Var Year, Month, MDay, WDay: Word); +var + NanoSecsPast: __wasi_timestamp_t; + DT: DateTime; begin + if __wasi_clock_time_get(__WASI_CLOCKID_REALTIME,10000000,@NanoSecsPast)=__WASI_ERRNO_SUCCESS then + begin + { todo: convert UTC to local time, as soon as we can get the local timezone + from WASI: https://github.com/WebAssembly/WASI/issues/239 } + WasiDateToDT(NanoSecsPast,DT); + Year:=DT.Year; + Month:=DT.Month; + MDay:=DT.Day; + WDay:=weekday(DT.Year,DT.Month,DT.Day); + end + else + begin + Year:=0; + Month:=0; + MDay:=0; + WDay:=0; + end; end; @@ -133,7 +153,29 @@ end; Procedure GetTime(Var Hour, Minute, Second, Sec100: Word); +var + NanoSecsPast: __wasi_timestamp_t; begin + if __wasi_clock_time_get(__WASI_CLOCKID_REALTIME,10000000,@NanoSecsPast)=__WASI_ERRNO_SUCCESS then + begin + { todo: convert UTC to local time, as soon as we can get the local timezone + from WASI: https://github.com/WebAssembly/WASI/issues/239 } + NanoSecsPast:=NanoSecsPast div 10000000; + Sec100:=NanoSecsPast mod 100; + NanoSecsPast:=NanoSecsPast div 100; + Second:=NanoSecsPast mod 60; + NanoSecsPast:=NanoSecsPast div 60; + Minute:=NanoSecsPast mod 60; + NanoSecsPast:=NanoSecsPast div 60; + Hour:=NanoSecsPast mod 24; + end + else + begin + Hour:=0; + Minute:=0; + Second:=0; + Sec100:=0; + end; end;