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

Using timezone settings to display datetime. #22236

Draft
wants to merge 15 commits into
base: dev
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
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
<PackageVersion Include="System.Text.Json" Version="9.0.0" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageVersion Include="TencentCloudSDK.Sms" Version="3.0.1142" />
<PackageVersion Include="TimeZoneConverter" Version="6.1.0" />
<PackageVersion Include="TimeZoneConverter" Version="7.0.0" />
<PackageVersion Include="Unidecode.NET" Version="2.1.0" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.extensibility.execution" Version="2.9.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Volo.Abp.AspNetCore.Mvc.Client;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Timing;

namespace Volo.Abp.AspNetCore.Components.MauiBlazor
{
Expand All @@ -18,16 +19,20 @@ public class MauiBlazorCachedApplicationConfigurationClient : ICachedApplication

protected ICurrentTenantAccessor CurrentTenantAccessor { get; }

protected ICurrentTimezoneProvider CurrentTimezoneProvider { get; }

public MauiBlazorCachedApplicationConfigurationClient(
AbpApplicationConfigurationClientProxy applicationConfigurationClientProxy,
ApplicationConfigurationCache cache,
ICurrentTenantAccessor currentTenantAccessor,
ICurrentTimezoneProvider currentTimezoneProvider,
AuthenticationStateProvider authenticationStateProvider,
AbpApplicationLocalizationClientProxy applicationLocalizationClientProxy)
{
ApplicationConfigurationClientProxy = applicationConfigurationClientProxy;
Cache = cache;
CurrentTenantAccessor = currentTenantAccessor;
CurrentTimezoneProvider = currentTimezoneProvider;
ApplicationLocalizationClientProxy = applicationLocalizationClientProxy;

authenticationStateProvider.AuthenticationStateChanged += async _ => { await InitializeAsync(); };
Expand All @@ -51,6 +56,7 @@ public virtual async Task InitializeAsync()
);

configurationDto.Localization.Resources = localizationDto.Resources;
CurrentTimezoneProvider.TimeZone = configurationDto.Timing.TimeZone.Iana.TimeZoneName;

Cache.Set(configurationDto);

Expand Down Expand Up @@ -81,4 +87,4 @@ private ApplicationConfigurationDto GetConfigurationByChecking()
return configuration;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Volo.Abp.DependencyInjection;
using Volo.Abp.Timing;

namespace Volo.Abp.AspNetCore.Components.MauiBlazor;

[Dependency(ReplaceServices = true)]
public class MauiBlazorCurrentTimezoneProvider : ICurrentTimezoneProvider, ISingletonDependency
{
public string? TimeZone { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Volo.Abp.AspNetCore.Mvc.Client;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Timing;

namespace Volo.Abp.AspNetCore.Components.WebAssembly;

Expand All @@ -19,6 +20,8 @@ public class WebAssemblyCachedApplicationConfigurationClient : ICachedApplicatio

protected ICurrentTenantAccessor CurrentTenantAccessor { get; }

protected ICurrentTimezoneProvider CurrentTimezoneProvider { get; }

protected ApplicationConfigurationChangedService ApplicationConfigurationChangedService { get; }

protected IJSRuntime JSRuntime { get; }
Expand All @@ -27,13 +30,15 @@ public WebAssemblyCachedApplicationConfigurationClient(
AbpApplicationConfigurationClientProxy applicationConfigurationClientProxy,
ApplicationConfigurationCache cache,
ICurrentTenantAccessor currentTenantAccessor,
ICurrentTimezoneProvider currentTimezoneProvider,
AbpApplicationLocalizationClientProxy applicationLocalizationClientProxy,
ApplicationConfigurationChangedService applicationConfigurationChangedService,
IJSRuntime jsRuntime)
{
ApplicationConfigurationClientProxy = applicationConfigurationClientProxy;
Cache = cache;
CurrentTenantAccessor = currentTenantAccessor;
CurrentTimezoneProvider = currentTimezoneProvider;
ApplicationLocalizationClientProxy = applicationLocalizationClientProxy;
ApplicationConfigurationChangedService = applicationConfigurationChangedService;
JSRuntime = jsRuntime;
Expand Down Expand Up @@ -69,6 +74,8 @@ public virtual async Task InitializeAsync()
configurationDto.CurrentTenant.Id,
configurationDto.CurrentTenant.Name
);

CurrentTimezoneProvider.TimeZone = configurationDto.Timing.TimeZone.Iana.TimeZoneName;
}

public virtual Task<ApplicationConfigurationDto> GetAsync()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Volo.Abp.DependencyInjection;
using Volo.Abp.Timing;

namespace Volo.Abp.AspNetCore.Components.WebAssembly;

[Dependency(ReplaceServices = true)]
public class WebAssemblyCurrentTimezoneProvider : ICurrentTimezoneProvider, ISingletonDependency
{
public string? TimeZone { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public override void ConfigureServices(ServiceConfigurationContext context)
context.Services.AddOptions<MvcNewtonsoftJsonOptions>()
.Configure<IServiceProvider>((options, rootServiceProvider) =>
{
options.SerializerSettings.ContractResolver = new AbpCamelCasePropertyNamesContractResolver(rootServiceProvider.GetRequiredService<AbpDateTimeConverter>());
options.SerializerSettings.ContractResolver =
new AbpCamelCasePropertyNamesContractResolver(rootServiceProvider
.GetRequiredService<AbpDateTimeConverter>());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,70 +16,94 @@
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Button;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Extensions;
using Volo.Abp.Json;
using Volo.Abp.Timing;

namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form.DatePicker;

public abstract class AbpDatePickerBaseTagHelperService<TTagHelper> : AbpTagHelperService<TTagHelper>
where TTagHelper : AbpDatePickerBaseTagHelper<TTagHelper>
{
protected readonly Dictionary<Type, Func<object, string>> SupportedInputTypes = new()
{
{
typeof(string), o =>
{
if(o is string s && DateTime.TryParse(s, out var dt))
{
return dt.ToString("O");
}

return string.Empty;
}
},
{
typeof(DateTime), o =>
{
if(o is DateTime dt && dt != default)
{
return dt.ToString("O");
}

return string.Empty;
}
},
{typeof(DateTime?), o => ((DateTime?) o)?.ToString("O")!},
{
typeof(DateTimeOffset), o =>
{
if(o is DateTimeOffset dto && dto != default)
{
return dto.ToString("O");
}

return string.Empty;
}
},
{typeof(DateTimeOffset?), o => ((DateTimeOffset?) o)?.ToString("O")!}
};
protected readonly Dictionary<Type, Func<object, string>> SupportedInputTypes;

protected readonly IJsonSerializer JsonSerializer;
protected readonly IHtmlGenerator Generator;
protected readonly HtmlEncoder Encoder;
protected readonly IServiceProvider ServiceProvider;
protected readonly IAbpTagHelperLocalizer TagHelperLocalizer;
protected readonly IClock Clock;
protected virtual string TagName { get; set; } = "abp-date-picker";
protected IStringLocalizer<AbpUiResource> L { get; }
protected abstract TagHelperOutput TagHelperOutput { get; set; }

protected AbpDatePickerBaseTagHelperService(IJsonSerializer jsonSerializer, IHtmlGenerator generator,
HtmlEncoder encoder, IServiceProvider serviceProvider, IStringLocalizer<AbpUiResource> l,
IAbpTagHelperLocalizer tagHelperLocalizer)
IAbpTagHelperLocalizer tagHelperLocalizer, IClock clock)
{
JsonSerializer = jsonSerializer;
Generator = generator;
Encoder = encoder;
ServiceProvider = serviceProvider;
L = l;
TagHelperLocalizer = tagHelperLocalizer;
Clock = clock;

SupportedInputTypes = new Dictionary<Type, Func<object, string>>
{
{
typeof(string), x =>
{
if(x is string s && DateTime.TryParse(s, out var dt))
{
return Clock.Convert(dt).ToString("O");
}

return string.Empty;
}
},
{
typeof(DateTime), x =>
{
if(x is DateTime dt && dt != default)
{
return Clock.Convert(dt).ToString("O");
}

return string.Empty;
}
},
{
typeof(DateTime?), x =>
{
if(x is DateTime dt && dt != default)
{
return Clock.Convert(dt).ToString("O");
}
return string.Empty;
}
},
{
typeof(DateTimeOffset), x =>
{
if(x is DateTimeOffset dto && dto != default)
{
return Clock.Convert(dto).ToString("O");
}

return string.Empty;
}
},
{
typeof(DateTimeOffset?), x =>
{
if(x is DateTimeOffset dto && dto != default)
{
return Clock.Convert(dto).ToString("O");
}

return string.Empty;
}
}
};
}

protected virtual T? GetAttribute<T>() where T : Attribute
Expand Down Expand Up @@ -233,6 +257,7 @@ protected virtual void AddGroupToFormGroupContents(TagHelperContext context, str
}

protected abstract int GetOrder();

protected abstract void AddBaseTagAttributes(TagHelperAttributeList attributes);

protected virtual string GetExtraInputHtml(TagHelperContext context, TagHelperOutput output)
Expand Down Expand Up @@ -375,7 +400,7 @@ protected TagHelperAttributeList ConvertDatePickerOptionsToAttributeList(IAbpDat
{
attrList.Add("data-visible-date-format", options.VisibleDateFormat);
}

if(!options.InputDateFormat.IsNullOrEmpty())
{
attrList.Add("data-input-date-format", options.InputDateFormat);
Expand Down Expand Up @@ -754,7 +779,7 @@ protected virtual Task<string> GetValidationAsHtmlAsync(TagHelperContext context
{
return Task.FromResult(string.Empty);
}

return GetValidationAsHtmlByInputAsync(context, output, @for);
}

Expand All @@ -766,7 +791,7 @@ protected virtual async Task<string> GetValidationAsHtmlByInputAsync(TagHelperCo
new ValidationMessageTagHelper(Generator) { For = @for, ViewContext = TagHelper.ViewContext };

var attributeList = new TagHelperAttributeList { { "class", "text-danger" } };

if(!output.Attributes.TryGetAttribute("name", out var nameAttribute) || nameAttribute == null || nameAttribute.Value == null)
{
if (nameAttribute != null)
Expand All @@ -776,7 +801,7 @@ protected virtual async Task<string> GetValidationAsHtmlByInputAsync(TagHelperCo
nameAttribute = new TagHelperAttribute("name", "date_" + Guid.NewGuid().ToString("N"));
output.Attributes.Add(nameAttribute);
}

attributeList.Add("data-valmsg-for", nameAttribute.Value);

return await validationMessageTagHelper.RenderAsync(attributeList, context, Encoder, "span",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@
using Microsoft.Extensions.Localization;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Extensions;
using Volo.Abp.Json;
using Volo.Abp.Timing;

namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form.DatePicker;

public class AbpDatePickerTagHelperService : AbpDatePickerBaseTagHelperService<AbpDatePickerTagHelper>
{
public AbpDatePickerTagHelperService(IJsonSerializer jsonSerializer, IHtmlGenerator generator, HtmlEncoder encoder, IServiceProvider serviceProvider, IStringLocalizer<AbpUiResource> l, IAbpTagHelperLocalizer tagHelperLocalizer) : base(jsonSerializer, generator, encoder, serviceProvider, l, tagHelperLocalizer)
public AbpDatePickerTagHelperService(
IJsonSerializer jsonSerializer,
IHtmlGenerator generator,
HtmlEncoder encoder,
IServiceProvider serviceProvider,
IStringLocalizer<AbpUiResource> l,
IAbpTagHelperLocalizer tagHelperLocalizer,
IClock clock)
: base(jsonSerializer, generator, encoder, serviceProvider, l, tagHelperLocalizer, clock)
{

}

protected override TagHelperOutput TagHelperOutput { get; set; } = default!;
Expand All @@ -42,10 +50,18 @@ public async override Task ProcessAsync(TagHelperContext context, TagHelperOutpu
{
InputTypeName = "hidden",
ViewContext = TagHelper.ViewContext,
For = TagHelper.AspFor,
For = TagHelper.AspFor
};

var attributes = new TagHelperAttributeList { { "data-date", "true" }, { "type", "hidden" } };

if (Clock.SupportsMultipleTimezone && TagHelper.AspFor.Model is DateTime dateTime)
{
DateTagHelper.Format = "{0:O}";
DateTagHelper.Value = Clock.Convert(dateTime).ToString("O");
attributes.Add("value", DateTagHelper.Value);
}

DateTagHelperOutput = await DateTagHelper.ProcessAndGetOutputAsync(attributes, context, "input");
}

Expand Down Expand Up @@ -80,4 +96,4 @@ protected override string GetExtraInputHtml(TagHelperContext context, TagHelperO
{
return DateTagHelperOutput?.Render(Encoder)!;
}
}
}
Loading