10000 Remove tzinfo check in datetime conversion by jhonabreul · Pull Request #80 · QuantConnect/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Remove tzinfo check in datetime conversion #80

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

Merged
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
49 changes: 48 additions & 1 deletion src/embed_tests/TestConverter.cs
10000
Original file line number Diff line number Diff line change
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
4 changes: 2 additions & 2 deletions src/perf_tests/Python.PerformanceTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.*" />
<PackageReference Include="quantconnect.pythonnet" Version="2.0.26" GeneratePathProperty="true">
<PackageReference Include="quantconnect.pythonnet" Version="2.0.27" GeneratePathProperty="true">
<IncludeAssets>compile</IncludeAssets>
</PackageReference>
</ItemGroup>
Expand All @@ -25,7 +25,7 @@
</Target>

<Target Name="CopyBaseline" AfterTargets="Build">
<Copy SourceFiles="$(NuGetPackageRoot)quantconnect.pythonnet\2.0.26\lib\net5.0\Python.Runtime.dll" DestinationFolder="$(OutDir)baseline" />
<Copy SourceFiles="$(NuGetPackageRoot)quantconnect.pythonnet\2.0.27\lib\net5.0\Python.Runtime.dll" DestinationFolder="$(OutDir)baseline" />
</Target>

<Target Name="CopyNewBuild" AfterTargets="Build">
Expand Down
28 changes: 1 addition & 27 deletions src/runtime/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ static Converter()
timeSpanCtor = Runtime.PyObject_GetAttrString(dateTimeMod.Borrow(), "timedelta").MoveToPyObject();
PythonException.ThrowIfIsNull(timeSpanCtor);


tzInfoCtor = new Lazy<PyObject>(() =>
{
var tzInfoMod = PyModule.FromString("custom_tzinfo", @"
Expand Down Expand Up @@ -1124,20 +1123,6 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec
var minute = Runtime.PyObject_GetAttrString(value, minutePtr);
var second = Runtime.PyObject_GetAttrString(value, secondPtr);
var microsecond = Runtime.PyObject_GetAttrString(value, microsecondPtr);
var timeKind = DateTimeKind.Unspecified;
var tzinfo = Runtime.PyObject_GetAttrString(value, tzinfoPtr);

NewReference hours = default;
NewReference minutes = default;
if (!tzinfo.IsNone() && !tzinfo.IsNull())
{
hours = Runtime.PyObject_GetAttrString(tzinfo.Borrow(), hoursPtr);
minutes = Runtime.PyObject_GetAttrString(tzinfo.Borrow(), minutesPtr);
if (Runtime.PyLong_AsLong(hours.Borrow()) == 0 && Runtime.PyLong_AsLong(minutes.Borrow()) == 0)
{
timeKind = DateTimeKind.Utc;
}
}

var convertedHour = 0L;
var convertedMinute = 0L;
Expand All @@ -1158,8 +1143,7 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec
(int)convertedHour,
(int)convertedMinute,
(int)convertedSecond,
millisecond: (int)milliseconds,
timeKind);
(int)milliseconds);

year.Dispose();
month.Dispose();
Expand All @@ -1169,16 +1153,6 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec
second.Dispose();
microsecond.Dispose();

if (!tzinfo.IsNull())
{
tzinfo.Dispose();
if (!tzinfo.IsNone())
{
hours.Dispose();
minutes.Dispose();
}
}

Exceptions.Clear();
return true;
default:
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
[assembly: InternalsVisibleTo("Python.EmbeddingTest, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")]
[assembly: InternalsVisibleTo("Python.Test, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")]

[assembly: AssemblyVersion("2.0.26")]
[assembly: AssemblyFileVersion("2.0.26")]
[assembly: AssemblyVersion("2.0.27")]
[assembly: AssemblyFileVersion("2.0.27")]
2 changes: 1 addition & 1 deletion src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<RootNamespace>Python.Runtime</RootNamespace>
<AssemblyName>Python.Runtime</AssemblyName>
<PackageId>QuantConnect.pythonnet</PackageId>
<Version>2.0.26</Version>
<Version>2.0.27</Version>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryUrl>https://github.com/pythonnet/pythonnet</RepositoryUrl>
Expand Down
0