Skip to content

Commit

Permalink
more date work
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Apr 26, 2024
1 parent 170c819 commit 9d3d7ae
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 25 deletions.
39 changes: 39 additions & 0 deletions src/TableStorage.Abstracts/Extensions/DateOnlyExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace TableStorage.Abstracts.Extensions;

#if NET6_0_OR_GREATER
/// <summary>
/// <see cref="DateOnly" /> extension methods
/// </summary>
public static class DateOnlyExtensions
{
/// <summary>
/// Converts the <see cref="DateOnly"/> to a <see cref="DateTimeOffset"/> in the specified timezone.
/// </summary>
/// <param name="dateOnly">The date to convert.</param>
/// <param name="zone">The time zone the date is in.</param>
/// <returns>The converted <see cref="DateTimeOffset"/></returns>
public static DateTimeOffset ToDateTimeOffset(this DateOnly dateOnly, TimeZoneInfo? zone = null)
{
zone ??= TimeZoneInfo.Local;

var dateTime = dateOnly.ToDateTime(TimeOnly.MinValue);
var offset = zone.GetUtcOffset(dateTime);

return new DateTimeOffset(dateTime, offset);
}

/// <summary>
/// Converts the <see cref="DateTimeOffset"/> to a <see cref="DateOnly"/> in the specified timezone.
/// </summary>
/// <param name="dateTime">The <see cref="DateTimeOffset"/> to convert.</param>
/// <param name="zone">The time zone the date is in.</param>
/// <returns>The converted <see cref="DateOnly"/></returns>
public static DateOnly ToDateOnly(this DateTimeOffset dateTime, TimeZoneInfo? zone = null)
{
zone ??= TimeZoneInfo.Local;

var targetZone = TimeZoneInfo.ConvertTime(dateTime, zone);
return DateOnly.FromDateTime(targetZone.Date);
}
}
#endif
45 changes: 25 additions & 20 deletions src/TableStorage.Abstracts/KeyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ public static string GenerateRowKey(DateTime eventTime)
public static string GeneratePartitionKeyQuery(DateOnly date, TimeSpan offset)
{
// date is assumed to be in local time, will be converted to UTC
var eventTime = new DateTimeOffset(date.Year, date.Month, date.Day, 0, 0, 0, offset);
return GeneratePartitionKeyQuery(eventTime);
var startTime = new DateTimeOffset(date.Year, date.Month, date.Day, 0, 0, 0, offset);
var endTime = startTime.AddDays(1);

return GeneratePartitionKeyQuery(startTime, endTime);
}

/// <summary>
Expand All @@ -109,40 +111,43 @@ public static string GeneratePartitionKeyQuery(DateOnly date, TimeSpan offset)
public static string GeneratePartitionKeyQuery(DateOnly date, TimeZoneInfo? zone = null)
{
// date is assumed to be in local time, will be converted to UTC
zone ??= TimeZoneInfo.Local;

var dateTime = date.ToDateTime(TimeOnly.MinValue);
var offset = zone.GetUtcOffset(dateTime);
var startTime = date.ToDateTimeOffset(zone);
var endTime = date.AddDays(1).ToDateTimeOffset(zone);

var eventTime = new DateTimeOffset(dateTime, offset);
return GeneratePartitionKeyQuery(eventTime);
return GeneratePartitionKeyQuery(startTime, endTime);
}
#endif

/// <summary>
/// Generates the partition key query using the specified <paramref name="eventTime"/>.
/// Generates the partition key query using the specified <paramref name="startDate"/> and <paramref name="endDate"/>.
/// </summary>
/// <param name="eventTime">The date to use for query.</param>
/// <param name="startDate">The start date to use for query.</param>
/// <param name="endDate">The end date to use for query.</param>
/// <returns>An Azure Table partiion key query.</returns>
public static string GeneratePartitionKeyQuery(DateTime eventTime)
public static string GeneratePartitionKeyQuery(DateTime startDate, DateTime endDate)
{
var dateTime = eventTime.ToUniversalTime();
var dateTimeOffset = new DateTimeOffset(dateTime, TimeSpan.Zero);
var startTime = startDate.ToUniversalTime();
var startTimeOffset = new DateTimeOffset(startTime, TimeSpan.Zero);

var endTime = endDate.ToUniversalTime();
var endTimeOffset = new DateTimeOffset(endTime, TimeSpan.Zero);

return GeneratePartitionKeyQuery(dateTimeOffset);
return GeneratePartitionKeyQuery(startTimeOffset, endTimeOffset);
}

/// <summary>
/// Generates the partition key query using the specified <paramref name="eventTime"/>.
/// Generates the partition key query using the specified <paramref name="startDate"/> and <paramref name="endDate"/>.
/// </summary>
/// <param name="eventTime">The date to use for query.</param>
/// <param name="startDate">The start date to use for query.</param>
/// <param name="endDate">The end date to use for query.</param>
/// <returns>An Azure Table partiion key query.</returns>
public static string GeneratePartitionKeyQuery(DateTimeOffset eventTime)
public static string GeneratePartitionKeyQuery(DateTimeOffset startDate, DateTimeOffset endDate)
{
var dateTime = eventTime.ToUniversalTime();
var startTime = startDate.ToUniversalTime();
var endTime = endDate.ToUniversalTime();

var upper = dateTime.ToReverseChronological().Ticks.ToString("D19");
var lower = dateTime.AddDays(1).ToReverseChronological().Ticks.ToString("D19");
var upper = startTime.ToReverseChronological().Ticks.ToString("D19");
var lower = endTime.ToReverseChronological().Ticks.ToString("D19");

return $"({PartitionKeyName} ge '{lower}') and ({PartitionKeyName} lt '{upper}')";
}
Expand Down
15 changes: 10 additions & 5 deletions test/TableStorage.Abstracts.Tests/KeyGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,26 @@ public void GeneratePartitionKeyQueryDateOnly()
[Fact]
public void GeneratePartitionKeyQueryDateTime()
{
var dateTime = new DateTimeOffset(2024, 4, 1, 0, 0, 0, TimeSpan.FromHours(-5));
var eventTime = dateTime.UtcDateTime;
var startDate = new DateTimeOffset(2024, 4, 1, 0, 0, 0, TimeSpan.FromHours(-5));
var startTime = startDate.DateTime;

var endDate = new DateTimeOffset(2024, 4, 2, 0, 0, 0, TimeSpan.FromHours(-5));
var endTime = endDate.DateTime;

var partitionKeyQuery = KeyGenerator.GeneratePartitionKeyQuery(eventTime);
var partitionKeyQuery = KeyGenerator.GeneratePartitionKeyQuery(startTime, endTime);
partitionKeyQuery.Should().NotBeNull();
partitionKeyQuery.Should().Be("(PartitionKey ge '2516902667999999999') and (PartitionKey lt '2516903531999999999')");
}

[Fact]
public void GeneratePartitionKeyQueryDateTimeOffset()
{
var dateTime = new DateTimeOffset(2024, 4, 1, 0, 0, 0, TimeSpan.FromHours(-5));
var startTime = new DateTimeOffset(2024, 4, 1, 0, 0, 0, TimeSpan.FromHours(-5));
var endTime = new DateTimeOffset(2024, 4, 2, 0, 0, 0, TimeSpan.FromHours(-5));

var partitionKeyQuery = KeyGenerator.GeneratePartitionKeyQuery(dateTime);
var partitionKeyQuery = KeyGenerator.GeneratePartitionKeyQuery(startTime, endTime);
partitionKeyQuery.Should().NotBeNull();
partitionKeyQuery.Should().Be("(PartitionKey ge '2516902667999999999') and (PartitionKey lt '2516903531999999999')");
}

}

0 comments on commit 9d3d7ae

Please sign in to comment.