// Copyright 2026 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///|
extern "c" fn uv_hrtime() -> UInt64 = "moonbit_uv_hrtime"
///|
/// Returns the current high-resolution timestamp. This is expressed in
/// nanoseconds. It is relative to an arbitrary time in the past. It is not
/// related to the time of day and therefore not subject to clock drift. The
/// primary use is for measuring performance between intervals.
///
/// **Note:** Not every platform can support nanosecond resolution; however,
/// this value will always be in nanoseconds.
pub fn hrtime() -> UInt64 {
uv_hrtime()
}
///|
extern "c" fn uv_sleep(milliseconds : UInt) = "moonbit_uv_sleep"
///|
pub fn sleep(milliseconds : UInt) -> Unit {
uv_sleep(milliseconds)
}
///|
#owned(uv)
extern "c" fn uv_now(uv : Loop) -> UInt64 = "moonbit_uv_now"
///|
/// Return the current timestamp in milliseconds. The timestamp is cached at the
/// start of the event loop tick, see `uv_update_time()` for details and
/// rationale.
///
/// The timestamp increases monotonically from some arbitrary point in time.
/// Don't make assumptions about the starting point, you will only get
/// disappointed.
///
/// **Note:** Use `@uv.hrtime()` if you need sub-millisecond granularity.
pub fn Loop::now(self : Loop) -> UInt64 {
uv_now(self)
}
///|
#owned(uv)
extern "c" fn uv_update_time(uv : Loop) = "moonbit_uv_update_time"
///|
/// Update the event loop's concept of "now". Libuv caches the current time at
/// the start of the event loop tick in order to reduce the number of
/// time-related system calls.
///
/// You won't normally need to call this function unless you have callbacks that
/// block the event loop for longer periods of time, where "longer" is somewhat
/// subjective but probably on the order of a millisecond or more.
pub fn Loop::update_time(self : Loop) -> Unit {
uv_update_time(self)
}
///|
pub(all) enum ClockId {
Monotonic = 0
Realtime = 1
}
///|
#owned(ts)
extern "c" fn uv_clock_gettime(
clock_id : ClockId,
ts : FixedArray[Int64],
) -> Int = "moonbit_uv_clock_gettime"
///|
pub struct Timespec64(FixedArray[Int64])
///|
pub fn Timespec64::sec(self : Timespec64) -> Int64 {
self.0[0]
}
///|
pub fn Timespec64::nsec(self : Timespec64) -> Int {
self.0[1].to_int()
}
///|
pub fn clock_gettime(clock_id : ClockId) -> Timespec64 raise Errno {
let ts : FixedArray[Int64] = FixedArray::make(2, 0)
let result : Int = uv_clock_gettime(clock_id, ts)
if result < 0 {
raise Errno::of_int(result)
}
Timespec64(ts)
}
///|
pub struct Timeval64(FixedArray[Int64])
///|
pub fn Timeval64::sec(self : Timeval64) -> Int64 {
self.0[0]
}
///|
pub fn Timeval64::usec(self : Timeval64) -> Int {
self.0[1].to_int()
}
///|
#owned(tv)
extern "c" fn uv_gettimeofday(tv : FixedArray[Int64]) -> Int = "moonbit_uv_gettimeofday"
///|
pub fn gettimeofday() -> Timeval64 raise Errno {
let tv : FixedArray[Int64] = FixedArray::make(2, 0)
let result : Int = uv_gettimeofday(tv)
if result < 0 {
raise Errno::of_int(result)
}
Timeval64(tv)
}