@@ -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