blueloveTH %!s(int64=2) %!d(string=hai) anos
pai
achega
6ee43311ea
Modificáronse 2 ficheiros con 16 adicións e 1 borrados
  1. 5 1
      docs/modules/time.md
  2. 11 0
      src/pocketpy.h

+ 5 - 1
docs/modules/time.md

@@ -5,4 +5,8 @@ label: time
 
 ### `time.time()`
 
-Returns the current time in seconds since the epoch as a floating point number.
+Returns the current time in seconds since the epoch as a floating point number.
+
+### `time.sleep(secs)`
+
+Suspend execution of the calling thread for the given number of seconds.

+ 11 - 0
src/pocketpy.h

@@ -797,6 +797,17 @@ inline void add_module_time(VM* vm){
         auto now = std::chrono::high_resolution_clock::now();
         return VAR(std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count() / 1000000.0);
     });
+
+    vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) {
+        f64 seconds = FLOAT(args[0]);
+        auto begin = std::chrono::high_resolution_clock::now();
+        while(true){
+            auto now = std::chrono::high_resolution_clock::now();
+            f64 elapsed = std::chrono::duration_cast<std::chrono::microseconds>(now - begin).count() / 1000000.0;
+            if(elapsed >= seconds) break;
+        }
+        return vm->None;
+    });
 }
 
 inline void add_module_sys(VM* vm){