Skip to content

Commit 46bf573

Browse files
committed
Preserve DateTime.Kind in ContinuationToken serialization
1 parent af3c4fb commit 46bf573

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/Arbiter.Services/ContinuationToken.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public static (T1, T2) Parse<T1, T2>(string token)
204204
}
205205

206206
/// <summary>
207-
/// Parses a continuation token and extracts three values.
207+
/// Parsing a continuation token and extracts three values.
208208
/// </summary>
209209
/// <typeparam name="T1">The type of the first value.</typeparam>
210210
/// <typeparam name="T2">The type of the second value.</typeparam>
@@ -235,7 +235,7 @@ private static int EstimateSize<T>(T value)
235235
{
236236
bool => 2,
237237
byte => 2,
238-
DateTime => 9,
238+
DateTime => 10,
239239
DateTimeOffset => 11,
240240
decimal => 17,
241241
double => 9,
@@ -396,16 +396,23 @@ private static int WriteDateTime(Span<byte> buffer, DateTime value)
396396
buffer[0] = (byte)TypeMarker.DateTime;
397397

398398
BinaryPrimitives.WriteInt64LittleEndian(buffer[1..], value.Ticks);
399+
400+
// Store DateTime.Kind to preserve it during round-trip
401+
// 0 = Unspecified, 1 = Utc, 2 = Local
402+
buffer[9] = (byte)value.Kind;
399403

400-
return 9;
404+
return 10;
401405
}
402406

403407
private static DateTime ReadDateTime(ReadOnlySpan<byte> buffer, ref int position)
404408
{
405409
var ticks = BinaryPrimitives.ReadInt64LittleEndian(buffer[position..]);
406410
position += 8;
411+
412+
// Read DateTime.Kind to restore the original kind
413+
var kind = (DateTimeKind)buffer[position++];
407414

408-
return new DateTime(ticks);
415+
return new DateTime(ticks, kind);
409416
}
410417

411418
private static int WriteDateTimeOffset(Span<byte> buffer, DateTimeOffset value)

test/Arbiter.Services.Tests/ContinuationTokenTests.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,37 @@ public void RoundTrip_WithDateTime_PreservesValue()
202202
}
203203
}
204204

205+
[Test]
206+
public void RoundTrip_WithDateTime_PreservesKind()
207+
{
208+
// Arrange
209+
var testValues = new[]
210+
{
211+
new DateTime(2024, 1, 15, 10, 30, 45, DateTimeKind.Utc),
212+
new DateTime(2024, 1, 15, 10, 30, 45, DateTimeKind.Local),
213+
new DateTime(2024, 1, 15, 10, 30, 45, DateTimeKind.Unspecified),
214+
DateTime.UtcNow,
215+
DateTime.Now,
216+
DateTime.MinValue,
217+
DateTime.MaxValue
218+
};
219+
220+
foreach (var value in testValues)
221+
{
222+
// Act
223+
var token = ContinuationToken.Create(value);
224+
var result = ContinuationToken.Parse<DateTime>(token);
225+
226+
// Assert
227+
result.Should().Be(value);
228+
result.Kind.Should().Be(value.Kind, $"DateTime.Kind should be preserved for value {value}");
229+
}
230+
}
231+
205232
[Test]
206233
public void RoundTrip_WithDateTimeOffset_PreservesValue()
207234
{
208-
// Arrange - Test values with UTC offset (zero) which round-trip correctly
235+
// Arrange
209236
var testValues = new[]
210237
{
211238
DateTimeOffset.MinValue,

0 commit comments

Comments
 (0)