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

create DateTimeExtensions #771

Merged
merged 1 commit into from
Oct 6, 2024
Merged

create DateTimeExtensions #771

merged 1 commit into from
Oct 6, 2024

Conversation

neozhu
Copy link
Owner

@neozhu neozhu commented Oct 6, 2024

before

public LoggerAdvancedSpecification(LogsWithPaginationQuery filter)
{
    var timezoneOffset = filter.LocalTimezoneOffset;
    var utcNow = DateTime.UtcNow;
    // Corrected: Add the time zone offset to UTC time to get local time
    var localNow = utcNow.AddHours(timezoneOffset);

    // Calculate the start and end of today in local time
    var startOfTodayLocal = localNow.Date;
    var endOfTodayLocal = startOfTodayLocal.AddDays(1);
    var startOfLast30DaysLocal = startOfTodayLocal.AddDays(-30);

    // Convert local times back to UTC to match the TimeStamp's time zone
    var startOfTodayLocalAsUtc = startOfTodayLocal.AddHours(-timezoneOffset);
    var endOfTodayLocalAsUtc = endOfTodayLocal.AddHours(-timezoneOffset);
    var startOfLast30DaysLocalAsUtc = startOfLast30DaysLocal.AddHours(-timezoneOffset);

    // Build query conditions
    Query.Where(p => p.TimeStamp >= startOfTodayLocalAsUtc && p.TimeStamp < endOfTodayLocalAsUtc,
            filter.ListView == LogListView.CreatedToday)
        .Where(p => p.TimeStamp >= startOfLast30DaysLocalAsUtc, filter.ListView == LogListView.Last30days)
        .Where(p => p.Level == filter.Level.ToString(), filter.Level is not null)
        .Where(x =>x.Message.Contains(filter.Keyword),!string.IsNullOrEmpty(filter.Keyword));
}

after

public LoggerAdvancedSpecification(LogsWithPaginationQuery filter)
{
    DateTime today = DateTime.UtcNow;
    var todayrange = today.GetDateRange("TODAY", filter.LocalTimezoneOffset);
    var last30daysrange = today.GetDateRange("LAST_30_DAYS", filter.LocalTimezoneOffset);
    // Build query conditions
    Query.Where(p => p.TimeStamp >= todayrange.Start && p.TimeStamp < todayrange.End.AddDays(1),
            filter.ListView == LogListView.TODAY)
        .Where(p => p.TimeStamp >= last30daysrange.Start, filter.ListView == LogListView.LAST_30_DAYS)
        .Where(p => p.Level == filter.Level.ToString(), filter.Level is not null)
        .Where(x =>x.Message.Contains(filter.Keyword),!string.IsNullOrEmpty(filter.Keyword));
}

@neozhu neozhu merged commit 8d32282 into main Oct 6, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant