
header "sys" as sys

+ fn microtime() uint {
    #if OS == win
    let tv = @stack(<sys:libc_timeval_fix>)
    gettimeofday(tv, null)
    #else
    let tv = @stack(<sys:libc_timeval>)
    sys:gettimeofday(tv, null)
    #end

    let time_in_micros : uint = 1000000 * tv.tv_sec.@cast(uint) + tv.tv_usec.@cast(uint)
    return time_in_micros
}

+ fn mstime() uint {
  return microtime() / 1000
}


#if OS == win
fn gettimeofday(tv: sys:libc_timeval_fix, tz: ?sys:libc_timezone) i32
{
  let delta_epoch_microsec : i64 = 11644473600000000
  let ft = @stack(<sys:libc_FILETIME>)
  let tmpres : i64 = 0;

  sys:GetSystemTimeAsFileTime(ft);

  tmpres = tmpres | ft.dwHighDateTime;
  tmpres = tmpres << 32;
  tmpres = tmpres | ft.dwLowDateTime;

  // convert into microseconds
  tmpres /= 10
  // converting file time to unix epoch
  tmpres -= delta_epoch_microsec;
  tv.tv_sec = (tmpres / 1000000)
  tv.tv_usec = (tmpres % 1000000)

  return 0;
}
#end

