From e6912d0efbbc2f6d7539bd9443124ac647007f94 Mon Sep 17 00:00:00 2001 From: clodanSPT Date: Tue, 19 Aug 2025 17:49:28 +0100 Subject: [PATCH] Fixed timer for non-UTC zones (#560) * Fixed timer for non-UTC zones * Added UT and removed unused method --------- Co-authored-by: Alex Co-authored-by: Chomp <27521899+chompDev@users.noreply.github.com> --- .../Helpers/WeatherHelper.cs | 2 +- .../SPTarkov.Server.Core/Utils/TimeUtil.cs | 9 +++-- .../Tests/Helpers/WeatherHelperTests.cs | 33 +++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 Testing/UnitTests/Tests/Helpers/WeatherHelperTests.cs diff --git a/Libraries/SPTarkov.Server.Core/Helpers/WeatherHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/WeatherHelper.cs index 4c75c7f2..6034694f 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/WeatherHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/WeatherHelper.cs @@ -34,7 +34,7 @@ public class WeatherHelper(ISptLogger logger, TimeUtil timeUtil, var twentyFourHoursSeconds = timeUtil.GetHoursAsSeconds(24); var currentTimestampSeconds = timestamp; - var tarkovTime = timeUtil.GetDateTimeFromTimeStamp( + var tarkovTime = timeUtil.GetUtcDateTimeFromTimeStamp( (long)(russiaOffsetSeconds + currentTimestampSeconds * _weatherConfig.Acceleration) % twentyFourHoursSeconds ); diff --git a/Libraries/SPTarkov.Server.Core/Utils/TimeUtil.cs b/Libraries/SPTarkov.Server.Core/Utils/TimeUtil.cs index 67d52428..2958293a 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/TimeUtil.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/TimeUtil.cs @@ -152,9 +152,14 @@ public class TimeUtil return DateTimeOffset.FromUnixTimeSeconds(timeStamp).DateTime; } - public int GetSecondsAsMilliseconds(int seconds) + /// + /// Takes a unix timestamp and converts to its UTC date + /// + /// + /// + public DateTime GetUtcDateTimeFromTimeStamp(long timeStamp) { - return seconds * 60 * 1000; + return DateTimeOffset.FromUnixTimeSeconds(timeStamp).UtcDateTime; } /// diff --git a/Testing/UnitTests/Tests/Helpers/WeatherHelperTests.cs b/Testing/UnitTests/Tests/Helpers/WeatherHelperTests.cs new file mode 100644 index 00000000..10a722ae --- /dev/null +++ b/Testing/UnitTests/Tests/Helpers/WeatherHelperTests.cs @@ -0,0 +1,33 @@ +using NUnit.Framework; +using SPTarkov.Server.Core.Helpers; + +namespace UnitTests.Tests.Helpers; + +[TestFixture] +public class WeatherHelperTests +{ + private WeatherHelper _weatherHelper; + + [OneTimeSetUp] + public void Initialize() + { + _weatherHelper = DI.GetInstance().GetService(); + } + + [TestCase(1755621231, 22, 56, 57)] + [TestCase(1754120368, 8, 36, 16)] + [TestCase(1714120368, 14, 49, 36)] + [TestCase(1724120368, 19, 16, 16)] + public void GetInRaidTime_WithDifferentTimestamps_ExpectCorrectEFTTime( + long timestamp, + int expectedHour, + int expectedMinute, + int expectedSecond) + { + var timeOutput = _weatherHelper.GetInRaidTime(timestamp); + + Assert.AreEqual(expectedHour, timeOutput.Hour, $"Unexpected hour! {timeOutput.Hour}"); + Assert.AreEqual(expectedMinute, timeOutput.Minute, $"Unexpected minute! {timeOutput.Minute}"); + Assert.AreEqual(expectedSecond, timeOutput.Second, $"Unexpected second! {timeOutput.Second}"); + } +}