From 56741bfabf05ecab53e1f421cdc377e463594665 Mon Sep 17 00:00:00 2001 From: Masayuki Muto Date: Thu, 23 Mar 2017 18:28:57 +0900 Subject: [PATCH 1/4] Introduce ScriptOptions.LocalTimeZoneInfo --- src/MoonSharp.Interpreter/CoreLib/OsTimeModule.cs | 4 +++- src/MoonSharp.Interpreter/ScriptOptions.cs | 10 ++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/MoonSharp.Interpreter/CoreLib/OsTimeModule.cs b/src/MoonSharp.Interpreter/CoreLib/OsTimeModule.cs index 762632fa..36ac41b4 100755 --- a/src/MoonSharp.Interpreter/CoreLib/OsTimeModule.cs +++ b/src/MoonSharp.Interpreter/CoreLib/OsTimeModule.cs @@ -123,7 +123,9 @@ public static DynValue date(ScriptExecutionContext executionContext, CallbackArg try { - reference = TimeZoneInfo.ConvertTimeFromUtc(reference, TimeZoneInfo.Local); + TimeZoneInfo localTimeZoneInfo = executionContext.OwnerScript.Options.LocalTimeZoneInfo ?? TimeZoneInfo.Local; + + reference = TimeZoneInfo.ConvertTimeFromUtc(reference, localTimeZoneInfo); isDst = reference.IsDaylightSavingTime(); } catch (TimeZoneNotFoundException) diff --git a/src/MoonSharp.Interpreter/ScriptOptions.cs b/src/MoonSharp.Interpreter/ScriptOptions.cs index f74131a9..41d23dd9 100644 --- a/src/MoonSharp.Interpreter/ScriptOptions.cs +++ b/src/MoonSharp.Interpreter/ScriptOptions.cs @@ -27,6 +27,8 @@ internal ScriptOptions(ScriptOptions defaults) this.ScriptLoader = defaults.ScriptLoader; this.CheckThreadAccess = defaults.CheckThreadAccess; + + this.LocalTimeZoneInfo = defaults.LocalTimeZoneInfo; } /// @@ -88,13 +90,17 @@ internal ScriptOptions(ScriptOptions defaults) /// Gets or sets a value indicating whether the thread check is enabled. /// A "lazy" thread check is performed everytime execution is entered to ensure that no two threads /// calls MoonSharp execution concurrently. However 1) the check is performed best effort (thus, it might - /// not detect all issues) and 2) it might trigger in very odd legal situations (like, switching threads + /// not detect all issues) and 2) it might trigger in very odd legal situations (like, switching threads /// inside a CLR-callback without actually having concurrency. - /// + /// /// Disable this option if the thread check is giving problems in your scenario, but please check that /// you are not calling MoonSharp execution concurrently as it is not supported. /// public bool CheckThreadAccess { get; set; } + /// + /// Gets or sets the local time zone. If null, TimeZoneInfo.Local is used. + /// + public TimeZoneInfo LocalTimeZoneInfo { get; set; } } } From d8fe1d1ec7a12c14366dc98747ec7ba4e3ab4900 Mon Sep 17 00:00:00 2001 From: Masayuki Muto Date: Fri, 24 Mar 2017 16:06:16 +0900 Subject: [PATCH 2/4] Add tests --- .../EndToEnd/TimeZoneTests.cs | 46 +++++++++++++++++++ ...harp.Interpreter.Tests.net35-client.csproj | 1 + 2 files changed, 47 insertions(+) create mode 100644 src/MoonSharp.Interpreter.Tests/EndToEnd/TimeZoneTests.cs diff --git a/src/MoonSharp.Interpreter.Tests/EndToEnd/TimeZoneTests.cs b/src/MoonSharp.Interpreter.Tests/EndToEnd/TimeZoneTests.cs new file mode 100644 index 00000000..fdff855e --- /dev/null +++ b/src/MoonSharp.Interpreter.Tests/EndToEnd/TimeZoneTests.cs @@ -0,0 +1,46 @@ +using System; +using NUnit.Framework; + +namespace MoonSharp.Interpreter.Tests.EndToEnd +{ + [TestFixture] + public class TimeZoneTests + { + [Test] + public void LocalTime1() + { + Script S = new Script(); + S.Options.LocalTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time"); + + DynValue res = S.DoString("return os.date(\"%Y-%m-%d %H:%M:%S\", 0)"); + + Assert.AreEqual(DataType.String, res.Type); + Assert.AreEqual("1970-01-01 01:00:00", res.String); + } + + [Test] + public void LocalTime2() + { + Script S = new Script(); + S.Options.LocalTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time"); + + DynValue res = S.DoString("return os.date(\"!%Y-%m-%d %H:%M:%S\", 0)"); + + Assert.AreEqual(DataType.String, res.Type); + Assert.AreEqual("1970-01-01 00:00:00", res.String); + } + + [Test] + public void LocalTime3() + { + Script S = new Script(); + + TimeSpan offset = TimeZoneInfo.Local.GetUtcOffset(new DateTime(1970, 1, 1)); + + DynValue res = S.DoString($"return os.date(\"%Y-%m-%d %H:%M:%S\", -{offset.TotalSeconds})"); + + Assert.AreEqual(DataType.String, res.Type); + Assert.AreEqual("1970-01-01 00:00:00", res.String); + } + } +} diff --git a/src/MoonSharp.Interpreter.Tests/MoonSharp.Interpreter.Tests.net35-client.csproj b/src/MoonSharp.Interpreter.Tests/MoonSharp.Interpreter.Tests.net35-client.csproj index 3af2831c..86a1a337 100644 --- a/src/MoonSharp.Interpreter.Tests/MoonSharp.Interpreter.Tests.net35-client.csproj +++ b/src/MoonSharp.Interpreter.Tests/MoonSharp.Interpreter.Tests.net35-client.csproj @@ -135,6 +135,7 @@ + From 9cc3f0b8b21004493dc0cfc95927a6ae4b900da0 Mon Sep 17 00:00:00 2001 From: Masayuki Muto Date: Fri, 21 Apr 2017 17:46:26 +0900 Subject: [PATCH 3/4] Use string.Format() instead of string intepolation --- src/MoonSharp.Interpreter.Tests/EndToEnd/TimeZoneTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MoonSharp.Interpreter.Tests/EndToEnd/TimeZoneTests.cs b/src/MoonSharp.Interpreter.Tests/EndToEnd/TimeZoneTests.cs index fdff855e..ea7be815 100644 --- a/src/MoonSharp.Interpreter.Tests/EndToEnd/TimeZoneTests.cs +++ b/src/MoonSharp.Interpreter.Tests/EndToEnd/TimeZoneTests.cs @@ -37,7 +37,7 @@ public void LocalTime3() TimeSpan offset = TimeZoneInfo.Local.GetUtcOffset(new DateTime(1970, 1, 1)); - DynValue res = S.DoString($"return os.date(\"%Y-%m-%d %H:%M:%S\", -{offset.TotalSeconds})"); + DynValue res = S.DoString(string.Format("return os.date(\"%Y-%m-%d %H:%M:%S\", -{0})", offset.TotalSeconds)); Assert.AreEqual(DataType.String, res.Type); Assert.AreEqual("1970-01-01 00:00:00", res.String); From 84af8cc6e59b61215ab1a7765055b709cecdfddb Mon Sep 17 00:00:00 2001 From: Masayuki Muto Date: Fri, 21 Apr 2017 18:35:59 +0900 Subject: [PATCH 4/4] Add fallback of timezone format --- .../EndToEnd/TimeZoneTests.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/MoonSharp.Interpreter.Tests/EndToEnd/TimeZoneTests.cs b/src/MoonSharp.Interpreter.Tests/EndToEnd/TimeZoneTests.cs index ea7be815..87c65569 100644 --- a/src/MoonSharp.Interpreter.Tests/EndToEnd/TimeZoneTests.cs +++ b/src/MoonSharp.Interpreter.Tests/EndToEnd/TimeZoneTests.cs @@ -10,7 +10,14 @@ public class TimeZoneTests public void LocalTime1() { Script S = new Script(); - S.Options.LocalTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time"); + try + { + S.Options.LocalTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time"); + } + catch (TimeZoneNotFoundException) + { + S.Options.LocalTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin"); + } DynValue res = S.DoString("return os.date(\"%Y-%m-%d %H:%M:%S\", 0)"); @@ -22,7 +29,14 @@ public void LocalTime1() public void LocalTime2() { Script S = new Script(); - S.Options.LocalTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time"); + try + { + S.Options.LocalTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time"); + } + catch (TimeZoneNotFoundException) + { + S.Options.LocalTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin"); + } DynValue res = S.DoString("return os.date(\"!%Y-%m-%d %H:%M:%S\", 0)");