Skip to content

Commit

Permalink
time: reduce the diff for v run cmd/tools/check_os_api_parity time
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Jul 31, 2023
1 parent 618961f commit 2cd5b8a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 50 deletions.
12 changes: 12 additions & 0 deletions vlib/time/time.c.v
Expand Up @@ -135,3 +135,15 @@ pub fn (t Time) strftime(fmt string) string {
C.strftime(&buf[0], usize(sizeof(buf)), fmt_c, tm)
return unsafe { cstring_to_vstring(&char(&buf[0])) }
}

// some *nix system functions (e.g. `C.poll()`, C.epoll_wait()) accept an `int`
// value as *timeout in milliseconds* with the special value `-1` meaning "infinite"
pub fn (d Duration) sys_milliseconds() int {
if d > 2147483647 * millisecond { // treat 2147483647000001 .. C.INT64_MAX as "infinite"
return -1
} else if d <= 0 {
return 0 // treat negative timeouts as 0 - consistent with Unix behaviour
} else {
return int(d / millisecond)
}
}
4 changes: 2 additions & 2 deletions vlib/time/time_darwin.c.v
Expand Up @@ -88,11 +88,11 @@ fn darwin_utc() Time {
}

// dummy to compile with all compilers
pub fn solaris_now() Time {
fn solaris_now() Time {
return Time{}
}

// dummy to compile with all compilers
pub fn solaris_utc() Time {
fn solaris_utc() Time {
return Time{}
}
8 changes: 4 additions & 4 deletions vlib/time/time_linux.c.v
Expand Up @@ -6,21 +6,21 @@ fn sys_mono_now_darwin() u64 {
}

// darwin_now - dummy fn to compile on all platforms/compilers
pub fn darwin_now() Time {
fn darwin_now() Time {
return Time{}
}

// solaris_now - dummy fn to compile on all platforms/compilers
pub fn solaris_now() Time {
fn solaris_now() Time {
return Time{}
}

// darwin_utc - dummy fn to compile on all platforms/compilers
pub fn darwin_utc() Time {
fn darwin_utc() Time {
return Time{}
}

// solaris_utc - dummy fn to compile on all platforms/compilers
pub fn solaris_utc() Time {
fn solaris_utc() Time {
return Time{}
}
25 changes: 2 additions & 23 deletions vlib/time/time_nix.c.v
Expand Up @@ -95,12 +95,12 @@ fn linux_utc() Time {
}

// dummy to compile with all compilers
pub fn win_now() Time {
fn win_now() Time {
return Time{}
}

// dummy to compile with all compilers
pub fn win_utc() Time {
fn win_utc() Time {
return Time{}
}

Expand All @@ -125,15 +125,6 @@ pub fn (d Duration) timespec() C.timespec {
return ts
}

// zero_timespec returns the calendar time in seconds and nanoseconds of the beginning of the Unix epoch.
pub fn zero_timespec() C.timespec {
ts := C.timespec{
tv_sec: 0
tv_nsec: 0
}
return ts
}

// sleep suspends the execution of the calling thread for a given duration (in nanoseconds).
pub fn sleep(duration Duration) {
mut req := C.timespec{duration / second, duration % second}
Expand All @@ -147,15 +138,3 @@ pub fn sleep(duration Duration) {
}
}
}

// some *nix system functions (e.g. `C.poll()`, C.epoll_wait()) accept an `int`
// value as *timeout in milliseconds* with the special value `-1` meaning "infinite"
pub fn (d Duration) sys_milliseconds() int {
if d > C.INT32_MAX * millisecond { // treat 2147483647000001 .. C.INT64_MAX as "infinite"
return -1
} else if d <= 0 {
return 0 // treat negative timeouts as 0 - consistent with Unix behaviour
} else {
return int(d / millisecond)
}
}
4 changes: 2 additions & 2 deletions vlib/time/time_solaris.c.v
Expand Up @@ -22,11 +22,11 @@ fn solaris_utc() Time {
}

// dummy to compile with all compilers
pub fn darwin_now() Time {
fn darwin_now() Time {
return Time{}
}

// dummy to compile with all compilers
pub fn darwin_utc() Time {
fn darwin_utc() Time {
return Time{}
}
26 changes: 7 additions & 19 deletions vlib/time/time_windows.c.v
Expand Up @@ -171,7 +171,7 @@ fn win_utc() Time {
}

// unix_time returns Unix time.
pub fn (st SystemTime) unix_time() i64 {
fn (st SystemTime) unix_time() i64 {
tt := C.tm{
tm_sec: st.second
tm_min: st.minute
Expand All @@ -184,32 +184,32 @@ pub fn (st SystemTime) unix_time() i64 {
}

// dummy to compile with all compilers
pub fn darwin_now() Time {
fn darwin_now() Time {
return Time{}
}

// dummy to compile with all compilers
pub fn linux_now() Time {
fn linux_now() Time {
return Time{}
}

// dummy to compile with all compilers
pub fn solaris_now() Time {
fn solaris_now() Time {
return Time{}
}

// dummy to compile with all compilers
pub fn darwin_utc() Time {
fn darwin_utc() Time {
return Time{}
}

// dummy to compile with all compilers
pub fn linux_utc() Time {
fn linux_utc() Time {
return Time{}
}

// dummy to compile with all compilers
pub fn solaris_utc() Time {
fn solaris_utc() Time {
return Time{}
}

Expand All @@ -223,15 +223,3 @@ pub struct C.timeval {
pub fn sleep(duration Duration) {
C.Sleep(int(duration / millisecond))
}

// some Windows system functions (e.g. `C.WaitForSingleObject()`) accept an `u32`
// value as *timeout in milliseconds* with the special value `u32(-1)` meaning "infinite"
pub fn (d Duration) sys_milliseconds() u32 {
if d >= u32(-1) * millisecond { // treat 4294967295000000 .. C.INT64_MAX as "infinite"
return u32(-1)
} else if d <= 0 {
return 0 // treat negative timeouts as 0 - consistent with Unix behaviour
} else {
return u32(d / millisecond)
}
}

0 comments on commit 2cd5b8a

Please sign in to comment.