Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'CsWinRTUseUtcDateTimeOffsetMarshalling' switch #1602

Draft
wants to merge 1 commit into
base: staging/AOT
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions nuget/Microsoft.Windows.CsWinRT.targets
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ $(CsWinRTInternalProjection)
<PropertyGroup>
<CsWinRTEnableDynamicObjectsSupport Condition="'$(CsWinRTEnableDynamicObjectsSupport)' == ''">true</CsWinRTEnableDynamicObjectsSupport>
<CsWinRTUseExceptionResourceKeys Condition="'$(CsWinRTUseExceptionResourceKeys)' == ''">false</CsWinRTUseExceptionResourceKeys>
<CsWinRTUseUtcDateTimeOffsetMarshalling Condition="'$(CsWinRTUseUtcDateTimeOffsetMarshalling)' == ''">false</CsWinRTUseUtcDateTimeOffsetMarshalling>
<CsWinRTEnableDefaultCustomTypeMappings Condition="'$(CsWinRTEnableDefaultCustomTypeMappings)' == ''">true</CsWinRTEnableDefaultCustomTypeMappings>
<CsWinRTEnableICustomPropertyProviderSupport Condition="'$(CsWinRTEnableICustomPropertyProviderSupport)' == ''">true</CsWinRTEnableICustomPropertyProviderSupport>
<CsWinRTEnableIReferenceSupport Condition="'$(CsWinRTEnableIReferenceSupport)' == ''">true</CsWinRTEnableIReferenceSupport>
Expand All @@ -272,6 +273,11 @@ $(CsWinRTInternalProjection)
<RuntimeHostConfigurationOption Include="CSWINRT_USE_EXCEPTION_RESOURCE_KEYS"
Value="$(CsWinRTUseExceptionResourceKeys)"
Trim="true" />

<!-- CSWINRT_USE_UTC_DATETIMEOFFSET_MARSHALLING switch -->
<RuntimeHostConfigurationOption Include="CSWINRT_USE_UTC_DATETIMEOFFSET_MARSHALLING"
Value="$(CsWinRTUseUtcDateTimeOffsetMarshalling)"
Trim="true" />

<!-- CSWINRT_ENABLE_DEFAULT_CUSTOM_TYPE_MAPPINGS switch -->
<RuntimeHostConfigurationOption Include="CSWINRT_ENABLE_DEFAULT_CUSTOM_TYPE_MAPPINGS"
Expand Down
25 changes: 22 additions & 3 deletions src/WinRT.Runtime/Configuration/FeatureSwitches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ internal static class FeatureSwitches
/// <summary>
/// The configuration property name for <see cref="EnableDynamicObjectsSupport"/>.
/// </summary>
private const string EnablesDynamicObjectsSupportPropertyName = "CSWINRT_ENABLE_DYNAMIC_OBJECTS_SUPPORT";
private const string EnableDynamicObjectsSupportPropertyName = "CSWINRT_ENABLE_DYNAMIC_OBJECTS_SUPPORT";

/// <summary>
/// The configuration property name for <see cref="UseExceptionResourceKeys"/>.
/// The configuration property name for <see cref="UseUtcDateTimeOffsetMarshalling"/>.
/// </summary>
private const string UseExceptionResourceKeysPropertyName = "CSWINRT_USE_EXCEPTION_RESOURCE_KEYS";

/// <summary>
/// The configuration property name for <see cref="UseExceptionResourceKeys"/>.
/// </summary>
private const string UseUtcDateTimeOffsetMarshallingPropertyName = "CSWINRT_USE_UTC_DATETIMEOFFSET_MARSHALLING";

/// <summary>
/// The configuration property name for <see cref="EnableDefaultCustomTypeMappings"/>.
/// </summary>
Expand Down Expand Up @@ -61,6 +66,11 @@ internal static class FeatureSwitches
/// </summary>
private static int _useExceptionResourceKeys;

/// <summary>
/// The backing field for <see cref="UseUtcDateTimeOffsetMarshalling"/>.
/// </summary>
private static int _useUtcDateTimeOffsetMarshalling;

/// <summary>
/// The backing field for <see cref="EnableDefaultCustomTypeMappings"/>.
/// </summary>
Expand All @@ -87,7 +97,7 @@ internal static class FeatureSwitches
public static bool EnableDynamicObjectsSupport
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetConfigurationValue(EnablesDynamicObjectsSupportPropertyName, ref _enableDynamicObjectsSupportEnabled, true);
get => GetConfigurationValue(EnableDynamicObjectsSupportPropertyName, ref _enableDynamicObjectsSupportEnabled, true);
}

/// <summary>
Expand All @@ -99,6 +109,15 @@ public static bool UseExceptionResourceKeys
get => GetConfigurationValue(UseExceptionResourceKeysPropertyName, ref _useExceptionResourceKeys, false);
}

/// <summary>
/// Gets a value indicating whether or not <see cref="DateTimeOffset"/> values should be marshalled as UTC (defaults to <see langword="false"/>).
/// </summary>
public static bool UseUtcDateTimeOffsetMarshalling
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetConfigurationValue(UseUtcDateTimeOffsetMarshallingPropertyName, ref _useUtcDateTimeOffsetMarshalling, false);
}

/// <summary>
/// Gets a value indicating whether or not <see cref="Projections"/> should initialize all default type mappings automatically (defaults to <see langword="true"/>).
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions src/WinRT.Runtime/Configuration/ILLink.Substitutions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<method signature="System.Boolean get_UseExceptionResourceKeys()" body="stub" value="false" feature="CSWINRT_USE_EXCEPTION_RESOURCE_KEYS" featurevalue="false"/>
<method signature="System.Boolean get_UseExceptionResourceKeys()" body="stub" value="true" feature="CSWINRT_USE_EXCEPTION_RESOURCE_KEYS" featurevalue="true"/>

<!-- CSWINRT_USE_UTC_DATETIMEOFFSET_MARSHALLING switch -->
<method signature="System.Boolean get_UseUtcDateTimeOffsetMarshalling()" body="stub" value="false" feature="CSWINRT_USE_UTC_DATETIMEOFFSET_MARSHALLING" featurevalue="false"/>
<method signature="System.Boolean get_UseUtcDateTimeOffsetMarshalling()" body="stub" value="true" feature="CSWINRT_USE_UTC_DATETIMEOFFSET_MARSHALLING" featurevalue="true"/>

<!-- CSWINRT_ENABLE_DEFAULT_CUSTOM_TYPE_MAPPINGS switch -->
<method signature="System.Boolean get_EnableDefaultCustomTypeMappings()" body="stub" value="false" feature="CSWINRT_ENABLE_DEFAULT_CUSTOM_TYPE_MAPPINGS" featurevalue="false"/>
<method signature="System.Boolean get_EnableDefaultCustomTypeMappings()" body="stub" value="true" feature="CSWINRT_ENABLE_DEFAULT_CUSTOM_TYPE_MAPPINGS" featurevalue="true"/>
Expand Down
10 changes: 10 additions & 0 deletions src/WinRT.Runtime/Projections/SystemTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Runtime.InteropServices;
using WinRT;

namespace ABI.System
{
Expand Down Expand Up @@ -86,6 +87,15 @@ public static Marshaler CreateMarshaler(global::System.DateTimeOffset value)
public static global::System.DateTimeOffset FromAbi(DateTimeOffset value)
{
var utcTime = new global::System.DateTimeOffset(value.UniversalTime + ManagedUtcTicksAtNativeZero, global::System.TimeSpan.Zero);

if (FeatureSwitches.UseUtcDateTimeOffsetMarshalling)
{
return utcTime;
}

// Backwards-compat: validate the local time and return that instead of the UTC one.
// We can't make this the default behavior, as people might have taken a dependency
// on this, since it's been here for quite a while. So, we have a feature switch instead.
var offset = TimeZoneInfo.Local.GetUtcOffset(utcTime);
long localTicks = utcTime.Ticks + offset.Ticks;
if (localTicks < DateTime.MinValue.Ticks || localTicks > DateTime.MaxValue.Ticks)
Expand Down
Loading