From 602361e8e10ba97251c4483905173b3206ba4943 Mon Sep 17 00:00:00 2001 From: SirLynix Date: Wed, 28 Jun 2023 07:04:20 +0200 Subject: [PATCH] Core/Time: Add support for AsSeconds --- include/Nazara/Core/Time.inl | 13 ++++++++++--- tests/UnitTests/Engine/Core/TimeTest.cpp | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/Nazara/Core/Time.inl b/include/Nazara/Core/Time.inl index 2f35a1fe2..9bfa9ff0f 100644 --- a/include/Nazara/Core/Time.inl +++ b/include/Nazara/Core/Time.inl @@ -23,9 +23,16 @@ namespace Nz template constexpr T Time::AsSeconds() const { - static_assert(std::is_floating_point_v); - // TODO: Improve precision - return AsMicroseconds() / T(1'000'000.0) + (m_nanoseconds % 1000) / T(1'000'000'000); + if constexpr (std::is_floating_point_v) + { + // TODO: Improve precision + return AsMicroseconds() / T(1'000'000.0) + (m_nanoseconds % 1000) / T(1'000'000'000); + } + else + { + static_assert(std::is_integral_v); + return SafeCast(AsMicroseconds() / Int64(1'000'000)); + } } constexpr Int64 Time::AsMicroseconds() const diff --git a/tests/UnitTests/Engine/Core/TimeTest.cpp b/tests/UnitTests/Engine/Core/TimeTest.cpp index a25a6775f..cd97eb7f6 100644 --- a/tests/UnitTests/Engine/Core/TimeTest.cpp +++ b/tests/UnitTests/Engine/Core/TimeTest.cpp @@ -22,6 +22,7 @@ SCENARIO("Time", "[CORE][Time]") CHECK(time.AsMicroseconds() == 1'000'000); CHECK(time.AsMilliseconds() == 1'000); CHECK(time.AsSeconds() == 1.f); + CHECK(time.AsSeconds() == 1); CHECK(time == Nz::Time::Second()); CHECK(time == Nz::Time::Seconds(1.f)); CHECK(time == Nz::Time::Milliseconds(1'000));