Skip to content

Commit

Permalink
Merge pull request #94 from neozhu/timezoneoffsetrefactoring
Browse files Browse the repository at this point in the history
_localTimezoneOffset
  • Loading branch information
neozhu authored Oct 6, 2024
2 parents 93b09b5 + d75965c commit fe5a261
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Templates/Pages/.razor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
[CascadingParameter]
private UserProfile? UserProfile { get; set; }
[CascadingParameter(Name = "LocalTimezoneOffset")]
private int _localTimezoneOffset { get; set; }
private TimeSpan _localTimezoneOffset { get; set; }

private {nameofPlural}WithPaginationQuery Query { get; set; } = new();
[Inject]
Expand Down
2 changes: 1 addition & 1 deletion src/Templates/Queries/Pagination/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class {nameofPlural}WithPaginationQuery : {itemname}AdvancedFilter, ICach
{
public override string ToString()
{
return $"Listview:{ListView}-{LocalTimezoneOffset}, Search:{Keyword}, {OrderBy}, {SortDirection}, {PageNumber}, {PageSize}";
return $"Listview:{ListView}-{LocalTimezoneOffset.TotalHours}, Search:{Keyword}, {OrderBy}, {SortDirection}, {PageNumber}, {PageSize}";
}
public string CacheKey => {itemname}CacheKey.GetPaginationCacheKey($"{this}");
public MemoryCacheEntryOptions? Options => {itemname}CacheKey.MemoryCacheEntryOptions;
Expand Down
6 changes: 3 additions & 3 deletions src/Templates/Specifications/AdvancedFilter.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ public enum {itemname}ListView
[Description("My")]
My,
[Description("Created Toady")]
CreatedToday,
TODAY,
[Description("Created within the last 30 days")]
Created30Days
LAST_30_DAYS
}
/// <summary>
/// A class for applying advanced filtering options to {itemname} lists.
/// </summary>
public class {itemname}AdvancedFilter: PaginationFilter
{
public int LocalTimezoneOffset { get; set; }
public TimeSpan LocalTimezoneOffset { get; set; }
public {itemname}ListView ListView { get; set; } = {itemname}ListView.All;
public UserProfile? CurrentUser { get; set; }
}
23 changes: 6 additions & 17 deletions src/Templates/Specifications/AdvancedSpecification.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,15 @@ public class {itemname}AdvancedSpecification : Specification<{itemname}>
{
public {itemname}AdvancedSpecification({itemname}AdvancedFilter 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);
DateTime today = DateTime.UtcNow;
var todayrange = today.GetDateRange({itemname}ListView.TODAY.ToString(), filter.LocalTimezoneOffset);
var last30daysrange = today.GetDateRange({itemname}ListView.LAST_30_DAYS.ToString(),filter.LocalTimezoneOffset);

// 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);

Query.Where(q => q.Name != null)
Query.Where(q => q.Name != null)
.Where(filter.Keyword,!string.IsNullOrEmpty(filter.Keyword))
.Where(q => q.CreatedBy == filter.CurrentUser.UserId, filter.ListView == {itemname}ListView.My && filter.CurrentUser is not null)
.Where(q => q.Created >= startOfTodayLocalAsUtc && q.Created < endOfTodayLocalAsUtc, filter.ListView == {itemname}ListView.CreatedToday)
.Where(q => q.Created >= startOfLast30DaysLocalAsUtc, filter.ListView == {itemname}ListView.Created30Days);
.Where(x => x.Created >= todayrange.Start && x.Created < todayrange.End.AddDays(1), filter.ListView == {itemname}ListView.TODAY)
.Where(x => x.Created >= last30daysrange.Start, filter.ListView == {itemname}ListView.LAST_30_DAYS);

}
}

0 comments on commit fe5a261

Please sign in to comment.