Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TimeZone changeable #191

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/MoonSharp.Interpreter.Tests/EndToEnd/TimeZoneTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using NUnit.Framework;

namespace MoonSharp.Interpreter.Tests.EndToEnd
{
[TestFixture]
public class TimeZoneTests
{
[Test]
public void LocalTime1()
{
Script S = new Script();
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)");

Assert.AreEqual(DataType.String, res.Type);
Assert.AreEqual("1970-01-01 01:00:00", res.String);
}

[Test]
public void LocalTime2()
{
Script S = new Script();
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)");

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(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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
<Compile Include="EndToEnd\StringLibTests.cs" />
<Compile Include="EndToEnd\StructAssignmentTechnique.cs" />
<Compile Include="EndToEnd\TailCallTests.cs" />
<Compile Include="EndToEnd\TimeZoneTests.cs" />
<Compile Include="EndToEnd\UserDataEventsTests.cs" />
<Compile Include="EndToEnd\UserDataEnumsTest.cs" />
<Compile Include="EndToEnd\UserDataNestedTypesTests.cs" />
Expand Down
4 changes: 3 additions & 1 deletion src/MoonSharp.Interpreter/CoreLib/OsTimeModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic needs to be added to:

src/MoonSharp.Interpreter/_Projects/MoonSharp.Interpreter.netcore/src/CoreLib/OsTimeModule.cs
src/Unity/MoonSharp/Assets/Plugins/MoonSharp/Interpreter/CoreLib/OsTimeModule.cs

If adding this in to the current system; as we push to clean this up it will change, but for now this PR will be accepted if the additional locations are updated.


reference = TimeZoneInfo.ConvertTimeFromUtc(reference, localTimeZoneInfo);
isDst = reference.IsDaylightSavingTime();
}
catch (TimeZoneNotFoundException)
Expand Down
10 changes: 8 additions & 2 deletions src/MoonSharp.Interpreter/ScriptOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ internal ScriptOptions(ScriptOptions defaults)
this.ScriptLoader = defaults.ScriptLoader;

this.CheckThreadAccess = defaults.CheckThreadAccess;

this.LocalTimeZoneInfo = defaults.LocalTimeZoneInfo;
}

/// <summary>
Expand Down Expand Up @@ -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.
/// </summary>
public bool CheckThreadAccess { get; set; }

/// <summary>
/// Gets or sets the local time zone. If null, TimeZoneInfo.Local is used.
/// </summary>
public TimeZoneInfo LocalTimeZoneInfo { get; set; }
}
}