Skip to content

Commit 9ad5eb9

Browse files
committed
feat(msgpack): add Timestamp.now() method
- Implement now() function to get current timestamp - Handle nanosecond precision with proper conversion - Add comprehensive test for now() functionality
1 parent 53abff3 commit 9ad5eb9

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/msgpack.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,18 @@ pub const Timestamp = struct {
201201
};
202202
}
203203

204+
/// Get current timestamp (now)
205+
pub fn now() Timestamp {
206+
const ns = std.time.nanoTimestamp();
207+
const ns_i64: i64 = @intCast(@divFloor(ns, std.time.ns_per_s));
208+
const nano_remainder: i64 = @intCast(@mod(ns, std.time.ns_per_s));
209+
const nanoseconds: u32 = @intCast(if (nano_remainder < 0) nano_remainder + std.time.ns_per_s else nano_remainder);
210+
return Timestamp{
211+
.seconds = if (nano_remainder < 0) ns_i64 - 1 else ns_i64,
212+
.nanoseconds = nanoseconds,
213+
};
214+
}
215+
204216
/// Get total seconds as f64 (including fractional nanoseconds)
205217
pub fn toFloat(self: Timestamp) f64 {
206218
return @as(f64, @floatFromInt(self.seconds)) + @as(f64, @floatFromInt(self.nanoseconds)) / NANOSECONDS_PER_SECOND;

src/test.zig

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,53 @@ 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+
20002047
// ============================================================================
20012048
// Additional tests from test_additional.zig
20022049
// ============================================================================

0 commit comments

Comments
 (0)