From f6f2c67f3755028d84b331f6e96ac4c4963dc78b Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Tue, 3 Sep 2024 00:40:51 -0400 Subject: [PATCH] Add `time.time_to_datetime` --- core/time/time.odin | 18 ++++++++++++++++++ tests/core/time/test_core_time.odin | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/core/time/time.odin b/core/time/time.odin index 5903b212d..98639b36a 100644 --- a/core/time/time.odin +++ b/core/time/time.odin @@ -954,6 +954,24 @@ Convert datetime components into time. */ datetime_to_time :: proc{components_to_time, compound_to_time} +/* +Convert time into datetime. +*/ +time_to_datetime :: proc "contextless" (t: Time) -> (dt.DateTime, bool) { + unix_epoch := dt.DateTime{{1970, 1, 1}, {0, 0, 0, 0}} + + datetime, err := dt.add(unix_epoch, dt.Delta{ nanos = t._nsec }) + if err != .None { + return {}, false + } + return datetime, true +} + +/* +Alias for `time_to_datetime`. +*/ +time_to_compound :: time_to_datetime + /* Check if a year is a leap year. */ diff --git a/tests/core/time/test_core_time.odin b/tests/core/time/test_core_time.odin index c408bc582..51955b1c4 100644 --- a/tests/core/time/test_core_time.odin +++ b/tests/core/time/test_core_time.odin @@ -253,6 +253,31 @@ test_parse_iso8601_string :: proc(t: ^testing.T) { } } +@test +test_time_to_datetime_roundtrip :: proc(t: ^testing.T) { + // Roundtrip a time through `time_to_datetime` to `DateTime` and back. + // Select `N` evenly-distributed points throughout the positive signed 64-bit number line. + N :: 1024 + for i in 0..=i64(N) { + n := i * (max(i64) / N) + x := time.unix(0, n) + + y, ttd_err := time.time_to_datetime(x) + testing.expectf(t, ttd_err, + "Time<%i> failed to convert to DateTime", + n) or_continue + + z, dtt_err := time.datetime_to_time(y) + testing.expectf(t, dtt_err, + "DateTime<%v> failed to convert to Time", + y) or_continue + + testing.expectf(t, x == z, + "Roundtrip conversion of Time to DateTime and back failed: got %v, expected %v", + z, x) + } +} + MONTH_DAYS := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} YEAR_START :: 1900 YEAR_END :: 2024