Skip to content
Draft
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
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.{
.name = .datetime,
.version = "0.8.0",
.version = "0.9.0",
.fingerprint = 0x93f3c6cacc579370, // Changing this has security and trust implications.

.minimum_zig_version = "0.16.0",
.paths = .{
"src",
"build.zig",
Expand Down
78 changes: 47 additions & 31 deletions src/datetime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,14 @@ pub const Date = struct {
}

// Returns todays date
pub fn now() Date {
return Date.fromTimestamp(time.milliTimestamp());
pub fn now(io: std.Io) error{ClockError}!Date {
const clock = std.Io.Clock.real;
const res = clock.resolution(io) catch {
return error.ClockError;
};
if (res.nanoseconds == 0) return error.ClockError;

return Date.fromTimestamp(clock.now(io).toMilliseconds());
}

// Create a date from the number of seconds since 1 Jan 1970
Expand Down Expand Up @@ -429,8 +435,8 @@ pub const Date = struct {
return std.fmt.bufPrint(buf, ISO_DATE_FMT, .{ self.year, self.month, self.day });
}

pub fn writeIso(self: Date, writer: anytype) !void {
try std.fmt.format(writer, ISO_DATE_FMT, .{ self.year, self.month, self.day });
pub fn writeIso(self: Date, writer: *std.Io.Writer) !void {
try writer.print(ISO_DATE_FMT, .{ self.year, self.month, self.day });
}

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -538,7 +544,7 @@ pub const Date = struct {
};

test "date-now" {
_ = Date.now();
_ = try Date.now(std.testing.io);
}

test "date-compare" {
Expand Down Expand Up @@ -732,10 +738,10 @@ test "date-write-iso" {

for (date_strs) |date_str| {
var buf: [32]u8 = undefined;
var stream = std.io.fixedBufferStream(buf[0..]);
var d = try Date.parseIso(date_str);
try d.writeIso(stream.writer());
try testing.expectEqualStrings(date_str, stream.getWritten());
var buf_writer = std.Io.Writer.fixed(&buf);
try d.writeIso(&buf_writer);
try testing.expectEqualStrings(date_str, buf_writer.buffered());
}
}

Expand Down Expand Up @@ -843,8 +849,13 @@ pub const Time = struct {
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
pub fn now() Time {
return Time.fromTimestamp(time.milliTimestamp());
pub fn now(io: std.Io) error{ClockError}!Time {
const clock = std.Io.Clock.real;
const res = clock.resolution(io) catch {
return error.ClockError;
};
if (res.nanoseconds == 0) return error.ClockError;
return Time.fromTimestamp(clock.now(io).toMilliseconds());
}

// Create a Time struct and validate that all fields are in range
Expand Down Expand Up @@ -978,12 +989,12 @@ pub const Time = struct {
const ISO_HM_FORMAT = "T{d:0>2}:{d:0>2}";
const ISO_HMS_FORMAT = "T{d:0>2}:{d:0>2}:{d:0>2}";

pub fn writeIsoHM(self: Time, writer: anytype) !void {
try std.fmt.format(writer, ISO_HM_FORMAT, .{ self.hour, self.minute });
pub fn writeIsoHM(self: Time, writer: *std.Io.Writer) !void {
try writer.print(ISO_HM_FORMAT, .{ self.hour, self.minute });
}

pub fn writeIsoHMS(self: Time, writer: anytype) !void {
try std.fmt.format(writer, ISO_HMS_FORMAT, .{ self.hour, self.minute, self.second });
pub fn writeIsoHMS(self: Time, writer: *std.Io.Writer) !void {
try writer.print(ISO_HMS_FORMAT, .{ self.hour, self.minute, self.second });
}
};

Expand All @@ -1001,7 +1012,7 @@ test "time-create" {
}

test "time-now" {
_ = Time.now();
_ = try Time.now(std.testing.io);
}

test "time-from-seconds" {
Expand Down Expand Up @@ -1061,22 +1072,20 @@ test "time-write-iso-hm" {
const t = Time.fromTimestamp(1574908586928);

var buf: [6]u8 = undefined;
var fbs = std.io.fixedBufferStream(buf[0..]);

try t.writeIsoHM(fbs.writer());
var writer = std.Io.Writer.fixed(&buf);

try testing.expectEqualSlices(u8, "T02:36", fbs.getWritten());
try t.writeIsoHM(&writer);
try testing.expectEqualSlices(u8, "T02:36", writer.buffered());
}

test "time-write-iso-hms" {
const t = Time.fromTimestamp(1574908586928);

var buf: [9]u8 = undefined;
var fbs = std.io.fixedBufferStream(buf[0..]);
var writer = std.Io.Writer.fixed(&buf);

try t.writeIsoHMS(fbs.writer());

try testing.expectEqualSlices(u8, "T02:36:26", fbs.getWritten());
try t.writeIsoHMS(&writer);
try testing.expectEqualSlices(u8, "T02:36:26", writer.buffered());
}

pub const Datetime = struct {
Expand Down Expand Up @@ -1156,8 +1165,13 @@ pub const Datetime = struct {
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
pub fn now() Datetime {
return Datetime.fromTimestamp(time.milliTimestamp());
pub fn now(io: std.Io) error{ClockError}!Datetime {
const clock = std.Io.Clock.real;
const res = clock.resolution(io) catch {
return error.ClockError;
};
if (res.nanoseconds == 0) return error.ClockError;
return Datetime.fromTimestamp(clock.now(io).toMilliseconds());
}

pub fn create(year: u32, month: u32, day: u32, hour: u32, minute: u32, second: u32, nanosecond: u32, zone: ?Timezone) !Datetime {
Expand Down Expand Up @@ -1505,7 +1519,7 @@ pub const Datetime = struct {
};

test "datetime-now" {
_ = Datetime.now();
_ = try Datetime.now(std.testing.io);
}

test "datetime-create-timestamp" {
Expand Down Expand Up @@ -1617,7 +1631,7 @@ test "datetime-compare" {
const dt2 = try Datetime.fromDate(2016, 12, 2);
try testing.expect(dt2.lt(dt1));

const dt3 = Datetime.now();
const dt3 = try Datetime.now(std.testing.io);
try testing.expect(dt3.gt(dt2));

const dt4 = try dt3.copy();
Expand Down Expand Up @@ -1679,10 +1693,12 @@ test "datetime-parse-modified-since" {
}

test "file-modified-date" {
var f = try std.fs.cwd().openFile("README.md", .{});
const stat = try f.stat();
const io = std.testing.io;
var f = try std.Io.Dir.cwd().openFile(io, "README.md", .{});
const stat = try f.stat(io);

var buf: [32]u8 = undefined;
const str = try Datetime.formatHttpFromModifiedDate(&buf, stat.mtime);
const str = try Datetime.formatHttpFromModifiedDate(&buf, stat.mtime.nanoseconds);
std.log.warn("Modtime: {s}\n", .{str});
}

Expand All @@ -1695,7 +1711,7 @@ test "readme-example" {
assert(next_year.day == 1);

// In UTC
const now = Datetime.now();
const now = try Datetime.now(std.testing.io);
const now_str = try now.formatHttp(allocator);
defer allocator.free(now_str);
std.log.warn("The time is now: {s}\n", .{now_str});
Expand Down