Skip to content

Commit

Permalink
Merge pull request #15 from QuantConnect/bug-deallocation-none-dateti…
Browse files Browse the repository at this point in the history
…me-timezone

Do not include timezone if DateTimeKind is Unspecified
  • Loading branch information
mchandschuh authored Aug 24, 2018
2 parents c6db866 + bec9563 commit e95794c
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,27 @@ internal static IntPtr ToPython(object value, Type type)
case TypeCode.DateTime:
var datetime = (DateTime)value;

IntPtr dateTimeArgs = Runtime.PyTuple_New(8);
var size = datetime.Kind == DateTimeKind.Unspecified ? 7 : 8;

IntPtr dateTimeArgs = Runtime.PyTuple_New(size);
Runtime.PyTuple_SetItem(dateTimeArgs, 0, Runtime.PyInt_FromInt32(datetime.Year));
Runtime.PyTuple_SetItem(dateTimeArgs, 1, Runtime.PyInt_FromInt32(datetime.Month));
Runtime.PyTuple_SetItem(dateTimeArgs, 2, Runtime.PyInt_FromInt32(datetime.Day));
Runtime.PyTuple_SetItem(dateTimeArgs, 3, Runtime.PyInt_FromInt32(datetime.Hour));
Runtime.PyTuple_SetItem(dateTimeArgs, 4, Runtime.PyInt_FromInt32(datetime.Minute));
Runtime.PyTuple_SetItem(dateTimeArgs, 5, Runtime.PyInt_FromInt32(datetime.Second));
Runtime.PyTuple_SetItem(dateTimeArgs, 6, Runtime.PyInt_FromInt32(datetime.Millisecond));
Runtime.PyTuple_SetItem(dateTimeArgs, 7, TzInfo(datetime.Kind));

// datetime.datetime 6th argument represents micro seconds
var totalSeconds = datetime.TimeOfDay.TotalSeconds;
var microSeconds = Convert.ToInt32((totalSeconds - Math.Truncate(totalSeconds)) * 1000000);
if (microSeconds == 1000000) microSeconds = 999999;
Runtime.PyTuple_SetItem(dateTimeArgs, 6, Runtime.PyInt_FromInt32(microSeconds));

if (size == 8)
{
Runtime.PyTuple_SetItem(dateTimeArgs, 7, TzInfo(datetime.Kind));
}

var returnDateTime = Runtime.PyObject_CallObject(dateTimeCtor, dateTimeArgs);
// clean up
Runtime.XDecref(dateTimeArgs);
Expand Down

0 comments on commit e95794c

Please sign in to comment.