@@ -323,15 +323,27 @@ internal static IntPtr ToPython(object value, Type type)
323
323
case TypeCode . DateTime :
324
324
var datetime = ( DateTime ) value ;
325
325
326
- IntPtr dateTimeArgs = Runtime . PyTuple_New ( 8 ) ;
326
+ var size = datetime . Kind == DateTimeKind . Unspecified ? 7 : 8 ;
327
+
328
+ IntPtr dateTimeArgs = Runtime . PyTuple_New ( size ) ;
327
329
Runtime . PyTuple_SetItem ( dateTimeArgs , 0 , Runtime . PyInt_FromInt32 ( datetime . Year ) ) ;
328
330
Runtime . PyTuple_SetItem ( dateTimeArgs , 1 , Runtime . PyInt_FromInt32 ( datetime . Month ) ) ;
329
331
Runtime . PyTuple_SetItem ( dateTimeArgs , 2 , Runtime . PyInt_FromInt32 ( datetime . Day ) ) ;
330
332
Runtime . PyTuple_SetItem ( dateTimeArgs , 3 , Runtime . PyInt_FromInt32 ( datetime . Hour ) ) ;
331
333
Runtime . PyTuple_SetItem ( dateTimeArgs , 4 , Runtime . PyInt_FromInt32 ( datetime . Minute ) ) ;
332
334
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
+
335
347
var returnDateTime = Runtime . PyObject_CallObject ( dateTimeCtor , dateTimeArgs ) ;
336
348
// clean up
337
349
Runtime . XDecref ( dateTimeArgs ) ;
0 commit comments