Skip to content

Commit

Permalink
Obsolete ILocalClock.LocalNowAsync (#16752)
Browse files Browse the repository at this point in the history
Co-authored-by: Mike Alhayek <[email protected]>
  • Loading branch information
hishamco and MikeAlhayek authored Sep 18, 2024
1 parent 54e7ab3 commit dd03cf8
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
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

0 comments on commit dd03cf8

Please sign in to comment.