Remove tzinfo check in datetime conversion by jhonabreul · Pull Request #80 · QuantConnect/pythonnet
Expand Up
@@ -187,6 +187,53 @@ public void ConvertDateTimeRoundTrip(DateTimeKind kind)
Assert.AreEqual(datetime, result);
}
[TestCase("")] [TestCase("America/New_York")] [TestCase("UTC")] public void ConvertDateTimeWithTimeZonePythonToCSharp(string timeZone) { const int year = 2024; const int month = 2; const int day = 27; const int hour = 12; const int minute = 30; const int second = 45;
using (Py.GIL()) { dynamic module = PyModule.FromString("module", @$" from clr import AddReference AddReference(""Python.EmbeddingTest"") AddReference(""System"")
from Python.EmbeddingTest import *
from datetime import datetime from pytz import timezone
tzinfo = timezone('{timeZone}') if '{timeZone}' else None
def GetPyDateTime(): return datetime({year}, {month}, {day}, {hour}, {minute}, {second}, tzinfo=tzinfo) \ if tzinfo else \ datetime({year}, {month}, {day}, {hour}, {minute}, {second})
def GetNextDay(dateTime): return TestConverter.GetNextDay(dateTime) ");
var pyDateTime = module.GetPyDateTime(); var dateTimeResult = default(object);
Assert.DoesNotThrow(() => Converter.ToManaged(pyDateTime, typeof(DateTime), out dateTimeResult, false));
var managedDateTime = (DateTime)dateTimeResult;
var expectedDateTime = new DateTime(year, month, day, hour, minute, second); Assert.AreEqual(expectedDateTime, managedDateTime); } }
[Test] public void ConvertTimestampRoundTrip() { Expand Down Expand Up @@ -362,7 +409,7 @@ class PyGetListImpl(test.GetListImpl): List<string> result = inst.GetList(); CollectionAssert.AreEqual(new[] { "testing" }, result); }
[Test] public void PrimitiveIntConversion() { Expand Down
[TestCase("")] [TestCase("America/New_York")] [TestCase("UTC")] public void ConvertDateTimeWithTimeZonePythonToCSharp(string timeZone) { const int year = 2024; const int month = 2; const int day = 27; const int hour = 12; const int minute = 30; const int second = 45;
using (Py.GIL()) { dynamic module = PyModule.FromString("module", @$" from clr import AddReference AddReference(""Python.EmbeddingTest"") AddReference(""System"")
from Python.EmbeddingTest import *
from datetime import datetime from pytz import timezone
tzinfo = timezone('{timeZone}') if '{timeZone}' else None
def GetPyDateTime(): return datetime({year}, {month}, {day}, {hour}, {minute}, {second}, tzinfo=tzinfo) \ if tzinfo else \ datetime({year}, {month}, {day}, {hour}, {minute}, {second})
def GetNextDay(dateTime): return TestConverter.GetNextDay(dateTime) ");
var pyDateTime = module.GetPyDateTime(); var dateTimeResult = default(object);
Assert.DoesNotThrow(() => Converter.ToManaged(pyDateTime, typeof(DateTime), out dateTimeResult, false));
var managedDateTime = (DateTime)dateTimeResult;
var expectedDateTime = new DateTime(year, month, day, hour, minute, second); Assert.AreEqual(expectedDateTime, managedDateTime); } }
[Test] public void ConvertTimestampRoundTrip() { Expand Down Expand Up @@ -362,7 +409,7 @@ class PyGetListImpl(test.GetListImpl): List<string> result = inst.GetList(); CollectionAssert.AreEqual(new[] { "testing" }, result); }
[Test] public void PrimitiveIntConversion() { Expand Down