🚀 add os.time() to get the local time string

This commit is contained in:
ValKmjolnir 2022-06-12 19:08:31 +08:00
parent dea9c4ad3b
commit 6b20e0f167
5 changed files with 62 additions and 49 deletions

View File

@ -117,9 +117,11 @@ Or use this in __`linux/macOS/Unix`__
> [cpp compiler] -std=c++11 -O3 main.cpp -o nasal -fno-exceptions -ldl
Or using makefile,`mingw32-make` is __`Windows`__ platform's `make`:
Or using makefile,`mingw32-make` is __`Windows(MinGW-w64)`__ platform's `make`:
> mingw32-make nasal.exe
>
> mingw32-make.exe nasal.exe
in __`linux/macOS/Unix`__:

View File

@ -440,7 +440,8 @@ var dylib=
var os=
{
# get a string that tell which os it runs on.
platform: func(){return __builtin_platform;}
platform: func(){return __builtin_platform;},
time: func(){return __builtin_logtime; }
};
# runtime gives us some functions that we could manage it manually.

View File

@ -100,6 +100,7 @@ nas_native(builtin_costatus);
nas_native(builtin_corun);
nas_native(builtin_millisec);
nas_native(builtin_sysargv);
nas_native(builtin_logtime);
nasal_ref builtin_err(const char* func_name,const std::string& info)
{
@ -206,6 +207,7 @@ struct
{"__builtin_corun" ,builtin_corun },
{"__builtin_millisec",builtin_millisec},
{"__builtin_sysargv", builtin_sysargv },
{"__builtin_logtime", builtin_logtime },
{nullptr, nullptr }
};
@ -1454,4 +1456,14 @@ nasal_ref builtin_sysargv(nasal_ref* local,nasal_gc& gc)
res.vec().elems=gc.env_argv;
return res;
}
nasal_ref builtin_logtime(nasal_ref* local,nasal_gc& gc)
{
time_t t=time(nullptr);
tm* tm_t=localtime(&t);
char s[128];
sprintf(s,"%d-%.2d-%.2d %.2d:%.2d:%.2d",tm_t->tm_year+1900,tm_t->tm_mon+1,tm_t->tm_mday,tm_t->tm_hour,tm_t->tm_min,tm_t->tm_sec);
nasal_ref res=gc.alloc(vm_str);
res.str()=s;
return res;
}
#endif

View File

@ -10,9 +10,6 @@ println("-------------------------------------------------------------");
println(" See help using command line argument: --fg-env-help");
println("-------------------------------------------------------------");
var fg_env_global_timestamp=maketimestamp();
fg_env_global_timestamp.stamp();
var fg_env_cli={
"--fg-env-help":{
info:"get help",
@ -50,27 +47,27 @@ var fg_env_cli={
}
};
println("[\e[32m fg_env \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init begin");
println("[\e[32m maketimer \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init tasks");
println("[\e[32m maketimer \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init events");
println("[\e[32m fg_env \e[0m] [",os.time(),"] init begin");
println("[\e[32m maketimer \e[0m] [",os.time(),"] init tasks");
println("[\e[32m maketimer \e[0m] [",os.time(),"] init events");
var fg_globals={
task:{},
event:{}
};
println("[\e[32m maketimer \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] new func add_event(name,interval,function)");
println("[\e[32m maketimer \e[0m] [",os.time(),"] new func add_event(name,interval,function)");
var add_event=func(name,interval,function){
fg_globals.event[name]=coroutine.create(func{
var timestamp=maketimestamp();
timestamp.stamp();
while(timestamp.elapsedMSec()<interval*1000)
coroutine.yield();
println("[\e[32m",name,"\e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] type:\e[33mevent\e[0m interval:\e[34m",interval,"\e[0m");
println("[\e[32m",name,"\e[0m] [",os.time(),"] type:\e[33mevent\e[0m interval:\e[34m",interval,"\e[0m");
function();
});
}
println("[\e[32m maketimer \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] new func add_task(name,interval,function)");
println("[\e[32m maketimer \e[0m] [",os.time(),"] new func add_task(name,interval,function)");
var add_task=func(name,interval,function){
fg_globals.task[name]=coroutine.create(func{
var counter=0;
@ -80,26 +77,26 @@ var add_task=func(name,interval,function){
timestamp.stamp();
while(timestamp.elapsedMSec()<interval*1000)
coroutine.yield();
println("[\e[32m",name,"\e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] type:\e[34mtask\e[0m interval:\e[34m",interval,"\e[0m invoke-time:\e[96m",counter,"\e[0m");
println("[\e[32m",name,"\e[0m] [",os.time(),"] type:\e[34mtask\e[0m interval:\e[34m",interval,"\e[0m invoke-time:\e[96m",counter,"\e[0m");
function();
coroutine.yield();
}
});
}
println("[\e[32m maketimer \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] new func remove_task(name)");
println("[\e[32m maketimer \e[0m] [",os.time(),"] new func remove_task(name)");
var remove_task=func(name){
if(contains(fg_globals.task,name))
delete(fg_globals.task,name);
}
println("[\e[32m maketimer \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] new func remove_event(name)");
println("[\e[32m maketimer \e[0m] [",os.time(),"] new func remove_event(name)");
var remove_event=func(name){
if(contains(fg_globals.event,name))
delete(fg_globals.event,name);
}
println("[\e[32m maketimer \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] new func maketimer(interval,function)");
println("[\e[32m maketimer \e[0m] [",os.time(),"] new func maketimer(interval,function)");
var maketimer=func(interval,function){
var name="nasal-timer-";
var res={
@ -132,7 +129,7 @@ var maketimer=func(interval,function){
return res;
}
println("[\e[32m settimer \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] new func settimer(function,interval,rt)");
println("[\e[32m settimer \e[0m] [",os.time(),"] new func settimer(function,interval,rt)");
var settimer=func(){
var index=0;
return func(function,interval,realtime=1){
@ -142,7 +139,7 @@ var settimer=func(){
}
}();
println("[\e[32m maketimer \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] test func simulation()");
println("[\e[32m maketimer \e[0m] [",os.time(),"] test func simulation()");
var simulation=func(){
var running=1;
while(running){
@ -168,7 +165,7 @@ var simulation=func(){
}
}
println("[\e[32m maketimer \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] test func maketimer_multi_coroutine_test(size)");
println("[\e[32m maketimer \e[0m] [",os.time(),"] test func maketimer_multi_coroutine_test(size)");
var maketimer_multi_coroutine_test=func(coroutine_size){
if(coroutine_size<1)
return;
@ -210,14 +207,14 @@ var maketimer_multi_coroutine_test=func(coroutine_size){
simulation();
}
println("[\e[32m geodinfo \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init geodinfo(lat,lon)");
println("[\e[32m geodinfo \e[0m] [",os.time(),"] init geodinfo(lat,lon)");
var geodinfo=func(lat,lon){
return [nil,{
names:["Road","Freeway"]
}];
}
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init props");
println("[\e[32m props \e[0m] [",os.time(),"] init props");
var props=
{
globals:nil,
@ -240,7 +237,7 @@ var props=
}
};
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init props.Node");
println("[\e[32m props \e[0m] [",os.time(),"] init props.Node");
props.Node=
{
@ -356,7 +353,7 @@ props.Node=
};
var fg_env_props_node_traits=[props.Node];
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init props.globals");
println("[\e[32m props \e[0m] [",os.time(),"] init props.globals");
props.globals=props.Node.new();
var c=['aircraft','ai','models','position','orientation','controls','sim','consumables','engines','velocities','accelerations','gear','instrumentation','rotors'];
foreach(var i;c)
@ -368,7 +365,7 @@ props.getNode('/models',1).addChildren('building',4);
for(var i=0;i<4;i+=1)
props.getNode('/models/building['~i~']',1).setIntValue(i);
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /consumables");
println("[\e[32m props \e[0m] [",os.time(),"] init /consumables");
props.getNode("/consumables",1).addChild("fuel");
props.getNode("/consumables/fuel",1).addChild("total-fuel-lbs");
props.getNode("/consumables/fuel",1).addChild("total-gal_us");
@ -389,11 +386,11 @@ for(var i=0;i<4;i+=1){
props.getNode("/consumables/fuel/tank["~i~"]/density-ppg",1).setValue('/',0);
}
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /controls");
println("[\e[32m props \e[0m] [",os.time(),"] init /controls");
foreach(var i;['anti-ice','APU','armament','autoflight','electric','engines','flight','fuel','gear','hydraulic','lighting','pneumatic','pressurization','seat'])
props.getNode("/controls",1).addChild(i);
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /controls/anti-ice");
println("[\e[32m props \e[0m] [",os.time(),"] init /controls/anti-ice");
foreach(var i;['wing-heat','pitot-heat','wiper','window-heat'])
props.getNode("/controls/anti-ice",1).addChild(i);
props.getNode("/controls/anti-ice",1).addChildren("engine",2);
@ -402,11 +399,11 @@ for(var i=0;i<2;i+=1){
props.getNode("/controls/anti-ice/engine["~i~"]",1).addChild("inlet-heat");
}
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /controls/APU");
println("[\e[32m props \e[0m] [",os.time(),"] init /controls/APU");
props.getNode("/controls/APU",1).addChild("off-start-run");
props.getNode("/controls/APU",1).addChild("fire-switch");
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /controls/armament");
println("[\e[32m props \e[0m] [",os.time(),"] init /controls/armament");
props.getNode("/controls/armament",1).addChild("master-arm");
props.getNode("/controls/armament",1).addChild("station-select");
props.getNode("/controls/armament",1).addChild("release-all");
@ -418,14 +415,14 @@ for(var i=0;i<4;i+=1){
props.getNode("/controls/armament/station["~i~"]",1).addChild("jettison-all");
}
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /controls/autoflight");
println("[\e[32m props \e[0m] [",os.time(),"] init /controls/autoflight");
foreach(var i;['autothrottle-arm','autothrottle-engage','heading-select','altitude-select','bank-angle-select','vertical-speed-select','speed-select','mach-select','vertical-mode','lateral-mode'])
props.getNode("/controls/autoflight",1).addChild(i);
props.getNode("/controls/autoflight",1).addChildren("autopilot",2);
for(var i=0;i<2;i+=1)
props.getNode("/controls/autoflight/autopilot["~i~"]",1).addChild("engage");
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /controls/electric");
println("[\e[32m props \e[0m] [",os.time(),"] init /controls/electric");
foreach(var i;['battery-switch','external-power','APU-generator'])
props.getNode("/controls/electric",1).addChild(i);
props.getNode("/controls/electric",1).addChildren("engine",2);
@ -434,18 +431,18 @@ for(var i=0;i<2;i+=1){
props.getNode("/controls/electric/engine["~i~"]",1).addChild("bus-tie");
}
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /controls/engines");
println("[\e[32m props \e[0m] [",os.time(),"] init /controls/engines");
props.getNode("/controls/engines",1).addChild("throttle-idle");
props.getNode("/controls/engines",1).addChildren("engine",2);
for(var i=0;i<2;i+=1)
foreach(var j;['throttle','starter','fuel-pump','fire-switch','fire-bottle-discharge','cutoff','mixture','propeller-pitch','magnetos','boost','WEP','cowl-flaps-norm','feather','ignition','augmentation','afterburner','reverser','water-injection','condition'])
props.getNode("/controls/engines/engine["~i~"]",1).addChild(j);
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /controls/flight");
println("[\e[32m props \e[0m] [",os.time(),"] init /controls/flight");
foreach(var i;['aileron','aileron-trim','elevator','elevator-trim','rudder','rudder-trim','flaps','slats','BLC','spoilers','speedbrake','wing-sweep','wing-fold','drag-chute'])
props.getNode("/controls/flight",1).addChild(i);
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /controls/fuel");
println("[\e[32m props \e[0m] [",os.time(),"] init /controls/fuel");
props.getNode("/controls/fuel",1).addChild("dump-value");
props.getNode("/controls/fuel",1).addChildren("tank",4);
for(var i=0;i<4;i+=1){
@ -454,38 +451,38 @@ for(var i=0;i<4;i+=1){
props.getNode("/controls/fuel/tank["~i~"]",1).addChildren("boost-pump",4);
}
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /controls/gear");
println("[\e[32m props \e[0m] [",os.time(),"] init /controls/gear");
foreach(var i;['brake-left','brake-right','brake-parking','steering','gear-down','antiskid','tailhook','tailwheel-lock'])
props.getNode("/controls/gear",1).addChild(i);
props.getNode("/controls/gear",1).addChildren("wheel",4);
for(var i=0;i<4;i+=1)
props.getNode("/controls/gear/wheel["~i~"]",1).addChild("alternate-extension");
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /controls/hydraulic");
println("[\e[32m props \e[0m] [",os.time(),"] init /controls/hydraulic");
props.getNode("/controls/hydraulic",1).addChildren("system",2);
for(var i=0;i<2;i+=1){
props.getNode("/controls/hydraulic/system["~i~"]",1).addChild("engine-pump");
props.getNode("/controls/hydraulic/system["~i~"]",1).addChild("electric-pump");
}
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /controls/lighting");
println("[\e[32m props \e[0m] [",os.time(),"] init /controls/lighting");
foreach(var i;['landing-lights','turn-off-lights','formation-lights','taxi-light','logo-lights','nav-lights','beacon','strobe','panel-norm','instruments-norm','dome-norm'])
props.getNode("/controls/lighting",1).addChild(i);
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /controls/pneumatic");
println("[\e[32m props \e[0m] [",os.time(),"] init /controls/pneumatic");
props.getNode("/controls/pneumatic",1).addChild("APU-bleed");
props.getNode("/controls/pneumatic",1).addChildren("engine",2);
for(var i=0;i<2;i+=1)
props.getNode("/controls/pneumatic/engine["~i~"]",1).addChild("bleed");
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /controls/pressurization");
println("[\e[32m props \e[0m] [",os.time(),"] init /controls/pressurization");
foreach(var i;['mode','dump','outflow-valve'])
props.getNode("/controls/pressurization",1).addChild(i);
props.getNode("/controls/pressurization",1).addChildren("pack",4);
for(var i=0;i<4;i+=1)
props.getNode("/controls/pressurization/pack["~i~"]",1).addChild("pack-on");
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /controls/seat");
println("[\e[32m props \e[0m] [",os.time(),"] init /controls/seat");
foreach(var i;['vertical-adjust','fore-aft-adjust','cmd_selector_valve'])
props.getNode("/controls/seat",1).addChild(i);
props.getNode("/controls/seat",1).addChildren("eject",3);
@ -494,7 +491,7 @@ for(var i=0;i<3;i+=1){
props.getNode("/controls/seat/eject["~i~"]",1).addChild("status");
}
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /engines");
println("[\e[32m props \e[0m] [",os.time(),"] init /engines");
props.getNode("/engines",1).addChildren("engine",2);
for(var i=0;i<2;i+=1)
foreach(var j;
@ -504,19 +501,19 @@ for(var i=0;i<2;i+=1)
'oil-pressure-psi','cht-degf','rpm','pitch','torque'])
props.getNode("/engines/engine["~i~"]",1).addChild(j);
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /position");
println("[\e[32m props \e[0m] [",os.time(),"] init /position");
foreach(var i;['','altitude-agl-ft','altitude-ft','ground-elev-ft','ground-elev-m','latitude-deg','latitude-string','longitude-deg','longitude-string','sea-level-radius-ft'])
props.getNode("/position",1).addChild(i);
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /orientation");
println("[\e[32m props \e[0m] [",os.time(),"] init /orientation");
foreach(var i;['roll-deg','pitch-deg','heading-deg','roll-rate-degps','pitch-rate-degps','yaw-rate-degps','side-slip-rad','side-slip-deg','alpha-deg'])
props.getNode("/orientation",1).addChild(i);
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /velocities");
println("[\e[32m props \e[0m] [",os.time(),"] init /velocities");
foreach(var i;['airspeed-kt','mach','speed-north-fps','speed-east-fps','speed-down-fps','uBody-fps','vBody-fps','wBody-fps','vertical-speed-fps','glideslope'])
props.getNode("/velocities",1).addChild(i);
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /accelerations");
println("[\e[32m props \e[0m] [",os.time(),"] init /accelerations");
foreach(var i;['nlf','ned','pilot'])
props.getNode("/accelerations",1).addChild(i);
foreach(var i;['north-accel-fps_sec','east-accel-fps_sec','down-accel-fps_sec'])
@ -524,18 +521,18 @@ foreach(var i;['north-accel-fps_sec','east-accel-fps_sec','down-accel-fps_sec'])
foreach(var i;['x-accel-fps_sec','y-accel-fps_sec','z-accel-fps_sec'])
props.getNode("/accelerations/pilot",1).addChild(i);
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /gear");
println("[\e[32m props \e[0m] [",os.time(),"] init /gear");
props.getNode("/gear",1).addChild("serviceable");
props.getNode("/gear",1).addChildren("gear",4);
for(var i=0;i<4;i+=1)
foreach(var j;['cast-angle-deg','compression-m','compression-norm','ground-friction-factor','ground-is-solid','has-brake','rollspeed-ms','wow','xoffset-in','yoffset-in','zoffset-in'])
props.getNode("/gear/gear["~i~"]",1).addChild(j);
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /instrumentation");
println("[\e[32m props \e[0m] [",os.time(),"] init /instrumentation");
foreach(var i;['adf','airspeed-indicator','altimeter','annunciator','clock','comm','comm[1]','dme','efis','encoder','flightdirector','gps','gps-annunciator','heading-indicator','heading-indicator-fg','magnetic-compass','marker-beacon','nav','nav[1]','radar','slip-skid-ball','tacan','transponder','turn-indicator','vertical-speed-indicator','wxradar'])
props.getNode("/instrumentation",1).addChild(i);
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init /rotors");
println("[\e[32m props \e[0m] [",os.time(),"] init /rotors");
foreach(var i;['gear','{name}'])
props.getNode("/rotors",1).addChild(i);
foreach(var i;['torque-sound-filtered','total-torque'])
@ -579,8 +576,8 @@ func(){
}
}();
println("[\e[32m props \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init done");
println("[\e[32m fg_env \e[0m] [",fg_env_global_timestamp.elapsedMSec()/1000,"] init done");
println("[\e[32m props \e[0m] [",os.time(),"] init done");
println("[\e[32m fg_env \e[0m] [",os.time(),"] init done");
println("-------------------------------------------------------------");
foreach(var a;runtime.argv())

View File

@ -440,7 +440,8 @@ var dylib=
var os=
{
# get a string that tell which os it runs on.
platform: func(){return __builtin_platform;}
platform: func(){return __builtin_platform;},
time: func(){return __builtin_logtime; }
};
# runtime gives us some functions that we could manage it manually.