Skip to content

Std.os.time #933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ set(ZIG_STD_FILES
"os/linux/index.zig"
"os/linux/x86_64.zig"
"os/path.zig"
"os/time.zig"
"os/epoch.zig"
"os/windows/error.zig"
"os/windows/index.zig"
"os/windows/util.zig"
Expand Down
18 changes: 18 additions & 0 deletions std/c/darwin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,28 @@ pub extern "c" fn _NSGetExecutablePath(buf: &u8, bufsize: &u32) c_int;

pub extern "c" fn __getdirentries64(fd: c_int, buf_ptr: &u8, buf_len: usize, basep: &i64) usize;

pub extern "c" fn mach_absolute_time() u64;
pub extern "c" fn mach_timebase_info(tinfo: ?&mach_timebase_info_data) void;

pub use @import("../os/darwin_errno.zig");

pub const _errno = __error;

pub const timeval = extern struct {
tv_sec: isize,
tv_usec: isize,
};

pub const timezone = extern struct {
tz_minuteswest: i32,
tz_dsttime: i32,
};

pub const mach_timebase_info_data = struct {
numer: u32,
denom: u32,
};

/// Renamed to Stat to not conflict with the stat function.
pub const Stat = extern struct {
dev: i32,
Expand Down
1 change: 1 addition & 0 deletions std/c/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub extern "c" fn dup2(old_fd: c_int, new_fd: c_int) c_int;
pub extern "c" fn readlink(noalias path: &const u8, noalias buf: &u8, bufsize: usize) isize;
pub extern "c" fn realpath(noalias file_name: &const u8, noalias resolved_name: &u8) ?&u8;
pub extern "c" fn sigprocmask(how: c_int, noalias set: &const sigset_t, noalias oset: ?&sigset_t) c_int;
pub extern "c" fn gettimeofday(tv: ?&timeval, tz: ?&timezone) c_int;
pub extern "c" fn sigaction(sig: c_int, noalias act: &const Sigaction, noalias oact: ?&Sigaction) c_int;
pub extern "c" fn nanosleep(rqtp: &const timespec, rmtp: ?&timespec) c_int;
pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int;
Expand Down
3 changes: 2 additions & 1 deletion std/fmt/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context),
},
's' => {
state = State.Buf;
},'.' => {
},
'.' => {
state = State.Float;
},
else => @compileError("Unknown format character: " ++ []u8{c}),
Expand Down
12 changes: 12 additions & 0 deletions std/os/darwin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ pub fn readlink(noalias path: &const u8, noalias buf_ptr: &u8, buf_len: usize) u
return errnoWrap(c.readlink(path, buf_ptr, buf_len));
}

pub fn gettimeofday(tv: ?&timeval, tz: ?&timezone) usize {
return errnoWrap(c.gettimeofday(tv, tz));
}

pub fn nanosleep(req: &const timespec, rem: ?&timespec) usize {
return errnoWrap(c.nanosleep(req, rem));
}
Expand Down Expand Up @@ -318,3 +322,11 @@ pub fn sigaddset(set: &sigset_t, signo: u5) void {
fn errnoWrap(value: isize) usize {
return @bitCast(usize, if (value == -1) -isize(*c._errno()) else value);
}


pub const timezone = c.timezone;
pub const timeval = c.timeval;
pub const mach_timebase_info_data = c.mach_timebase_info_data;

pub const mach_absolute_time = c.mach_absolute_time;
pub const mach_timebase_info = c.mach_timebase_info;
26 changes: 26 additions & 0 deletions std/os/epoch.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// Epoch reference times in terms of their difference from
/// posix epoch in seconds.
pub const posix = 0; //Jan 01, 1970 AD
pub const dos = 315532800; //Jan 01, 1980 AD
pub const ios = 978307200; //Jan 01, 2001 AD
pub const openvms = -3506716800; //Nov 17, 1858 AD
pub const zos = -2208988800; //Jan 01, 1900 AD
pub const windows = -11644473600; //Jan 01, 1601 AD
pub const amiga = 252460800; //Jan 01, 1978 AD
pub const pickos = -63244800; //Dec 31, 1967 AD
pub const gps = 315964800; //Jan 06, 1980 AD
pub const clr = -62135769600; //Jan 01, 0001 AD

pub const unix = posix;
pub const android = posix;
pub const os2 = dos;
pub const bios = dos;
pub const vfat = dos;
pub const ntfs = windows;
pub const ntp = zos;
pub const jbase = pickos;
pub const aros = amiga;
pub const morphos = amiga;
pub const brew = gps;
pub const atsc = gps;
pub const go = clr;
72 changes: 16 additions & 56 deletions std/os/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub const posix = switch(builtin.os) {
pub const ChildProcess = @import("child_process.zig").ChildProcess;
pub const path = @import("path.zig");
pub const File = @import("file.zig").File;
pub const time = @import("time.zig");

pub const FileMode = switch (builtin.os) {
Os.windows => void,
Expand Down Expand Up @@ -1356,50 +1357,6 @@ pub fn readLink(allocator: &Allocator, pathname: []const u8) ![]u8 {
}
}

pub fn sleep(seconds: usize, nanoseconds: usize) void {
switch(builtin.os) {
Os.linux, Os.macosx, Os.ios => {
posixSleep(u63(seconds), u63(nanoseconds));
},
Os.windows => {
const milliseconds = seconds * 1000 + nanoseconds / 1000000;
windows.Sleep(windows.DWORD(milliseconds));
},
else => @compileError("Unsupported OS"),
}
}

const u63 = @IntType(false, 63);
pub fn posixSleep(seconds: u63, nanoseconds: u63) void {
var req = posix.timespec {
.tv_sec = seconds,
.tv_nsec = nanoseconds,
};
var rem: posix.timespec = undefined;
while (true) {
const ret_val = posix.nanosleep(&req, &rem);
const err = posix.getErrno(ret_val);
if (err == 0) return;
switch (err) {
posix.EFAULT => unreachable,
posix.EINVAL => {
// Sometimes Darwin returns EINVAL for no reason.
// We treat it as a spurious wakeup.
return;
},
posix.EINTR => {
req = rem;
continue;
},
else => return,
}
}
}

test "os.sleep" {
sleep(0, 1);
}

pub fn posix_setuid(uid: u32) !void {
const err = posix.getErrno(posix.setuid(uid));
if (err == 0) return;
Expand Down Expand Up @@ -1753,18 +1710,21 @@ fn testWindowsCmdLine(input_cmd_line: &const u8, expected_args: []const []const
assert(it.next(debug.global_allocator) == null);
}

test "std.os" {
_ = @import("child_process.zig");
_ = @import("darwin_errno.zig");
_ = @import("darwin.zig");
_ = @import("get_user_id.zig");
_ = @import("linux/errno.zig");
//_ = @import("linux_i386.zig");
_ = @import("linux/x86_64.zig");
_ = @import("linux/index.zig");
_ = @import("path.zig");
_ = @import("windows/index.zig");
_ = @import("test.zig");
comptime {
if (builtin.is_test) {
_ = @import("child_process.zig");
_ = @import("darwin_errno.zig");
_ = @import("darwin.zig");
_ = @import("get_user_id.zig");
_ = @import("linux/errno.zig");
//_ = @import("linux_i386.zig");
_ = @import("linux/x86_64.zig");
_ = @import("linux/index.zig");
_ = @import("path.zig");
_ = @import("time.zig");
_ = @import("windows/index.zig");
_ = @import("test.zig");
}
}


Expand Down
20 changes: 20 additions & 0 deletions std/os/linux/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,26 @@ pub fn waitpid(pid: i32, status: &i32, options: i32) usize {
return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0);
}

pub fn clock_gettime(clk_id: i32, tp: &timespec) usize {
return syscall2(SYS_clock_gettime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
}

pub fn clock_getres(clk_id: i32, tp: &timespec) usize {
return syscall2(SYS_clock_getres, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
}

pub fn clock_settime(clk_id: i32, tp: &const timespec) usize {
return syscall2(SYS_clock_settime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
}

pub fn gettimeofday(tv: &timeval, tz: &timezone) usize {
return syscall2(SYS_gettimeofday, @ptrToInt(tv), @ptrToInt(tz));
}

pub fn settimeofday(tv: &const timeval, tz: &const timezone) usize {
return syscall2(SYS_settimeofday, @ptrToInt(tv), @ptrToInt(tz));
}

pub fn nanosleep(req: &const timespec, rem: ?&timespec) usize {
return syscall2(SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem));
}
Expand Down
10 changes: 10 additions & 0 deletions std/os/linux/x86_64.zig
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,16 @@ pub const timespec = extern struct {
tv_nsec: isize,
};

pub const timeval = extern struct {
tv_sec: isize,
tv_usec: isize,
};

pub const timezone = extern struct {
tz_minuteswest: i32,
tz_dsttime: i32,
};

pub const dirent = extern struct {
d_ino: usize,
d_off: usize,
Expand Down
Loading