Ctrl K
ComunidadGuías…

std/time.zc

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
import "./core.zc"

include <time.h>
include <unistd.h>
include <sys/time.h>
include <stdlib.h>

@crepr("struct timeval")
struct timeval {
    tv_sec: i64;
    tv_usec: i64;
}

extern fn gettimeofday(tv: timeval*, tz: void*) -> int;
// Note: time() is declared by <time.h> which is already included above.
// Zen C does not redeclare it to avoid type conflicts (time_t varies by platform).

fn _time_now_impl() -> u64 {
    let tv: timeval;
    gettimeofday(&tv, NULL);
    return (u64)tv.tv_sec * 1000 + (u64)tv.tv_usec / 1000;
}

fn _z_time_time() -> i64 {
    return (i64)time(NULL);
}

struct Duration {
    millis: u64;
}

impl Duration {
    /// Creates a Duration from milliseconds.
    fn from_ms(ms: u64) -> Duration {
        return Duration { millis: ms };
    }
    
    /// Creates a Duration from seconds.
    fn from_secs(s: u64) -> Duration {
        return Duration { millis: s * (u64)1000 };
    }
}

struct Time {}

impl Time {
    /// Returns the current time in milliseconds.
    fn now() -> u64 {
        return _time_now_impl();
    }
    
    /// Sleeps for the given Duration.
    fn sleep(d: Duration) {
        usleep((u32)(d.millis * (u64)1000));
    }
    
    /// Sleeps for the given number of milliseconds.
    fn sleep_ms(ms: u64) {
        usleep((u32)(ms * (u64)1000));
    }
}