Skip to content

Commit bb8e898

Browse files
committed
fix dateonly
1 parent e5ec8dd commit bb8e898

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

samples/SampleWebApplication/Pages/Logs.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
asp-page="/Logs"
183183
asp-route-z="@Model.PageSize"
184184
asp-route-l="@Model.Level"
185-
asp-route-d="@Model.Date.Date"
185+
asp-route-d="@Model.Date"
186186
asp-route-t="">First</a>
187187
</li>
188188
}
@@ -199,7 +199,7 @@
199199
asp-page="/Logs"
200200
asp-route-z="@Model.PageSize"
201201
asp-route-l="@Model.Level"
202-
asp-route-d="@Model.Date.Date"
202+
asp-route-d="@Model.Date"
203203
asp-route-t="@Model.NextToken">Next</a>
204204
</li>
205205
}

samples/SampleWebApplication/Pages/Logs.cshtml.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using SampleWebApplication.Models;
77

88
using Serilog.Sinks.AzureTableStorage;
9-
using Serilog.Sinks.AzureTableStorage.Extensions;
109

1110
namespace SampleWebApplication.Pages;
1211

@@ -26,7 +25,7 @@ public LogsModel(TableServiceClient tableServiceClient)
2625
public string? Level { get; set; } = string.Empty;
2726

2827
[BindProperty(Name = "d", SupportsGet = true)]
29-
public DateTime Date { get; set; } = DateTime.Today;
28+
public DateOnly Date { get; set; } = DateOnly.FromDateTime(DateTime.Today);
3029

3130
[BindProperty(Name = "t", SupportsGet = true)]
3231
public string ContinuationToken { get; set; } = string.Empty;
@@ -39,7 +38,7 @@ public async Task<IActionResult> OnGetAsync(CancellationToken cancellationToken)
3938
{
4039
var logTable = _tableServiceClient.GetTableClient("SampleLog");
4140

42-
var filter = DefaultKeyGenerator.GeneratePartitionKeyQuery(Date.Date);
41+
var filter = DefaultKeyGenerator.GeneratePartitionKeyQuery(Date);
4342

4443
if (!string.IsNullOrWhiteSpace(Level))
4544
filter += $" and ({nameof(LogEventModel.Level)} eq '{Level}')";
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
3+
namespace Serilog.Sinks.AzureTableStorage.Extensions;
4+
5+
#if NET6_0_OR_GREATER
6+
/// <summary>
7+
/// Extension methods for <see cref="DateOnly"/>
8+
/// </summary>
9+
public static class DateOnlyExtensions
10+
{
11+
/// <summary>
12+
/// Converts the <see cref="DateOnly"/> to a <see cref="DateTimeOffset"/> in the specified timezone.
13+
/// </summary>
14+
/// <param name="dateOnly">The date to convert.</param>
15+
/// <param name="zone">The time zone the date is in.</param>
16+
/// <returns>The converted <see cref="DateTimeOffset"/></returns>
17+
public static DateTimeOffset ToDateTimeOffset(this DateOnly dateOnly, TimeZoneInfo zone = null)
18+
{
19+
zone ??= TimeZoneInfo.Local;
20+
21+
var dateTime = dateOnly.ToDateTime(TimeOnly.MinValue);
22+
var offset = zone.GetUtcOffset(dateTime);
23+
24+
return new DateTimeOffset(dateTime, offset);
25+
}
26+
27+
/// <summary>
28+
/// Converts the <see cref="DateTimeOffset"/> to a <see cref="DateOnly"/> in the specified timezone.
29+
/// </summary>
30+
/// <param name="dateTime">The <see cref="DateTimeOffset"/> to convert.</param>
31+
/// <param name="zone">The time zone the date is in.</param>
32+
/// <returns>The converted <see cref="DateOnly"/></returns>
33+
public static DateOnly ToDateOnly(this DateTimeOffset dateTime, TimeZoneInfo zone = null)
34+
{
35+
zone ??= TimeZoneInfo.Local;
36+
37+
var targetZone = TimeZoneInfo.ConvertTime(dateTime, zone);
38+
return DateOnly.FromDateTime(targetZone.Date);
39+
}
40+
}
41+
#endif

0 commit comments

Comments
 (0)