Browse Source

change : move time abstractions to TimeProvider

Akeit0 6 months ago
parent
commit
e7d70904fd

+ 3 - 0
src/Lua/LuaState.cs

@@ -44,6 +44,9 @@ public sealed class LuaState
     public ILuaFileSystem FileSystem => Platform.FileSystem ?? throw new InvalidOperationException("FileSystem is not set. Please set it before access.");
     public ILuaFileSystem FileSystem => Platform.FileSystem ?? throw new InvalidOperationException("FileSystem is not set. Please set it before access.");
 
 
     public ILuaOsEnvironment OsEnvironment => Platform.OsEnvironment ?? throw new InvalidOperationException("OperatingSystem is not set. Please set it before access.");
     public ILuaOsEnvironment OsEnvironment => Platform.OsEnvironment ?? throw new InvalidOperationException("OperatingSystem is not set. Please set it before access.");
+    
+    
+    public TimeProvider TimeProvider => Platform.TimeProvider ?? throw new InvalidOperationException("TimeProvider is not set. Please set it before access.");
 
 
     public ILuaStandardIO StandardIO => Platform.StandardIO;
     public ILuaStandardIO StandardIO => Platform.StandardIO;
 
 

+ 0 - 10
src/Lua/Platforms/ILuaOsEnvironment.cs

@@ -19,14 +19,4 @@ public interface ILuaOsEnvironment
     /// Get current process start time for clock calculations (units: seconds)
     /// Get current process start time for clock calculations (units: seconds)
     /// </summary>
     /// </summary>
     double GetTotalProcessorTime();
     double GetTotalProcessorTime();
-
-    /// <summary>
-    /// Get current UTC time
-    /// </summary>
-    DateTime GetCurrentUtcTime();
-
-    /// <summary>
-    /// Get local time zone offset from UTC
-    /// </summary>
-    TimeSpan GetLocalTimeZoneOffset();
 }
 }

+ 4 - 2
src/Lua/Platforms/LuaPlatform.cs

@@ -9,7 +9,7 @@ namespace Lua.Platforms;
 /// <param name="fileSystem"></param>
 /// <param name="fileSystem"></param>
 /// <param name="osEnvironment"></param>
 /// <param name="osEnvironment"></param>
 /// <param name="standardIO"></param>
 /// <param name="standardIO"></param>
-public sealed class LuaPlatform(ILuaFileSystem fileSystem, ILuaOsEnvironment osEnvironment, ILuaStandardIO standardIO)
+public sealed class LuaPlatform(ILuaFileSystem fileSystem, ILuaOsEnvironment osEnvironment, ILuaStandardIO standardIO, TimeProvider timeProvider)
 {
 {
     /// <summary>
     /// <summary>
     /// Standard console platform implementation.
     /// Standard console platform implementation.
@@ -18,9 +18,11 @@ public sealed class LuaPlatform(ILuaFileSystem fileSystem, ILuaOsEnvironment osE
     public static LuaPlatform Default => new(
     public static LuaPlatform Default => new(
         fileSystem: new FileSystem(),
         fileSystem: new FileSystem(),
         osEnvironment: new SystemOsEnvironment(),
         osEnvironment: new SystemOsEnvironment(),
-        standardIO: new ConsoleStandardIO());
+        standardIO: new ConsoleStandardIO()
+        , timeProvider: TimeProvider.System);
 
 
     public ILuaFileSystem FileSystem { get; set; } = fileSystem;
     public ILuaFileSystem FileSystem { get; set; } = fileSystem;
     public ILuaOsEnvironment OsEnvironment { get; set; } = osEnvironment;
     public ILuaOsEnvironment OsEnvironment { get; set; } = osEnvironment;
     public ILuaStandardIO StandardIO { get; set; } = standardIO;
     public ILuaStandardIO StandardIO { get; set; } = standardIO;
+    public TimeProvider TimeProvider { get; set; } = timeProvider;
 }
 }

+ 0 - 10
src/Lua/Platforms/SystemOsEnvironment.cs

@@ -22,15 +22,5 @@ namespace Lua.Platforms
         {
         {
             return Process.GetCurrentProcess().TotalProcessorTime.TotalSeconds;
             return Process.GetCurrentProcess().TotalProcessorTime.TotalSeconds;
         }
         }
-
-        public DateTime GetCurrentUtcTime()
-        {
-            return DateTime.UtcNow;
-        }
-
-        public TimeSpan GetLocalTimeZoneOffset()
-        {
-            return TimeZoneInfo.Local.BaseUtcOffset;
-        }
     }
     }
 }
 }

+ 3 - 4
src/Lua/Standard/OperatingSystemLibrary.cs

@@ -45,7 +45,7 @@ public sealed class OperatingSystemLibrary
         }
         }
         else
         else
         {
         {
-            now = context.State.OsEnvironment.GetCurrentUtcTime();
+            now = context.State.TimeProvider.GetUtcNow().DateTime;
         }
         }
 
 
         var isDst = false;
         var isDst = false;
@@ -55,8 +55,7 @@ public sealed class OperatingSystemLibrary
         }
         }
         else
         else
         {
         {
-            var offset = context.State.OsEnvironment.GetLocalTimeZoneOffset();
-            now += offset;
+            now = context.State.TimeProvider.GetLocalNow().DateTime;
             isDst = now.IsDaylightSavingTime();
             isDst = now.IsDaylightSavingTime();
         }
         }
 
 
@@ -182,7 +181,7 @@ public sealed class OperatingSystemLibrary
         }
         }
         else
         else
         {
         {
-            return new(context.Return(DateTimeHelper.GetUnixTime(context.State.OsEnvironment.GetCurrentUtcTime())));
+            return new(context.Return(DateTimeHelper.GetUnixTime(context.State.TimeProvider.GetUtcNow().DateTime)));
         }
         }
     }
     }
 
 

+ 7 - 4
tests/Lua.Tests/AbstractFileTests.cs

@@ -9,7 +9,7 @@ public class AbstractFileTests
 {
 {
     class ReadOnlyFileSystem(Dictionary<string, string> dictionary) : NotImplementedExceptionFileSystemBase
     class ReadOnlyFileSystem(Dictionary<string, string> dictionary) : NotImplementedExceptionFileSystemBase
     {
     {
-        public override ValueTask<ILuaStream> Open(string path, LuaFileOpenMode mode,CancellationToken cancellationToken)
+        public override ValueTask<ILuaStream> Open(string path, LuaFileOpenMode mode, CancellationToken cancellationToken)
         {
         {
             if (!dictionary.TryGetValue(path, out var value))
             if (!dictionary.TryGetValue(path, out var value))
             {
             {
@@ -18,7 +18,7 @@ public class AbstractFileTests
 
 
             if (mode != LuaFileOpenMode.Read)
             if (mode != LuaFileOpenMode.Read)
                 throw new IOException($"File {path} not opened in read mode");
                 throw new IOException($"File {path} not opened in read mode");
-            return new (ILuaStream.CreateFromMemory(value.AsMemory()));
+            return new(ILuaStream.CreateFromMemory(value.AsMemory()));
         }
         }
     }
     }
 
 
@@ -30,7 +30,9 @@ public class AbstractFileTests
         var state = LuaState.Create(new(
         var state = LuaState.Create(new(
             fileSystem: fileSystem,
             fileSystem: fileSystem,
             osEnvironment: null!,
             osEnvironment: null!,
-            standardIO: new ConsoleStandardIO()));
+            standardIO: new ConsoleStandardIO(),
+            timeProvider: TimeProvider.System
+        ));
         state.OpenStandardLibraries();
         state.OpenStandardLibraries();
         try
         try
         {
         {
@@ -62,7 +64,8 @@ public class AbstractFileTests
         var state = LuaState.Create(new(
         var state = LuaState.Create(new(
             fileSystem: fileSystem,
             fileSystem: fileSystem,
             osEnvironment: null!,
             osEnvironment: null!,
-            standardIO: new ConsoleStandardIO()));
+            standardIO: new ConsoleStandardIO(),
+            timeProvider: TimeProvider.System));
         state.OpenStandardLibraries();
         state.OpenStandardLibraries();
 
 
         await state.DoStringAsync(
         await state.DoStringAsync(