Skip to content

Commit

Permalink
fix: set behavior depending on timeZoneInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
VitaliyChaban committed Sep 23, 2024
1 parent cafa260 commit 83450c0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ public static object ConvertPrimitiveValue(object value, Type type, TimeZoneInfo
{
DateTimeOffset dateTimeOffsetValue = (DateTimeOffset)value;
TimeZoneInfo timeZone = timeZoneInfo ?? TimeZoneInfo.Local;

dateTimeOffsetValue = TimeZoneInfo.ConvertTime(dateTimeOffsetValue, timeZone);
return DateTime.SpecifyKind(dateTimeOffsetValue.DateTime, DateTimeKind.Utc);

var dateTimeKind = GetTargetDateTimeKind(timeZone);

return DateTime.SpecifyKind(dateTimeOffsetValue.DateTime, dateTimeKind);
}

if (value is Date)
Expand Down Expand Up @@ -183,4 +187,15 @@ public static object ConvertPrimitiveValue(object value, Type type, TimeZoneInfo
}
}
}
}

private static DateTimeKind GetTargetDateTimeKind(TimeZoneInfo timeZone)
{
if (timeZone.Equals(TimeZoneInfo.Local))
return DateTimeKind.Local;

if (timeZone.Equals(TimeZoneInfo.Utc))
return DateTimeKind.Utc;

return DateTimeKind.Unspecified;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void ConvertDateTimeValue_NonStandardPrimitives_DefaultTimeZoneInfo(DateT

[Theory]
[MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))]
public void ConvertDateTimeValue_ExplicitUtc(DateTimeOffset valueToConvert)
public void ConvertDateTimeValue_ImplicitKind(DateTimeOffset valueToConvert)
{
//Some databases (for example, Npgsql) require an explicit indication of Kind = Utc
//and do not accept Local and Unspecified in the new versions of the framework
Expand All @@ -136,6 +136,40 @@ public void ConvertDateTimeValue_ExplicitUtc(DateTimeOffset valueToConvert)
// Arrange & Act
object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime));

// Assert
DateTime dt = Assert.IsType<DateTime>(actual);
Assert.Equal(DateTimeKind.Local, dt.Kind);
}

[Theory]
[MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))]
public void ConvertDateTimeValue_ExplicitLocalKind(DateTimeOffset valueToConvert)
{
//Some databases (for example, Npgsql) require an explicit indication of Kind = Utc
//and do not accept Local and Unspecified in the new versions of the framework

//example: Cannot write DateTime with Kind=Unspecified to PostgreSQL type 'timestamp with time zone', only UTC is supported

// Arrange & Act
object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime), TimeZoneInfo.Local);

// Assert
DateTime dt = Assert.IsType<DateTime>(actual);
Assert.Equal(DateTimeKind.Local, dt.Kind);
}

[Theory]
[MemberData(nameof(ConvertDateTime_NonStandardPrimitives_Data))]
public void ConvertDateTimeValue_ExplicitUtcKind(DateTimeOffset valueToConvert)
{
//Some databases (for example, Npgsql) require an explicit indication of Kind = Utc
//and do not accept Local and Unspecified in the new versions of the framework

//example: Cannot write DateTime with Kind=Unspecified to PostgreSQL type 'timestamp with time zone', only UTC is supported

// Arrange & Act
object actual = EdmPrimitiveHelper.ConvertPrimitiveValue(valueToConvert, typeof(DateTime), TimeZoneInfo.Utc);

// Assert
DateTime dt = Assert.IsType<DateTime>(actual);
Assert.Equal(DateTimeKind.Utc, dt.Kind);
Expand Down

0 comments on commit 83450c0

Please sign in to comment.