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

Obsolete ILocalClock.LocalNowAsync #16752

Merged
merged 6 commits into from
Sep 18, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public async Task<AuditTrailEventQueryResult> QueryAsync(int page, int pageSize,
}
}

var localNow = await _localClock.LocalNowAsync;
var localNow = await _localClock.GetLocalNowAsync();
var startOfWeek = CultureInfo.CurrentUICulture.DateTimeFormat.FirstDayOfWeek;

options.AuditTrailDates =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async ValueTask<FluidValue> ProcessAsync(FluidValue input, FilterArgument

if (stringValue == "now" || stringValue == "today")
{
value = await _localClock.LocalNowAsync;
value = await _localClock.GetLocalNowAsync();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async ValueTask<FluidValue> ProcessAsync(FluidValue input, FilterArgument

if (stringValue == "now" || stringValue == "today")
{
value = await _localClock.LocalNowAsync;
value = await _localClock.GetLocalNowAsync();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ namespace OrchardCore.Modules;
/// </summary>
public interface ILocalClock
{
[Obsolete("This property has been deprecated and will be removed in a future version. Please use GetLocalNowAsync() instead.")]
Task<DateTimeOffset> LocalNowAsync { get; }

/// <summary>
/// Gets the time for the local time zone.
/// </summary>
Task<DateTimeOffset> LocalNowAsync { get; }

#pragma warning disable CS0618 // Type or member is obsolete
Task<DateTimeOffset> GetLocalNowAsync()
=> LocalNowAsync;
#pragma warning restore CS0618 // Type or member is obsolete

/// <summary>
/// Returns the local time zone.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
31 changes: 17 additions & 14 deletions src/OrchardCore/OrchardCore/Modules/Services/LocalClock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,29 @@ public class LocalClock : ILocalClock
private readonly IEnumerable<ITimeZoneSelector> _timeZoneSelectors;
private readonly IClock _clock;
private readonly ICalendarManager _calendarManager;

private ITimeZone _timeZone;

public LocalClock(IEnumerable<ITimeZoneSelector> timeZoneSelectors, IClock clock, ICalendarManager calendarManager)

public LocalClock(
IEnumerable<ITimeZoneSelector> timeZoneSelectors,
IClock clock,
ICalendarManager calendarManager)
{
_timeZoneSelectors = timeZoneSelectors;
_clock = clock;
_calendarManager = calendarManager;
}

public Task<DateTimeOffset> LocalNowAsync
{
get
{
return GetLocalNowAsync();
}
}
=> GetLocalNowAsync();

private async Task<DateTimeOffset> GetLocalNowAsync()
{
return _clock.ConvertToTimeZone(_clock.UtcNow, await GetLocalTimeZoneAsync());
}
public async Task<DateTimeOffset> GetLocalNowAsync()
=> _clock.ConvertToTimeZone(_clock.UtcNow, await GetLocalTimeZoneAsync());

// Caching the result per request.
public async Task<ITimeZone> GetLocalTimeZoneAsync() => _timeZone ??= await LoadLocalTimeZoneAsync();
public async Task<ITimeZone> GetLocalTimeZoneAsync()
=> _timeZone ??= await LoadLocalTimeZoneAsync();

public async Task<DateTimeOffset> ConvertToLocalAsync(DateTimeOffset dateTimeOffSet)
{
Expand All @@ -40,14 +39,17 @@ public async Task<DateTimeOffset> 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<DateTime> ConvertToUtcAsync(DateTime dateTime)
{
var localTimeZone = await GetLocalTimeZoneAsync();
var dateTimeZone = ((TimeZone)localTimeZone).DateTimeZone;
var localDate = LocalDateTime.FromDateTime(dateTime);

return dateTimeZone.AtStrictly(localDate).ToDateTimeUtc();
}

Expand All @@ -69,7 +71,8 @@ private async Task<ITimeZone> LoadLocalTimeZoneAsync()
{
return _clock.GetSystemTimeZone();
}
else if (timeZoneResults.Count > 1)

if (timeZoneResults.Count > 1)
{
timeZoneResults.Sort((x, y) => y.Priority.CompareTo(x.Priority));
}
Expand Down