diff options
author | stijn <stinos@zoho.com> | 2014-05-08 10:56:33 +0200 |
---|---|---|
committer | stijn <stinos@zoho.com> | 2014-05-09 13:58:15 +0200 |
commit | 5ed284a15e028e0435f3b6e0773e14225d8f165d (patch) | |
tree | b83683ee2d84e423cc00cf630ebb688430a119c4 /unix/modtime.c | |
parent | d25cba4f642e50e2f713b48f5f079036b352f72d (diff) | |
download | micropython-5ed284a15e028e0435f3b6e0773e14225d8f165d.tar.gz micropython-5ed284a15e028e0435f3b6e0773e14225d8f165d.zip |
windows: Add modtime implementation
Diffstat (limited to 'unix/modtime.c')
-rw-r--r-- | unix/modtime.c | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/unix/modtime.c b/unix/modtime.c index 283b9dfd69..c497763bf3 100644 --- a/unix/modtime.c +++ b/unix/modtime.c @@ -36,6 +36,23 @@ #include "obj.h" #include "runtime.h" +#ifdef _WIN32 +void msec_sleep_tv(struct timeval *tv) { + msec_sleep(tv->tv_sec * 1000.0 + tv->tv_usec / 1000.0); +} +#define sleep_select(a,b,c,d,e) msec_sleep_tv((e)) +#else +#define sleep_select select +#endif + +#if CLOCKS_PER_SEC == 1000000 // POSIX +#define CLOCK_DIV 1000.0 +#elif CLOCKS_PER_SEC == 1000 // WIN32 +#define CLOCK_DIV 1.0 +#else +#error Unsupported clock() implementation +#endif + STATIC mp_obj_t mod_time_time() { #if MICROPY_ENABLE_FLOAT struct timeval tv; @@ -51,11 +68,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_time_obj, mod_time_time); // Note: this is deprecated since CPy3.3, but pystone still uses it. STATIC mp_obj_t mod_time_clock() { #if MICROPY_ENABLE_FLOAT - // POSIX requires CLOCKS_PER_SEC equals 1000000, so that's what we assume. // float cannot represent full range of int32 precisely, so we pre-divide // int to reduce resolution, and then actually do float division hoping // to preserve integer part resolution. - return mp_obj_new_float((float)(clock() / 1000) / 1000.0); + return mp_obj_new_float((float)(clock() / 1000) / CLOCK_DIV); #else return mp_obj_new_int((machine_int_t)clock()); #endif @@ -69,7 +85,7 @@ STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) { double ipart; tv.tv_usec = round(modf(val, &ipart) * 1000000); tv.tv_sec = ipart; - select(0, NULL, NULL, NULL, &tv); + sleep_select(0, NULL, NULL, NULL, &tv); #else sleep(mp_obj_get_int(arg)); #endif |