Răsfoiți Sursa

add `modf` and `localtime`

BLUELOVETH 2 ani în urmă
părinte
comite
7531a7a77e
4 a modificat fișierele cu 32 adăugiri și 2 ștergeri
  1. 2 1
      docs/features/differences.md
  2. 2 0
      docs/modules/math.md
  3. 5 1
      docs/modules/time.md
  4. 23 0
      src/pocketpy.h

+ 2 - 1
docs/features/differences.md

@@ -41,4 +41,5 @@ The easiest way to test a feature is to [try it on your browser](https://pocketp
 5. `int` is not of unlimited precision. In 32 bit system, `int` and `float` is 30 bit; in 64 bit system, they are both 62 bit.
 6. `__ne__` is not required. Define `__eq__` is enough.
 7. Raw string cannot have boundary quotes in it, even escaped. See [#55](https://github.com/blueloveTH/pocketpy/issues/55).
-8. In a starred unpacked assignment, e.g. `a, b, *c = x`, the starred variable can only be presented in the last position. `a, *b, c = x` is not supported.
+8. In a starred unpacked assignment, e.g. `a, b, *c = x`, the starred variable can only be presented in the last position. `a, *b, c = x` is not supported.
+9. `a < b < c` does not work as you expected in cpython. Use `a < b and b < c` instead.

+ 2 - 0
docs/modules/math.md

@@ -120,5 +120,7 @@ Convert angle `x` from radians to degrees.
 Convert angle `x` from degrees to radians.
 
 
+### `math.modf(x)`
 
+Return the fractional and integer parts of `x`. Both results carry the sign of `x` and are floats.
 

+ 5 - 1
docs/modules/time.md

@@ -9,4 +9,8 @@ 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.
+Suspend execution of the calling thread for the given number of seconds.
+
+### `time.localtime()`
+
+Returns the current struct time as a `dict` object.

+ 23 - 0
src/pocketpy.h

@@ -1033,6 +1033,23 @@ inline void add_module_time(VM* vm){
         }
         return vm->None;
     });
+
+    vm->bind_func<0>(mod, "localtime", [](VM* vm, ArgsView args) {
+        auto now = std::chrono::system_clock::now();
+        std::time_t t = std::chrono::system_clock::to_time_t(now);
+        std::tm* tm = std::localtime(&t);
+        Dict d(vm);
+        d.set(VAR("tm_year"), VAR(tm->tm_year + 1900));
+        d.set(VAR("tm_mon"), VAR(tm->tm_mon + 1));
+        d.set(VAR("tm_mday"), VAR(tm->tm_mday));
+        d.set(VAR("tm_hour"), VAR(tm->tm_hour));
+        d.set(VAR("tm_min"), VAR(tm->tm_min));
+        d.set(VAR("tm_sec"), VAR(tm->tm_sec + 1));
+        d.set(VAR("tm_wday"), VAR((tm->tm_wday + 6) % 7));
+        d.set(VAR("tm_yday"), VAR(tm->tm_yday + 1));
+        d.set(VAR("tm_isdst"), VAR(tm->tm_isdst));
+        return VAR(std::move(d));
+    });
 }
 
 inline void add_module_sys(VM* vm){
@@ -1129,6 +1146,12 @@ inline void add_module_math(VM* vm){
     
     vm->bind_func<1>(mod, "degrees", CPP_LAMBDA(VAR(CAST_F(args[0]) * 180 / 3.1415926535897932384)));
     vm->bind_func<1>(mod, "radians", CPP_LAMBDA(VAR(CAST_F(args[0]) * 3.1415926535897932384 / 180)));
+
+    vm->bind_func<1>(mod, "modf", [](VM* vm, ArgsView args) {
+        f64 i;
+        f64 f = std::modf(CAST_F(args[0]), &i);
+        return VAR(Tuple({VAR(f), VAR(i)}));
+    });
 }
 
 inline void add_module_dis(VM* vm){