// test_time.el - native test suite for runtime/time.el // // Covers: time_now (positive timestamp), time_to_parts (UTC decomposition), // time_format (ISO 8601 and strftime tokens), time_add/diff, unix_timestamp, // duration helpers, and instant conversions. test "time-now-positive" { let ts: Int = time_now() // time_now() returns milliseconds since epoch. // Any value greater than 1_700_000_000_000 (Nov 2023) is valid in 2025+. assert ts > 1700000000000, "time_now returns a reasonable recent timestamp" } test "unix-timestamp-positive" { let s: Int = unix_timestamp() // Should be greater than 1_700_000_000 (seconds, Nov 2023) assert s > 1700000000, "unix_timestamp returns seconds > 1700000000" } test "now-ns-positive" { let ns: Int = now_ns() // Should be a large nanosecond value assert ns > 0, "now_ns returns positive value" } test "time-to-parts-epoch" { // Unix epoch: 0 ms = 1970-01-01T00:00:00.000Z let parts: String = time_to_parts(0) let year: String = json_get(parts, "year") let month: String = json_get(parts, "month") let day: String = json_get(parts, "day") let hour: String = json_get(parts, "hour") assert year == "1970", "epoch year is 1970" assert month == "1", "epoch month is 1" assert day == "1", "epoch day is 1" assert hour == "0", "epoch hour is 0" } test "time-to-parts-known-date" { // 2024-03-15T12:30:45.000Z // seconds = 2024-03-15 12:30:45 UTC // epoch ms: use a known value // 2024-03-15 00:00:00 UTC = 1710460800 seconds // + 12*3600 + 30*60 + 45 = 43200 + 1800 + 45 = 45045 seconds // total: 1710505845 seconds = 1710505845000 ms let ts: Int = 1710505845000 let parts: String = time_to_parts(ts) let year: String = json_get(parts, "year") let month: String = json_get(parts, "month") let day: String = json_get(parts, "day") let hour: String = json_get(parts, "hour") let minute: String = json_get(parts, "minute") let second: String = json_get(parts, "second") assert year == "2024", "year is 2024" assert month == "3", "month is 3 (March)" assert day == "15", "day is 15" assert hour == "12", "hour is 12" assert minute == "30", "minute is 30" assert second == "45", "second is 45" } test "time-format-iso" { // Epoch = 1970-01-01T00:00:00.000Z let formatted: String = time_format(0, "ISO") assert formatted == "1970-01-01T00:00:00.000Z", "epoch formats to ISO correctly" } test "time-format-iso-empty-fmt" { // Empty format string should also produce ISO 8601 let formatted: String = time_format(0, "") assert formatted == "1970-01-01T00:00:00.000Z", "empty fmt produces ISO" } test "time-format-strftime" { // 2024-03-15T12:30:45.000Z let ts: Int = 1710505845000 let fmt: String = time_format(ts, "%Y-%m-%d") assert fmt == "2024-03-15", "strftime %Y-%m-%d" let hms: String = time_format(ts, "%H:%M:%S") assert hms == "12:30:45", "strftime %H:%M:%S" } test "time-add-milliseconds" { let base: Int = 1000000 let result: Int = time_add(base, 500, "ms") assert result == 1000500, "add 500 ms" } test "time-add-seconds" { let base: Int = 0 let result: Int = time_add(base, 60, "sec") assert result == 60000, "add 60 seconds = 60000 ms" } test "time-add-minutes" { let base: Int = 0 let result: Int = time_add(base, 2, "min") assert result == 120000, "add 2 minutes = 120000 ms" } test "time-add-hours" { let base: Int = 0 let result: Int = time_add(base, 1, "hour") assert result == 3600000, "add 1 hour = 3600000 ms" } test "time-add-days" { let base: Int = 0 let result: Int = time_add(base, 1, "day") assert result == 86400000, "add 1 day = 86400000 ms" } test "time-diff-seconds" { let t1: Int = 0 let t2: Int = 90000 let d: Int = time_diff(t1, t2, "sec") assert d == 90, "diff 90000 ms = 90 seconds" } test "time-diff-negative" { let t1: Int = 5000 let t2: Int = 2000 let d: Int = time_diff(t1, t2, "sec") assert d == -3, "negative diff when t2 < t1" } test "duration-helpers" { let d_secs: Int = duration_seconds(5) assert d_secs == 5000000000, "5 seconds in nanoseconds" let d_ms: Int = duration_millis(100) assert d_ms == 100000000, "100 ms in nanoseconds" let d_ns: Int = duration_nanos(42) assert d_ns == 42, "42 nanos is identity" let back_secs: Int = duration_to_seconds(d_secs) assert back_secs == 5, "convert back to seconds" let back_ms: Int = duration_to_millis(d_ms) assert back_ms == 100, "convert back to milliseconds" } test "instant-conversions" { let inst: Int = unix_millis(1000) assert inst == 1000000000, "unix_millis(1000) in nanoseconds" let inst_secs: Int = unix_seconds(2) assert inst_secs == 2000000000, "unix_seconds(2) in nanoseconds" let back_ms: Int = instant_to_unix_millis(inst) assert back_ms == 1000, "instant to unix millis" let back_secs: Int = instant_to_unix_seconds(inst_secs) assert back_secs == 2, "instant to unix seconds" } test "time-from-parts" { // time_from_parts(secs, ns, tz) -> secs*1000 + ns/1_000_000 let ts: Int = time_from_parts(1000, 500000000, "UTC") assert ts == 1000500, "time_from_parts: 1000 secs + 500ms" }