Browse Source

Refactor timezone handling to use local time zone retrieval and remove deprecated methods

pull/22236/head
maliming 10 months ago
parent
commit
44abba0900
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 14
      framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Timing/AbpTimeZoneMiddleware.cs
  2. 4
      framework/src/Volo.Abp.Timing/Volo/Abp/Timing/ITimezoneProvider.cs
  3. 11
      framework/src/Volo.Abp.Timing/Volo/Abp/Timing/TZConvertTimezoneProvider.cs
  4. 75
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Timing/AbpTimeZoneMiddleware_Tests.cs

14
framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Timing/AbpTimeZoneMiddleware.cs

@ -34,7 +34,7 @@ public class AbpTimeZoneMiddleware : AbpMiddlewareBase, ITransientDependency
if (timezone.IsNullOrWhiteSpace())
{
// Try to get the timezone from the current running server if the setting and request are not available
timezone = context.RequestServices.GetRequiredService<ITimezoneProvider>().GetCurrentIanaTimezoneName();
timezone = await GetLocalTimeZoneAsync();
}
var currentTimezoneProvider = context.RequestServices.GetRequiredService<ICurrentTimezoneProvider>();
@ -67,4 +67,16 @@ public class AbpTimeZoneMiddleware : AbpMiddlewareBase, ITransientDependency
return null;
}
protected virtual Task<string?> GetLocalTimeZoneAsync()
{
if (TimeZoneInfo.Local.HasIanaId)
{
return Task.FromResult<string?>(TimeZoneInfo.Local.Id);
}
return TimeZoneInfo.TryConvertWindowsIdToIanaId(TimeZoneInfo.Local.Id, out var ianaName)
? Task.FromResult<string?>(ianaName)
: Task.FromResult<string?>(null);
}
}

4
framework/src/Volo.Abp.Timing/Volo/Abp/Timing/ITimezoneProvider.cs

@ -14,8 +14,4 @@ public interface ITimezoneProvider
string IanaToWindows(string ianaTimeZoneName);
TimeZoneInfo GetTimeZoneInfo(string windowsOrIanaTimeZoneId);
string GetCurrentWindowsTimezoneName();
string GetCurrentIanaTimezoneName();
}

11
framework/src/Volo.Abp.Timing/Volo/Abp/Timing/TZConvertTimezoneProvider.cs

@ -32,15 +32,4 @@ public class TZConvertTimezoneProvider : ITimezoneProvider, ITransientDependency
{
return TZConvert.GetTimeZoneInfo(windowsOrIanaTimeZoneId);
}
public virtual string GetCurrentWindowsTimezoneName()
{
return TimeZoneInfo.Local.StandardName;;
}
public virtual string GetCurrentIanaTimezoneName()
{
var timezone = GetCurrentWindowsTimezoneName();
return TZConvert.WindowsToIana(timezone);
}
}

75
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Timing/AbpTimeZoneMiddleware_Tests.cs

@ -1,15 +1,11 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
using Volo.Abp.AspNetCore.Mvc.Authorization;
using Volo.Abp.Security.Claims;
using Volo.Abp.Settings;
using Volo.Abp.Timing;
using Xunit;
@ -19,13 +15,10 @@ public class AbpTimeZoneMiddleware_Tests : AspNetCoreMvcTestBase
{
private readonly ICurrentTimezoneProvider _currentTimezoneProvider;
private readonly ITimezoneProvider _timezoneProvider;
private readonly FakeUserClaims _fakeRequiredService;
public AbpTimeZoneMiddleware_Tests()
{
_currentTimezoneProvider = GetRequiredService<ICurrentTimezoneProvider>();
_timezoneProvider = GetRequiredService<ITimezoneProvider>();
_fakeRequiredService = GetRequiredService<FakeUserClaims>();
}
protected override void ConfigureServices(IServiceCollection services)
@ -42,7 +35,7 @@ public class AbpTimeZoneMiddleware_Tests : AspNetCoreMvcTestBase
using (_currentTimezoneProvider.Change("UTC"))
{
var result = await Client.GetStringAsync("api/timing-test");
result.ShouldBe(_timezoneProvider.GetCurrentIanaTimezoneName());
result.ShouldBe(GetLocalTimeZone());
}
// Query string
@ -78,51 +71,79 @@ public class AbpTimeZoneMiddleware_Tests : AspNetCoreMvcTestBase
}
}
[Fact]
public async Task Should_Not_Override_TimeZone_Setting_By_Request_For_Authenticated_User()
private string GetLocalTimeZone()
{
_fakeRequiredService.Claims.AddRange(new[]
if (TimeZoneInfo.Local.HasIanaId)
{
new Claim(AbpClaimTypes.UserId, AuthTestController.FakeUserId.ToString())
return TimeZoneInfo.Local.Id;
}
return TimeZoneInfo.TryConvertWindowsIdToIanaId(TimeZoneInfo.Local.Id, out var ianaName)
? ianaName
: null;
}
}
public class AbpTimeZoneMiddleware_With_SettingValue_Tests : AspNetCoreMvcTestBase
{
private readonly ICurrentTimezoneProvider _currentTimezoneProvider;
public AbpTimeZoneMiddleware_With_SettingValue_Tests()
{
_currentTimezoneProvider = GetRequiredService<ICurrentTimezoneProvider>();
}
protected override void ConfigureServices(IServiceCollection services)
{
var settingStore = Substitute.For<ISettingProvider>();
settingStore.GetOrNullAsync(TimingSettingNames.TimeZone).Returns("Asia/Shanghai");
services.AddSingleton(settingStore);
services.Configure<AbpClockOptions>(options =>
{
options.Kind = DateTimeKind.Utc;
});
}
using (_currentTimezoneProvider.Change("Europe/Berlin"))
[Fact]
public async Task Should_Not_Override_TimeZone_Setting_By_Request()
{
using (_currentTimezoneProvider.Change("UTC"))
{
var result = await Client.GetStringAsync("api/timing-test");
result.ShouldBe(_timezoneProvider.GetCurrentIanaTimezoneName());
result.ShouldBe("Asia/Shanghai");
}
// Query string
using (_currentTimezoneProvider.Change("Europe/Berlin"))
using (_currentTimezoneProvider.Change("UTC"))
{
var result = await Client.GetStringAsync("api/timing-test?__timezone=Europe/Istanbul");
result.ShouldBe(_timezoneProvider.GetCurrentIanaTimezoneName());
result.ShouldBe("Asia/Shanghai");
}
// Header
using (_currentTimezoneProvider.Change("Europe/Berlin"))
using (_currentTimezoneProvider.Change("UTC"))
{
Client.DefaultRequestHeaders.Add("__timezone", "Asia/Shanghai");
Client.DefaultRequestHeaders.Add("__timezone", "Europe/Istanbul");
var result = await Client.GetStringAsync("api/timing-test");
result.ShouldBe("UTC");
result.ShouldBe("Asia/Shanghai");
}
// Form
using (_currentTimezoneProvider.Change("Europe/Berlin"))
using (_currentTimezoneProvider.Change("UTC"))
{
Client.DefaultRequestHeaders.Remove("__timezone");
var result = await Client.PostAsync("api/timing-test", new FormUrlEncodedContent(new[] {new KeyValuePair<string, string>("__timezone", "Europe/Germany")}));
(await result.Content.ReadAsStringAsync()).ShouldBe("UTC");
var result = await Client.PostAsync("api/timing-test", new FormUrlEncodedContent(new[] {new KeyValuePair<string, string>("__timezone", "Europe/Istanbul")}));
(await result.Content.ReadAsStringAsync()).ShouldBe("Asia/Shanghai");
}
// Cookie
using (_currentTimezoneProvider.Change("Europe/Berlin"))
using (_currentTimezoneProvider.Change("UTC"))
{
Client.DefaultRequestHeaders.Remove("__timezone");
Client.DefaultRequestHeaders.Add("Cookie", "__timezone=Europe/Istanbul");
var result = await Client.GetStringAsync("api/timing-test");
result.ShouldBe("UTC");
result.ShouldBe("Asia/Shanghai");
}
}
}

Loading…
Cancel
Save