From 2ff8af3037778313a146565f1393f5cab973c9c3 Mon Sep 17 00:00:00 2001 From: Ethan Moffat Date: Tue, 9 May 2023 01:08:47 -0700 Subject: [PATCH] Fix timestamp generation on non-windows --- EOLib/GameStartTimeRepository.cs | 7 +------ EOLib/misc.cs | 7 ++++--- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/EOLib/GameStartTimeRepository.cs b/EOLib/GameStartTimeRepository.cs index 38c656c23..89120a571 100644 --- a/EOLib/GameStartTimeRepository.cs +++ b/EOLib/GameStartTimeRepository.cs @@ -1,13 +1,10 @@ using AutomaticTypeMapper; -using System; using System.Diagnostics; namespace EOLib { public interface IGameStartTimeProvider { - DateTime StartTime { get; } - Stopwatch Elapsed { get; } int TimeStamp { get; } @@ -16,10 +13,8 @@ public interface IGameStartTimeProvider [AutoMappedType(IsSingleton = true)] public class GameStartTimeRepository : IGameStartTimeProvider { - public DateTime StartTime { get; } = DateTime.UtcNow; - public Stopwatch Elapsed { get; } = Stopwatch.StartNew(); - public int TimeStamp => StartTime.ToEOTimeStamp(Elapsed.ElapsedTicks); + public int TimeStamp => Elapsed.ToEOTimeStamp(); } } diff --git a/EOLib/misc.cs b/EOLib/misc.cs index 919ce6f62..01aee3b41 100644 --- a/EOLib/misc.cs +++ b/EOLib/misc.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; namespace EOLib { @@ -20,10 +21,10 @@ public static T[] SubArray(this T[] arr, int offset, int count) public static class DateTimeExtension { - public static int ToEOTimeStamp(this DateTime dt, long elapsedTicks) + public static int ToEOTimeStamp(this Stopwatch sw) { - dt = dt.Add(TimeSpan.FromTicks(elapsedTicks)); - return dt.Hour * 360000 + dt.Minute * 6000 + dt.Second * 100 + dt.Millisecond / 10; + var elapsedHundreths = Math.Round(sw.ElapsedTicks / (Stopwatch.Frequency / 100.0)); + return (int)elapsedHundreths; } }