Skip to content

Commit

Permalink
Fixes a time zone offset issue with the date filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jschick04 authored and bill-long committed Dec 2, 2024
1 parent 376c037 commit c60e3c0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
5 changes: 4 additions & 1 deletion src/EventLogExpert.UI/Store/EventLog/EventLogEffects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ await Parallel.ForEachAsync(
return;
}

dispatcher.Dispatch(new EventLogAction.LoadEvents(logData, events.ToList().AsReadOnly()));
dispatcher.Dispatch(
new EventLogAction.LoadEvents(
logData,
events.OrderByDescending(e => e.RecordId).ToList().AsReadOnly()));

dispatcher.Dispatch(new StatusBarAction.SetEventsLoading(activityId, 0, 0));

Expand Down
2 changes: 1 addition & 1 deletion src/EventLogExpert/Components/EventTable.razor.css
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ tr {

.level { width: 100px; }

.dateandtime { width: 150px; }
.dateandtime { width: 160px; }

.activityid { width: 270px; }

Expand Down
47 changes: 25 additions & 22 deletions src/EventLogExpert/Components/FilterPane.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,29 @@ private void AddDateFilter()
{
_model.TimeZoneInfo = SettingsState.Value.Config.TimeZoneInfo;

// Round up/down to the nearest hour
var hourTicks = TimeSpan.FromHours(1).Ticks;

_model.Before = new DateTime(hourTicks * ((EventLogState.Value.ActiveLogs.Values
.Where(log => log.Events.Count > 0)
.Select(log => log.Events.First().TimeCreated)
.OrderBy(t => t)
.DefaultIfEmpty(DateTime.UtcNow)
.Last()
.Ticks + hourTicks) / hourTicks))
.ConvertTimeZone(_model.TimeZoneInfo);

_model.After = new DateTime(hourTicks * (EventLogState.Value.ActiveLogs.Values
.Where(log => log.Events.Count > 0)
.Select(log => log.Events.Last().TimeCreated)
.OrderBy(t => t)
.DefaultIfEmpty(DateTime.UtcNow)
.First()
.Ticks / hourTicks))
.ConvertTimeZone(_model.TimeZoneInfo);
long ticksPerHour = TimeSpan.FromHours(1).Ticks;

long oldestEventTicks =
(EventLogState.Value.ActiveLogs.Values.Select(log => log.Events.LastOrDefault()?.TimeCreated)
.Order()
.LastOrDefault() ??
DateTime.UtcNow)
.Ticks;

long mostRecentEventTicks =
(EventLogState.Value.ActiveLogs.Values.Select(log => log.Events.FirstOrDefault()?.TimeCreated)
.Order()
.FirstOrDefault() ??
DateTime.UtcNow)
.Ticks;

// Round down to the nearest hour for the earliest event
_model.After = new DateTime(oldestEventTicks / ticksPerHour * ticksPerHour, DateTimeKind.Utc)
.ConvertTimeZone(SettingsState.Value.Config.TimeZoneInfo);

// Round up to the nearest hour for the latest event
_model.Before = new DateTime(((mostRecentEventTicks + ticksPerHour - 1) / ticksPerHour) * ticksPerHour, DateTimeKind.Utc)
.ConvertTimeZone(SettingsState.Value.Config.TimeZoneInfo);

_isFilterListVisible = true;
_canEditDate = true;
Expand All @@ -115,8 +118,8 @@ private void ApplyDateFilter()
{
FilterDateModel model = new()
{
After = _model.After.ToUniversalTime(),
Before = _model.Before.ToUniversalTime()
After = _model.After.ConvertTimeZoneToUtc(SettingsState.Value.Config.TimeZoneInfo),
Before = _model.Before.ConvertTimeZoneToUtc(SettingsState.Value.Config.TimeZoneInfo)
};

Dispatcher.Dispatch(new FilterPaneAction.SetFilterDateRange(model));
Expand Down
3 changes: 3 additions & 0 deletions src/EventLogExpert/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ static async Task ForgetAwaited(Task task, ITraceLogger? logger = null)
internal static DateTime ConvertTimeZone(this DateTime time, TimeZoneInfo? destinationTime) =>
destinationTime is null ? time : TimeZoneInfo.ConvertTimeFromUtc(time, destinationTime);

internal static DateTime ConvertTimeZoneToUtc(this DateTime time, TimeZoneInfo? destinationTime) =>
destinationTime is null ? time : TimeZoneInfo.ConvertTimeToUtc(time, destinationTime);

internal static string GetEventKeywords(this IEnumerable<string> keywords)
{
StringBuilder sb = new("Keywords:");
Expand Down
1 change: 1 addition & 0 deletions src/EventLogExpert/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static MauiApp CreateMauiApp()

// Core Services
builder.Services.AddSingleton<ITraceLogger, DebugLogService>();
builder.Services.AddSingleton<IFileLogger, DebugLogService>();
builder.Services.AddSingleton<ILogWatcherService, LiveLogWatcherService>();

var fileLocationOptions = new FileLocationOptions(FileSystem.AppDataDirectory);
Expand Down

0 comments on commit c60e3c0

Please sign in to comment.