@@ -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
20482001test "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