add modf and localtime

This commit is contained in:
BLUELOVETH 2023-05-30 19:44:45 +08:00
parent d987b70873
commit 7531a7a77e
4 changed files with 32 additions and 2 deletions

View File

@ -42,3 +42,4 @@ The easiest way to test a feature is to [try it on your browser](https://pocketp
6. `__ne__` is not required. Define `__eq__` is enough. 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). 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.

View File

@ -120,5 +120,7 @@ Convert angle `x` from radians to degrees.
Convert angle `x` from degrees to radians. 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.

View File

@ -10,3 +10,7 @@ Returns the current time in seconds since the epoch as a floating point number.
### `time.sleep(secs)` ### `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.

View File

@ -1033,6 +1033,23 @@ inline void add_module_time(VM* vm){
} }
return vm->None; 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){ 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, "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, "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){ inline void add_module_dis(VM* vm){