Skip to content

Commit

Permalink
Remove tzinfo check in datetime conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonabreul committed Feb 28, 2024
1 parent 0678780 commit 363b352
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 35 deletions.
9 changes: 4 additions & 5 deletions src/embed_tests/TestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ public void ConvertDateTimeRoundTrip(DateTimeKind kind)
Assert.AreEqual(datetime, result);
}

[TestCase("", DateTimeKind.Unspecified)]
[TestCase("America/New_York", DateTimeKind.Unspecified)]
[TestCase("UTC", DateTimeKind.Utc)]
public void ConvertDateTimeWithTimeZonePythonToCSharp(string timeZone, DateTimeKind expectedDateTimeKind)
[TestCase("")]
[TestCase("America/New_York")]
[TestCase("UTC")]
public void ConvertDateTimeWithTimeZonePythonToCSharp(string timeZone)
{
const int year = 2024;
const int month = 2;
Expand Down Expand Up @@ -231,7 +231,6 @@ def GetNextDay(dateTime):

var expectedDateTime = new DateTime(year, month, day, hour, minute, second);
Assert.AreEqual(expectedDateTime, managedDateTime);
Assert.AreEqual(managedDateTime.Kind, expectedDateTimeKind);
}
}

Expand Down
31 changes: 1 addition & 30 deletions src/runtime/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,24 +1123,6 @@ 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 (!tzinfo.IsNone() && !tzinfo.IsNull())
{
var tznameMethod = Runtime.PyObject_GetAttrString(tzinfo.Borrow(), new StrPtr("tzname", Encoding.UTF8));
var args = Runtime.PyTuple_New(1);
Runtime.PyTuple_SetItem(args.Borrow(), 0, Runtime.None.Steal());
var tznameObj = Runtime.PyObject_CallObject(tznameMethod.Borrow(), args.Borrow());
var tzname = Runtime.GetManagedString(tznameObj.Borrow());

if (tzname.Contains("UTC", StringComparison.InvariantCultureIgnoreCase))
{
timeKind = DateTimeKind.Utc;
}
}

var convertedHour = 0L;
var convertedMinute = 0L;
Expand All @@ -1161,8 +1143,7 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec
(int)convertedHour,
(int)convertedMinute,
(int)convertedSecond,
millisecond: (int)milliseconds,
timeKind);
(int)milliseconds);

year.Dispose();
month.Dispose();
Expand All @@ -1172,16 +1153,6 @@ 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

0 comments on commit 363b352

Please sign in to comment.