From 1100e8319284f67dcbf03cb3852b8473496bed70 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sat, 6 Jan 2024 17:33:42 +0000 Subject: [PATCH] Date/time: Ensure clock-face conversions correct for negative TickMinutes --- src/date_type.h | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/date_type.h b/src/date_type.h index 74266f046a..ea6126e07d 100644 --- a/src/date_type.h +++ b/src/date_type.h @@ -71,11 +71,21 @@ using DateTicksScaledDelta = StrongType::Typedef>; /* Mixin for TickMinutes, ClockFaceMinutes */ +template struct MinuteOperations { template struct mixin { private: - TBaseType GetBase() const { return static_cast(*this).base(); } + TBaseType GetBase() const + { + TBaseType value = static_cast(*this).base(); + if constexpr (TNegativeCheck) { + if (value < 0) { + value = (value % 1440) + 1440; + } + } + return value; + } public: int ClockMinute() const { return this->GetBase() % 60; } @@ -96,7 +106,7 @@ struct ClockFaceMinuteOperations { }; /* The type to store general clock-face minutes in (i.e. 0..1440) */ -using ClockFaceMinutes = StrongType::Typedef; +using ClockFaceMinutes = StrongType::Typedef, ClockFaceMinuteOperations>; /* Mixin for TickMinutes */ struct TickMinuteOperations { @@ -122,7 +132,7 @@ struct TickMinuteOperations { }; /* The type to store DateTicksScaled-based minutes in */ -using TickMinutes = StrongType::Typedef; +using TickMinutes = StrongType::Typedef, TickMinuteOperations>; #define DATE_UNIT_SIZE (_settings_time.time_in_minutes ? _settings_time.ticks_per_minute : (DAY_TICKS * _settings_game.economy.day_length_factor))