-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.zig
54 lines (44 loc) · 1.27 KB
/
utils.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const std = @import("std");
const Tui = @import("tui.zig");
const fs = std.fs;
const fmt = std.fmt;
const mem = std.mem;
const unicode = std.unicode;
const libc = @cImport({
@cInclude("time.h");
});
pub const os = @import("./utils/os.zig");
pub const log = @import("./utils/log.zig");
pub const string = @import("./utils/string.zig");
pub const BufferWriter = @import("./utils/BufferWriter.zig");
pub const eql = string.eql;
pub const lpad = string.lpad;
pub const split = string.split;
pub const repeat = string.repeat;
pub const CharArray = std.ArrayList(u8);
pub fn strftime(format: []const u8, sec: isize, buf: []u8) []u8 {
const time_info = libc.localtime(&sec);
const wlen = libc.strftime(
buf.ptr,
buf.len,
@ptrCast(format.ptr),
time_info,
);
return buf[0..wlen];
}
pub fn isCurrentYear(sec: isize) bool {
const now = libc.time(null);
return year(sec) == year(now);
}
fn year(sec: isize) isize {
return @divFloor(sec, 3600 * 24 * 365) + 1970;
}
pub fn FieldType(comptime T: type, comptime name: []const u8) type {
comptime std.debug.assert(@hasField(T, name));
inline for (@typeInfo(T).Struct.fields) |f| {
if (std.mem.eql(u8, f.name, name)) {
return f.type;
}
}
unreachable;
}