From 7531a7a77e9d8625733054eae9612666f7c4241e Mon Sep 17 00:00:00 2001 From: BLUELOVETH Date: Tue, 30 May 2023 19:44:45 +0800 Subject: [PATCH] add `modf` and `localtime` --- docs/features/differences.md | 3 ++- docs/modules/math.md | 2 ++ docs/modules/time.md | 6 +++++- src/pocketpy.h | 23 +++++++++++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/docs/features/differences.md b/docs/features/differences.md index 808e8103..7d32067c 100644 --- a/docs/features/differences.md +++ b/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. \ No newline at end of file +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. \ No newline at end of file diff --git a/docs/modules/math.md b/docs/modules/math.md index 94362a40..75f6dbe7 100644 --- a/docs/modules/math.md +++ b/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. diff --git a/docs/modules/time.md b/docs/modules/time.md index 7a8afe28..6caf0649 100644 --- a/docs/modules/time.md +++ b/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. \ No newline at end of file +Suspend execution of the calling thread for the given number of seconds. + +### `time.localtime()` + +Returns the current struct time as a `dict` object. \ No newline at end of file diff --git a/src/pocketpy.h b/src/pocketpy.h index 4f1599d0..2474fb4d 100644 --- a/src/pocketpy.h +++ b/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){