Fix for datetime tzinfo conversion by jhonabreul · Pull Request #81 · QuantConnect/pythonnet
Expand Up
@@ -1123,13 +1123,31 @@ 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 (!ReferenceNullOrNone(tzinfo)) { // We set the datetime kind to UTC if the tzinfo was set to UTC by the ToPthon method // using it's custom GMT Python tzinfo class hours = Runtime.PyObject_GetAttrString(tzinfo.Borrow(), hoursPtr); minutes = Runtime.PyObject_GetAttrString(tzinfo.Borrow(), minutesPtr); if (!ReferenceNullOrNone(hours) && !ReferenceNullOrNone(minutes) && Runtime.PyLong_AsLong(hours.Borrow()) == 0 && Runtime.PyLong_AsLong(minutes.Borrow()) == 0) { timeKind = DateTimeKind.Utc; } }
var convertedHour = 0L; var convertedMinute = 0L; var convertedSecond = 0L; var milliseconds = 0L; // could be python date type if (!hour.IsNull() && !hour.IsNone()) if (!ReferenceNullOrNone(hour)) { convertedHour = Runtime.PyLong_AsLong(hour.Borrow()); convertedMinute = Runtime.PyLong_AsLong(minute.Borrow()); Expand All @@ -1143,7 +1161,8 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec (int)convertedHour, (int)convertedMinute, (int)convertedSecond, (int)milliseconds); (int)milliseconds, timeKind);
year.Dispose(); month.Dispose(); Expand All @@ -1153,6 +1172,16 @@ 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 Expand Up @@ -1183,6 +1212,11 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec return false; }
private static bool ReferenceNullOrNone(NewReference reference) { return reference.IsNull() || reference.IsNone(); }
private static void SetConversionError(BorrowedReference value, Type target) { Expand Down
NewReference hours = default; NewReference minutes = default; if (!ReferenceNullOrNone(tzinfo)) { // We set the datetime kind to UTC if the tzinfo was set to UTC by the ToPthon method // using it's custom GMT Python tzinfo class hours = Runtime.PyObject_GetAttrString(tzinfo.Borrow(), hoursPtr); minutes = Runtime.PyObject_GetAttrString(tzinfo.Borrow(), minutesPtr); if (!ReferenceNullOrNone(hours) && !ReferenceNullOrNone(minutes) && Runtime.PyLong_AsLong(hours.Borrow()) == 0 && Runtime.PyLong_AsLong(minutes.Borrow()) == 0) { timeKind = DateTimeKind.Utc; } }
var convertedHour = 0L; var convertedMinute = 0L; var convertedSecond = 0L; var milliseconds = 0L; // could be python date type if (!hour.IsNull() && !hour.IsNone()) if (!ReferenceNullOrNone(hour)) { convertedHour = Runtime.PyLong_AsLong(hour.Borrow()); convertedMinute = Runtime.PyLong_AsLong(minute.Borrow()); Expand All @@ -1143,7 +1161,8 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec (int)convertedHour, (int)convertedMinute, (int)convertedSecond, (int)milliseconds); (int)milliseconds, timeKind);
year.Dispose(); month.Dispose(); Expand All @@ -1153,6 +1172,16 @@ 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 Expand Up @@ -1183,6 +1212,11 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec return false; }
private static bool ReferenceNullOrNone(NewReference reference) { return reference.IsNull() || reference.IsNone(); }
private static void SetConversionError(BorrowedReference value, Type target) { Expand Down