From 48ea85efa69693d6d7430c58a47c5aece43f85f1 Mon Sep 17 00:00:00 2001
From: Steve Lorello <42971704+slorello89@users.noreply.github.com>
Date: Mon, 28 Oct 2024 07:55:35 -0400
Subject: [PATCH] fixing issue with timespans having fractional millisecond
values (#497)
* fixing issue with timespans having fractional values
* fixign test
* fixing test
* more test fixes
---
src/Redis.OM/Extensions/TimespanExtensions.cs | 20 ++++++++++++++
src/Redis.OM/RedisCommands.cs | 20 +++++++-------
src/Redis.OM/Searching/RedisCollection.cs | 12 ++++-----
test/Redis.OM.Unit.Tests/CoreTests.cs | 4 +--
.../RediSearchTests/SearchFunctionalTests.cs | 26 +++++++++++++++++++
5 files changed, 64 insertions(+), 18 deletions(-)
create mode 100644 src/Redis.OM/Extensions/TimespanExtensions.cs
diff --git a/src/Redis.OM/Extensions/TimespanExtensions.cs b/src/Redis.OM/Extensions/TimespanExtensions.cs
new file mode 100644
index 0000000..7c83225
--- /dev/null
+++ b/src/Redis.OM/Extensions/TimespanExtensions.cs
@@ -0,0 +1,20 @@
+using System;
+using System.Globalization;
+
+namespace Redis.OM;
+
+///
+/// Extension methods for Timespans.
+///
+internal static class TimespanExtensions
+{
+ ///
+ /// Rounds up total milliseconds as an integer.
+ ///
+ /// the timespan.
+ /// the rounded timespan milliseconds.
+ public static string TotalMillisecondsString(this TimeSpan ts)
+ {
+ return Math.Ceiling(ts.TotalMilliseconds).ToString(CultureInfo.InvariantCulture);
+ }
+}
\ No newline at end of file
diff --git a/src/Redis.OM/RedisCommands.cs b/src/Redis.OM/RedisCommands.cs
index 2f10a2f..098c283 100644
--- a/src/Redis.OM/RedisCommands.cs
+++ b/src/Redis.OM/RedisCommands.cs
@@ -187,7 +187,7 @@ public static async Task JsonSetAsync(this IRedisConnection connection, st
/// whether the operation succeeded.
public static async Task JsonSetAsync(this IRedisConnection connection, string key, string path, string json, WhenKey when, TimeSpan? timeSpan = null)
{
- var argList = new List { timeSpan != null ? ((long)timeSpan.Value.TotalMilliseconds).ToString() : "-1", path, json };
+ var argList = new List { timeSpan != null ? timeSpan.Value.TotalMillisecondsString() : "-1", path, json };
switch (when)
{
case WhenKey.Exists:
@@ -328,7 +328,7 @@ public static bool JsonSet(this IRedisConnection connection, string key, string
/// whether the operation succeeded.
public static bool JsonSet(this IRedisConnection connection, string key, string path, string json, WhenKey when, TimeSpan? timeSpan = null)
{
- var argList = new List { timeSpan != null ? ((long)timeSpan.Value.TotalMilliseconds).ToString() : "-1", path, json };
+ var argList = new List { timeSpan != null ? timeSpan.Value.TotalMillisecondsString() : "-1", path, json };
switch (when)
{
case WhenKey.Exists:
@@ -416,7 +416,7 @@ public static string Set(this IRedisConnection connection, object obj)
var kvps = obj.BuildHashSet();
var argsList = new List