diff --git a/src/OrchardCore.Modules/OrchardCore.AuditTrail/Services/DefaultAuditTrailAdminListQueryService.cs b/src/OrchardCore.Modules/OrchardCore.AuditTrail/Services/DefaultAuditTrailAdminListQueryService.cs index 4d74d31a419..45069577e40 100644 --- a/src/OrchardCore.Modules/OrchardCore.AuditTrail/Services/DefaultAuditTrailAdminListQueryService.cs +++ b/src/OrchardCore.Modules/OrchardCore.AuditTrail/Services/DefaultAuditTrailAdminListQueryService.cs @@ -87,7 +87,7 @@ public async Task QueryAsync(int page, int pageSize, } } - var localNow = await _localClock.LocalNowAsync; + var localNow = await _localClock.GetLocalNowAsync(); var startOfWeek = CultureInfo.CurrentUICulture.DateTimeFormat.FirstDayOfWeek; options.AuditTrailDates = diff --git a/src/OrchardCore.Modules/OrchardCore.Liquid/Filters/LocalTimeZoneFilter.cs b/src/OrchardCore.Modules/OrchardCore.Liquid/Filters/LocalTimeZoneFilter.cs index d109ff16896..fe8eff67a06 100644 --- a/src/OrchardCore.Modules/OrchardCore.Liquid/Filters/LocalTimeZoneFilter.cs +++ b/src/OrchardCore.Modules/OrchardCore.Liquid/Filters/LocalTimeZoneFilter.cs @@ -24,7 +24,7 @@ public async ValueTask ProcessAsync(FluidValue input, FilterArgument if (stringValue == "now" || stringValue == "today") { - value = await _localClock.LocalNowAsync; + value = await _localClock.GetLocalNowAsync(); } else { diff --git a/src/OrchardCore.Modules/OrchardCore.Liquid/Filters/UtcTimeZoneFilter.cs b/src/OrchardCore.Modules/OrchardCore.Liquid/Filters/UtcTimeZoneFilter.cs index a56eee9c0ab..481ddcef24d 100644 --- a/src/OrchardCore.Modules/OrchardCore.Liquid/Filters/UtcTimeZoneFilter.cs +++ b/src/OrchardCore.Modules/OrchardCore.Liquid/Filters/UtcTimeZoneFilter.cs @@ -24,7 +24,7 @@ public async ValueTask ProcessAsync(FluidValue input, FilterArgument if (stringValue == "now" || stringValue == "today") { - value = await _localClock.LocalNowAsync; + value = await _localClock.GetLocalNowAsync(); } else { diff --git a/src/OrchardCore/OrchardCore.Abstractions/Modules/Services/ILocalClock.cs b/src/OrchardCore/OrchardCore.Abstractions/Modules/Services/ILocalClock.cs index f25bf16acb6..47c268f0324 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Modules/Services/ILocalClock.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Modules/Services/ILocalClock.cs @@ -5,10 +5,17 @@ namespace OrchardCore.Modules; /// public interface ILocalClock { + [Obsolete("This property has been deprecated and will be removed in a future version. Please use GetLocalNowAsync() instead.")] + Task LocalNowAsync { get; } + /// /// Gets the time for the local time zone. /// - Task LocalNowAsync { get; } + +#pragma warning disable CS0618 // Type or member is obsolete + Task GetLocalNowAsync() + => LocalNowAsync; +#pragma warning restore CS0618 // Type or member is obsolete /// /// Returns the local time zone. diff --git a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/LiquidViewTemplate.cs b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/LiquidViewTemplate.cs index 0753650a14f..b3dcbf9f8ad 100644 --- a/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/LiquidViewTemplate.cs +++ b/src/OrchardCore/OrchardCore.DisplayManagement.Liquid/LiquidViewTemplate.cs @@ -244,7 +244,7 @@ internal static async Task EnterScopeAsync(this LiquidTemplateContext context, V } // Configure Fluid with the local date and time - var now = await localClock.LocalNowAsync; + var now = await localClock.GetLocalNowAsync(); context.Now = () => now; diff --git a/src/OrchardCore/OrchardCore/Modules/Services/LocalClock.cs b/src/OrchardCore/OrchardCore/Modules/Services/LocalClock.cs index 635afe40018..905e0f1f536 100644 --- a/src/OrchardCore/OrchardCore/Modules/Services/LocalClock.cs +++ b/src/OrchardCore/OrchardCore/Modules/Services/LocalClock.cs @@ -8,9 +8,14 @@ public class LocalClock : ILocalClock private readonly IEnumerable _timeZoneSelectors; private readonly IClock _clock; private readonly ICalendarManager _calendarManager; + private ITimeZone _timeZone; - public LocalClock(IEnumerable timeZoneSelectors, IClock clock, ICalendarManager calendarManager) + + public LocalClock( + IEnumerable timeZoneSelectors, + IClock clock, + ICalendarManager calendarManager) { _timeZoneSelectors = timeZoneSelectors; _clock = clock; @@ -18,20 +23,14 @@ public LocalClock(IEnumerable timeZoneSelectors, IClock clock } public Task LocalNowAsync - { - get - { - return GetLocalNowAsync(); - } - } + => GetLocalNowAsync(); - private async Task GetLocalNowAsync() - { - return _clock.ConvertToTimeZone(_clock.UtcNow, await GetLocalTimeZoneAsync()); - } + public async Task GetLocalNowAsync() + => _clock.ConvertToTimeZone(_clock.UtcNow, await GetLocalTimeZoneAsync()); // Caching the result per request. - public async Task GetLocalTimeZoneAsync() => _timeZone ??= await LoadLocalTimeZoneAsync(); + public async Task GetLocalTimeZoneAsync() + => _timeZone ??= await LoadLocalTimeZoneAsync(); public async Task ConvertToLocalAsync(DateTimeOffset dateTimeOffSet) { @@ -40,7 +39,9 @@ public async Task ConvertToLocalAsync(DateTimeOffset dateTimeOff var offsetDateTime = OffsetDateTime.FromDateTimeOffset(dateTimeOffSet); var currentCalendar = BclCalendars.GetCalendarByName(await _calendarManager.GetCurrentCalendar()); - return offsetDateTime.InZone(dateTimeZone).WithCalendar(currentCalendar).ToDateTimeOffset(); + return offsetDateTime.InZone(dateTimeZone) + .WithCalendar(currentCalendar) + .ToDateTimeOffset(); } public async Task ConvertToUtcAsync(DateTime dateTime) @@ -48,6 +49,7 @@ public async Task ConvertToUtcAsync(DateTime dateTime) var localTimeZone = await GetLocalTimeZoneAsync(); var dateTimeZone = ((TimeZone)localTimeZone).DateTimeZone; var localDate = LocalDateTime.FromDateTime(dateTime); + return dateTimeZone.AtStrictly(localDate).ToDateTimeUtc(); } @@ -69,7 +71,8 @@ private async Task LoadLocalTimeZoneAsync() { return _clock.GetSystemTimeZone(); } - else if (timeZoneResults.Count > 1) + + if (timeZoneResults.Count > 1) { timeZoneResults.Sort((x, y) => y.Priority.CompareTo(x.Priority)); }