Core/Time: Fix operator<< with negative values not using units
This commit is contained in:
parent
369f273894
commit
5db0c4ed09
|
|
@ -11,7 +11,7 @@
|
||||||
#include <Nazara/Core/Algorithm.hpp>
|
#include <Nazara/Core/Algorithm.hpp>
|
||||||
#include <Nazara/Core/Config.hpp>
|
#include <Nazara/Core/Config.hpp>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <ostream>
|
#include <iosfwd>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
|
|
@ -73,7 +73,7 @@ namespace Nz
|
||||||
friend constexpr bool operator>(Time lhs, Time rhs);
|
friend constexpr bool operator>(Time lhs, Time rhs);
|
||||||
friend constexpr bool operator>=(Time lhs, Time rhs);
|
friend constexpr bool operator>=(Time lhs, Time rhs);
|
||||||
|
|
||||||
friend inline std::ostream& operator<<(std::ostream& out, Time time);
|
friend NAZARA_CORE_API std::ostream& operator<<(std::ostream& out, Time time);
|
||||||
|
|
||||||
friend inline bool Serialize(SerializationContext& context, Time time, TypeTag<Time>);
|
friend inline bool Serialize(SerializationContext& context, Time time, TypeTag<Time>);
|
||||||
friend inline bool Unserialize(SerializationContext& context, Time* time, TypeTag<Time>);
|
friend inline bool Unserialize(SerializationContext& context, Time* time, TypeTag<Time>);
|
||||||
|
|
|
||||||
|
|
@ -212,22 +212,6 @@ namespace Nz
|
||||||
return lhs.m_nanoseconds >= rhs.m_nanoseconds;
|
return lhs.m_nanoseconds >= rhs.m_nanoseconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::ostream& operator<<(std::ostream& out, Time time)
|
|
||||||
{
|
|
||||||
if (time > Time::Second())
|
|
||||||
return out << time.AsSeconds<double>() << "s";
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Int64 ns = time.AsNanoseconds();
|
|
||||||
if (time > Time::Millisecond())
|
|
||||||
return out << ns / 1'000'000.0 << "ms";
|
|
||||||
else if (time > Time::Microsecond())
|
|
||||||
return out << ns / 1'000.0 << "us";
|
|
||||||
else
|
|
||||||
return out << ns << "ns";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool Serialize(SerializationContext& context, Time time, TypeTag<Time>)
|
inline bool Serialize(SerializationContext& context, Time time, TypeTag<Time>)
|
||||||
{
|
{
|
||||||
if (!Serialize(context, time.m_nanoseconds))
|
if (!Serialize(context, time.m_nanoseconds))
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
#include <Nazara/Core/Time.hpp>
|
#include <Nazara/Core/Time.hpp>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||||
#include <Nazara/Core/Win32/TimeImpl.hpp>
|
#include <Nazara/Core/Win32/TimeImpl.hpp>
|
||||||
|
|
@ -31,6 +33,21 @@ namespace Nz
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& out, Time time)
|
||||||
|
{
|
||||||
|
Int64 ns = time.AsNanoseconds();
|
||||||
|
Int64 nsAbs = std::llabs(ns);
|
||||||
|
|
||||||
|
if (nsAbs > 1'000'000'000)
|
||||||
|
return out << time.AsSeconds<double>() << "s";
|
||||||
|
else if (nsAbs > 1'000'000)
|
||||||
|
return out << ns / 1'000'000.0 << "ms";
|
||||||
|
else if (nsAbs > 1'000)
|
||||||
|
return out << ns / 1'000.0 << "us";
|
||||||
|
else
|
||||||
|
return out << ns << "ns";
|
||||||
|
}
|
||||||
|
|
||||||
GetElapsedTimeFunction GetElapsedMilliseconds = PlatformImpl::GetElapsedMillisecondsImpl;
|
GetElapsedTimeFunction GetElapsedMilliseconds = PlatformImpl::GetElapsedMillisecondsImpl;
|
||||||
GetElapsedTimeFunction GetElapsedNanoseconds = NAZARA_ANONYMOUS_NAMESPACE_PREFIX(GetElapsedNanosecondsFirstRun);
|
GetElapsedTimeFunction GetElapsedNanoseconds = NAZARA_ANONYMOUS_NAMESPACE_PREFIX(GetElapsedNanosecondsFirstRun);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue