Commit f7662645 authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

unix: Implement time.sleep().

parent f7c2410e
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <math.h>
#include "misc.h"
#include "mpconfig.h"
......@@ -23,8 +25,24 @@ static mp_obj_t mod_time_clock() {
static MP_DEFINE_CONST_FUN_OBJ_0(mod_time_clock_obj, mod_time_clock);
static mp_obj_t mod_time_sleep(mp_obj_t arg) {
struct timeval tv;
machine_float_t val = mp_obj_get_float(arg);
double ipart;
tv.tv_usec = round(modf(val, &ipart) * 1000000);
tv.tv_sec = ipart;
select(0, NULL, NULL, NULL, &tv);
return mp_const_none;
static MP_DEFINE_CONST_FUN_OBJ_1(mod_time_sleep_obj, mod_time_sleep);
void time_init() {
mp_obj_t m = mp_obj_new_module(QSTR_FROM_STR_STATIC("time"));
rt_store_attr(m, QSTR_FROM_STR_STATIC("time"), (mp_obj_t)&mod_time_time_obj);
rt_store_attr(m, QSTR_FROM_STR_STATIC("clock"), (mp_obj_t)&mod_time_clock_obj);
rt_store_attr(m, QSTR_FROM_STR_STATIC("sleep"), (mp_obj_t)&mod_time_sleep_obj);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment