@@ -323,15 +323,27 @@ internal static IntPtr ToPython(object value, Type type)
323323 case TypeCode . DateTime :
324324 var datetime = ( DateTime ) value ;
325325
326- IntPtr dateTimeArgs = Runtime . PyTuple_New ( 8 ) ;
326+ var size = datetime . Kind == DateTimeKind . Unspecified ? 7 : 8 ;
327+
328+ IntPtr dateTimeArgs = Runtime . PyTuple_New ( size ) ;
327329 Runtime . PyTuple_SetItem ( dateTimeArgs , 0 , Runtime . PyInt_FromInt32 ( datetime . Year ) ) ;
328330 Runtime . PyTuple_SetItem ( dateTimeArgs , 1 , Runtime . PyInt_FromInt32 ( datetime . Month ) ) ;
329331 Runtime . PyTuple_SetItem ( dateTimeArgs , 2 , Runtime . PyInt_FromInt32 ( datetime . Day ) ) ;
330332 Runtime . PyTuple_SetItem ( dateTimeArgs , 3 , Runtime . PyInt_FromInt32 ( datetime . Hour ) ) ;
331333 Runtime . PyTuple_SetItem ( dateTimeArgs , 4 , Runtime . PyInt_FromInt32 ( datetime . Minute ) ) ;
332334 Runtime . PyTuple_SetItem ( dateTimeArgs , 5 , Runtime . PyInt_FromInt32 ( datetime . Second ) ) ;
333- Runtime . PyTuple_SetItem ( dateTimeArgs , 6 , Runtime . PyInt_FromInt32 ( datetime . Millisecond ) ) ;
334- Runtime . PyTuple_SetItem ( dateTimeArgs , 7 , TzInfo ( datetime . Kind ) ) ;
335+
336+ // datetime.datetime 6th argument represents micro seconds
337+ var totalSeconds = datetime . TimeOfDay . TotalSeconds ;
338+ var microSeconds = Convert . ToInt32 ( ( totalSeconds - Math . Truncate ( totalSeconds ) ) * 1000000 ) ;
339+ if ( microSeconds == 1000000 ) microSeconds = 999999 ;
340+ Runtime . PyTuple_SetItem ( dateTimeArgs , 6 , Runtime . PyInt_FromInt32 ( microSeconds ) ) ;
341+
342+ if ( size == 8 )
343+ {
344+ Runtime . PyTuple_SetItem ( dateTimeArgs , 7 , TzInfo ( datetime . Kind ) ) ;
345+ }
346+
335347 var returnDateTime = Runtime . PyObject_CallObject ( dateTimeCtor , dateTimeArgs ) ;
336348 // clean up
337349 Runtime . XDecref ( dateTimeArgs ) ;
0 commit comments