Skip to content

Commit 2a72acc

Browse files
committed
refactor(msgpack): remove timestamp now() method
- Remove Timestamp.now() method from API - Remove test for now() function - Replace custom Timer with std.time.Timer - Simplify fromNanos() test to use fixed values
1 parent 7e94449 commit 2a72acc

File tree

3 files changed

+9
-81
lines changed

3 files changed

+9
-81
lines changed

src/bench.zig

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,6 @@ const pack = msgpack.Pack(
1717
);
1818

1919
/// Benchmark timer helper
20-
const Timer = struct {
21-
start: i128,
22-
23-
fn begin() Timer {
24-
return .{ .start = std.time.nanoTimestamp() };
25-
}
26-
27-
fn end(self: Timer) u64 {
28-
const now = std.time.nanoTimestamp();
29-
return @intCast(now - self.start);
30-
}
31-
};
32-
3320
/// Run a benchmark and print results
3421
fn benchmark(
3522
comptime name: []const u8,
@@ -46,11 +33,11 @@ fn benchmark(
4633
}
4734

4835
// Actual benchmark
49-
const timer = Timer.begin();
36+
var timer = try std.time.Timer.start();
5037
for (0..iterations) |_| {
5138
try func(allocator);
5239
}
53-
const elapsed_ns = timer.end();
40+
const elapsed_ns = timer.read();
5441

5542
const avg_ns = elapsed_ns / iterations;
5643
const ops_per_sec = if (avg_ns > 0) (1_000_000_000 / avg_ns) else 0;

src/msgpack.zig

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub const Timestamp = struct {
203203

204204
/// Create timestamp from nanoseconds since Unix epoch
205205
/// This is useful for converting from various time sources
206-
/// Example: Timestamp.fromNanos(std.time.nanoTimestamp())
206+
/// Example: Timestamp.fromNanos(some_nanosecond_value)
207207
pub fn fromNanos(nanos: i128) Timestamp {
208208
const ns_i64: i64 = @intCast(@divFloor(nanos, std.time.ns_per_s));
209209
const nano_remainder: i64 = @intCast(@mod(nanos, std.time.ns_per_s));
@@ -214,18 +214,6 @@ pub const Timestamp = struct {
214214
};
215215
}
216216

217-
/// Get current timestamp (now)
218-
pub fn now() Timestamp {
219-
const ns = std.time.nanoTimestamp();
220-
const ns_i64: i64 = @intCast(@divFloor(ns, std.time.ns_per_s));
221-
const nano_remainder: i64 = @intCast(@mod(ns, std.time.ns_per_s));
222-
const nanoseconds: u32 = @intCast(if (nano_remainder < 0) nano_remainder + std.time.ns_per_s else nano_remainder);
223-
return Timestamp{
224-
.seconds = if (nano_remainder < 0) ns_i64 - 1 else ns_i64,
225-
.nanoseconds = nanoseconds,
226-
};
227-
}
228-
229217
/// Get total seconds as f64 (including fractional nanoseconds)
230218
pub fn toFloat(self: Timestamp) f64 {
231219
return @as(f64, @floatFromInt(self.seconds)) + @as(f64, @floatFromInt(self.nanoseconds)) / NANOSECONDS_PER_SECOND;

src/test.zig

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,63 +1997,16 @@ test "timestamp precision and conversion" {
19971997
try expect(@abs(float_val4 - expected4) < 0.000000001);
19981998
}
19991999

2000-
// Test timestamp now() function
2001-
test "timestamp now() function" {
2002-
var arr: [0xfffff]u8 = std.mem.zeroes([0xfffff]u8);
2003-
var write_buffer = fixedBufferStream(&arr);
2004-
var read_buffer = fixedBufferStream(&arr);
2005-
var p = pack.init(&write_buffer, &read_buffer);
2006-
2007-
// Get current timestamp
2008-
const now_ts = msgpack.Timestamp.now();
2009-
2010-
// Verify seconds is reasonable (after 2020-01-01 and before 2100-01-01)
2011-
const year_2020: i64 = 1577836800; // 2020-01-01 00:00:00 UTC
2012-
const year_2100: i64 = 4102444800; // 2100-01-01 00:00:00 UTC
2013-
try expect(now_ts.seconds > year_2020);
2014-
try expect(now_ts.seconds < year_2100);
2015-
2016-
// Verify nanoseconds is in valid range
2017-
try expect(now_ts.nanoseconds >= 0);
2018-
try expect(now_ts.nanoseconds <= 999_999_999);
2019-
2020-
// Test serialization and deserialization
2021-
const payload = msgpack.Payload{ .timestamp = now_ts };
2022-
try p.write(payload);
2023-
2024-
read_buffer = fixedBufferStream(&arr);
2025-
p = pack.init(&write_buffer, &read_buffer);
2026-
2027-
const decoded = try p.read(allocator);
2028-
defer decoded.free(allocator);
2029-
2030-
try expect(decoded == .timestamp);
2031-
try expect(decoded.timestamp.seconds == now_ts.seconds);
2032-
try expect(decoded.timestamp.nanoseconds == now_ts.nanoseconds);
2033-
2034-
// Test toFloat conversion
2035-
const float_val = now_ts.toFloat();
2036-
const expected_float = @as(f64, @floatFromInt(now_ts.seconds)) +
2037-
@as(f64, @floatFromInt(now_ts.nanoseconds)) / 1_000_000_000.0;
2038-
try expect(@abs(float_val - expected_float) < 0.000000001);
2039-
2040-
// Test that calling now() twice gives increasing or equal timestamps
2041-
const now_ts2 = msgpack.Timestamp.now();
2042-
const float1 = now_ts.toFloat();
2043-
const float2 = now_ts2.toFloat();
2044-
try expect(float2 >= float1);
2045-
}
2046-
20472000
// Test timestamp fromNanos() function
20482001
test "timestamp fromNanos() conversion" {
20492002
var arr: [0xfffff]u8 = std.mem.zeroes([0xfffff]u8);
20502003
var write_buffer = fixedBufferStream(&arr);
20512004
var read_buffer = fixedBufferStream(&arr);
20522005
var p = pack.init(&write_buffer, &read_buffer);
20532006

2054-
// Test with current time
2055-
const current_nanos = std.time.nanoTimestamp();
2056-
const ts = msgpack.Timestamp.fromNanos(current_nanos);
2007+
// Test with a fixed timestamp value (2024-01-01 00:00:00 UTC + 123456789 nanoseconds)
2008+
const test_nanos: i128 = 1704067200 * std.time.ns_per_s + 123456789;
2009+
const ts = msgpack.Timestamp.fromNanos(test_nanos);
20572010

20582011
// Verify seconds is reasonable (after 2020-01-01 and before 2100-01-01)
20592012
const year_2020: i64 = 1577836800;
@@ -2066,8 +2019,8 @@ test "timestamp fromNanos() conversion" {
20662019
try expect(ts.nanoseconds <= 999_999_999);
20672020

20682021
// Test with known values
2069-
const test_nanos: i128 = 1704067200_123456789; // 2024-01-01 00:00:00.123456789 UTC
2070-
const test_ts = msgpack.Timestamp.fromNanos(test_nanos);
2022+
const known_nanos: i128 = 1704067200_123456789; // 2024-01-01 00:00:00.123456789 UTC
2023+
const test_ts = msgpack.Timestamp.fromNanos(known_nanos);
20712024
try expect(test_ts.seconds == 1704067200);
20722025
try expect(test_ts.nanoseconds == 123456789);
20732026

@@ -2078,7 +2031,7 @@ test "timestamp fromNanos() conversion" {
20782031
try expect(negative_ts.nanoseconds == 500000000);
20792032

20802033
// Test Payload.timestampFromNanos() convenience method
2081-
const payload = msgpack.Payload.timestampFromNanos(current_nanos);
2034+
const payload = msgpack.Payload.timestampFromNanos(test_nanos);
20822035
try expect(payload == .timestamp);
20832036
try expect(payload.timestamp.seconds == ts.seconds);
20842037
try expect(payload.timestamp.nanoseconds == ts.nanoseconds);

0 commit comments

Comments
 (0)