Skip to content

Commit c36b843

Browse files
authored
Merge pull request #80 from jhonabreul/bug-7760-datetime-with-tzinfo-conversion
Remove tzinfo check in datetime conversion
2 parents 2df3c27 + 3b0f31e commit c36b843

File tree

5 files changed

+54
-33
lines changed

5 files changed

+54
-33
lines changed

src/embed_tests/TestConverter.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,53 @@ public void ConvertDateTimeRoundTrip(DateTimeKind kind)
187187
Assert.AreEqual(datetime, result);
188188
}
189189

190+
[TestCase("")]
191+
[TestCase("America/New_York")]
192+
[TestCase("UTC")]
193+
public void ConvertDateTimeWithTimeZonePythonToCSharp(string timeZone)
194+
{
195+
const int year = 2024;
196+
const int month = 2;
197+
const int day = 27;
198+
const int hour = 12;
199+
const int minute = 30;
200+
const int second = 45;
201+
202+
using (Py.GIL())
203+
{
204+
dynamic module = PyModule.FromString("module", @$"
205+
from clr import AddReference
206+
AddReference(""Python.EmbeddingTest"")
207+
AddReference(""System"")
208+
209+
from Python.EmbeddingTest import *
210+
211+
from datetime import datetime
212+
from pytz import timezone
213+
214+
tzinfo = timezone('{timeZone}') if '{timeZone}' else None
215+
216+
def GetPyDateTime():
217+
return datetime({year}, {month}, {day}, {hour}, {minute}, {second}, tzinfo=tzinfo) \
218+
if tzinfo else \
219+
datetime({year}, {month}, {day}, {hour}, {minute}, {second})
220+
221+
def GetNextDay(dateTime):
222+
return TestConverter.GetNextDay(dateTime)
223+
");
224+
225+
var pyDateTime = module.GetPyDateTime();
226+
var dateTimeResult = default(object);
227+
228+
Assert.DoesNotThrow(() => Converter.ToManaged(pyDateTime, typeof(DateTime), out dateTimeResult, false));
229+
230+
var managedDateTime = (DateTime)dateTimeResult;
231+
232+
var expectedDateTime = new DateTime(year, month, day, hour, minute, second);
233+
Assert.AreEqual(expectedDateTime, managedDateTime);
234+
}
235+
}
236+
190237
[Test]
191238
public void ConvertTimestampRoundTrip()
192239
{
@@ -362,7 +409,7 @@ class PyGetListImpl(test.GetListImpl):
362409
List<string> result = inst.GetList();
363410
CollectionAssert.AreEqual(new[] { "testing" }, result);
364411
}
365-
412+
366413
[Test]
367414
public void PrimitiveIntConversion()
368415
{

src/perf_tests/Python.PerformanceTests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
</PackageReference>
1515
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.*" />
16-
<PackageReference Include="quantconnect.pythonnet" Version="2.0.26" GeneratePathProperty="true">
16+
<PackageReference Include="quantconnect.pythonnet" Version="2.0.27" GeneratePathProperty="true">
1717
<IncludeAssets>compile</IncludeAssets>
1818
</PackageReference>
1919
</ItemGroup>
@@ -25,7 +25,7 @@
2525
</Target>
2626

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

3131
<Target Name="CopyNewBuild" AfterTargets="Build">

src/runtime/Converter.cs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ static Converter()
7676
timeSpanCtor = Runtime.PyObject_GetAttrString(dateTimeMod.Borrow(), "timedelta").MoveToPyObject();
7777
PythonException.ThrowIfIsNull(timeSpanCtor);
7878

79-
8079
tzInfoCtor = new Lazy<PyObject>(() =>
8180
{
8281
var tzInfoMod = PyModule.FromString("custom_tzinfo", @"
@@ -1124,20 +1123,6 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec
11241123
var minute = Runtime.PyObject_GetAttrString(value, minutePtr);
11251124
var second = Runtime.PyObject_GetAttrString(value, secondPtr);
11261125
var microsecond = Runtime.PyObject_GetAttrString(value, microsecondPtr);
1127-
var timeKind = DateTimeKind.Unspecified;
1128-
var tzinfo = Runtime.PyObject_GetAttrString(value, tzinfoPtr);
1129-
1130-
NewReference hours = default;
1131-
NewReference minutes = default;
1132-
if (!tzinfo.IsNone() && !tzinfo.IsNull())
1133-
{
1134-
hours = Runtime.PyObject_GetAttrString(tzinfo.Borrow(), hoursPtr);
1135-
minutes = Runtime.PyObject_GetAttrString(tzinfo.Borrow(), minutesPtr);
1136-
if (Runtime.PyLong_AsLong(hours.Borrow()) == 0 && Runtime.PyLong_AsLong(minutes.Borrow()) == 0)
1137-
{
1138-
timeKind = DateTimeKind.Utc;
1139-
}
1140-
}
11411126

11421127
var convertedHour = 0L;
11431128
var convertedMinute = 0L;
@@ -1158,8 +1143,7 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec
11581143
(int)convertedHour,
11591144
(int)convertedMinute,
11601145
(int)convertedSecond,
1161-
millisecond: (int)milliseconds,
1162-
timeKind);
1146+
(int)milliseconds);
11631147

11641148
year.Dispose();
11651149
month.Dispose();
@@ -1169,16 +1153,6 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec
11691153
second.Dispose();
11701154
microsecond.Dispose();
11711155

1172-
if (!tzinfo.IsNull())
1173-
{
1174-
tzinfo.Dispose();
1175-
if (!tzinfo.IsNone())
1176-
{
1177-
hours.Dispose();
1178-
minutes.Dispose();
1179-
}
1180-
}
1181-
11821156
Exceptions.Clear();
11831157
return true;
11841158
default:

src/runtime/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
[assembly: InternalsVisibleTo("Python.EmbeddingTest, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")]
55
[assembly: InternalsVisibleTo("Python.Test, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")]
66

7-
[assembly: AssemblyVersion("2.0.26")]
8-
[assembly: AssemblyFileVersion("2.0.26")]
7+
[assembly: AssemblyVersion("2.0.27")]
8+
[assembly: AssemblyFileVersion("2.0.27")]

src/runtime/Python.Runtime.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<RootNamespace>Python.Runtime</RootNamespace>
66
<AssemblyName>Python.Runtime</AssemblyName>
77
<PackageId>QuantConnect.pythonnet</PackageId>
8-
<Version>2.0.26</Version>
8+
<Version>2.0.27</Version>
99
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
1010
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1111
<RepositoryUrl>https://github.com/pythonnet/pythonnet</RepositoryUrl>

0 commit comments

Comments
 (0)