Skip to content

Commit 9ef4586

Browse files
committed
Added micro and nanosecond support
1 parent e184088 commit 9ef4586

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/MongoDB.Bson/Serialization/Serializers/TimeOnlySerializer.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,9 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati
178178
bsonWriter.WriteInt32("Minute", value.Minute);
179179
bsonWriter.WriteInt32("Second", value.Second);
180180
bsonWriter.WriteInt32("Millisecond", value.Millisecond);
181-
#if NET7_0_OR_GREATER
182-
bsonWriter.WriteInt32("Microsecond", value.Microsecond);
183-
bsonWriter.WriteInt32("Nanosecond", value.Nanosecond);
184-
#endif
181+
// Microsecond and Nanosecond properties were added in .NET 7
182+
bsonWriter.WriteInt32("Microsecond", GetMicrosecondsComponent(value.Ticks));
183+
bsonWriter.WriteInt32("Nanosecond", GetNanosecondsComponent(value.Ticks));
185184
bsonWriter.WriteInt64("Ticks", value.Ticks);
186185
bsonWriter.WriteEndDocument();
187186
break;
@@ -295,6 +294,18 @@ private long ToInt64(TimeOnly timeOnly, TimeOnlyUnits units)
295294
: timeOnly.Ticks / TicksPerUnit(units);
296295
}
297296

297+
private int GetNanosecondsComponent(long ticks)
298+
{
299+
// ticks % 10 * 100
300+
return (int)(ticks % TicksPerUnit(TimeOnlyUnits.Microseconds) * 100);
301+
}
302+
303+
private int GetMicrosecondsComponent(long ticks)
304+
{
305+
// ticks / 10 % 1000
306+
return (int)(ticks / TicksPerUnit(TimeOnlyUnits.Microseconds) % 1000);
307+
}
308+
298309
// explicit interface implementations
299310
IBsonSerializer IRepresentationConfigurable.WithRepresentation(BsonType representation)
300311
{

tests/MongoDB.Bson.Tests/Serialization/Serializers/TimeOnlySerializerTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void Attribute_should_set_correct_units()
5353
""";
5454

5555
var documentString = """
56-
{ "Hour" : 13, "Minute" : 24, "Second" : 53, "Millisecond" : 0, "Ticks" : 482930000000 }
56+
{ "Hour" : 13, "Minute" : 24, "Second" : 53, "Millisecond" : 0, "Microsecond" : 0, "Nanosecond" : 0, "Ticks" : 482930000000 }
5757
""";
5858

5959

@@ -325,9 +325,9 @@ public void GetHashCode_should_return_zero()
325325
}
326326

327327
[Theory]
328-
[InlineData("08:32:05.5946583", """{ "x" : { "Hour" : { "$numberInt" : "8" }, "Minute" : { "$numberInt" : "32" }, "Second" : { "$numberInt" : "5" }, "Millisecond" : { "$numberInt" : "594" }, "Ticks" : { "$numberLong" : "307255946583" } } }""")]
329-
[InlineData("00:00:00.0000000", """{ "x" : { "Hour" : { "$numberInt" : "0" }, "Minute" : { "$numberInt" : "0" }, "Second" : { "$numberInt" : "0" }, "Millisecond" : { "$numberInt" : "0" }, "Ticks" : { "$numberLong" : "0" } } }""")]
330-
[InlineData("23:59:59.9999999", """{ "x" : { "Hour" : { "$numberInt" : "23" }, "Minute" : { "$numberInt" : "59" }, "Second" : { "$numberInt" : "59" }, "Millisecond" : { "$numberInt" : "999" }, "Ticks" : { "$numberLong" : "863999999999" } } }""")]
328+
[InlineData("08:32:05.5946583", """{ "x" : { "Hour" : { "$numberInt" : "8" }, "Minute" : { "$numberInt" : "32" }, "Second" : { "$numberInt" : "5" }, "Millisecond" : { "$numberInt" : "594" }, "Microsecond" : { "$numberInt" : "658" }, "Nanosecond" : { "$numberInt" : "300" }, "Ticks" : { "$numberLong" : "307255946583" } } }""")]
329+
[InlineData("00:00:00.0000000", """{ "x" : { "Hour" : { "$numberInt" : "0" }, "Minute" : { "$numberInt" : "0" }, "Second" : { "$numberInt" : "0" }, "Millisecond" : { "$numberInt" : "0" }, "Microsecond" : { "$numberInt" : "0" }, "Nanosecond" : { "$numberInt" : "0" }, "Ticks" : { "$numberLong" : "0" } } }""")]
330+
[InlineData("23:59:59.9999999", """{ "x" : { "Hour" : { "$numberInt" : "23" }, "Minute" : { "$numberInt" : "59" }, "Second" : { "$numberInt" : "59" }, "Millisecond" : { "$numberInt" : "999" }, "Microsecond" : { "$numberInt" : "999" }, "Nanosecond" : { "$numberInt" : "900" }, "Ticks" : { "$numberLong" : "863999999999" } } }""")]
331331
public void Serialize_with_document_representation_should_have_expected_result(string valueString, string expectedResult)
332332
{
333333
var subject = new TimeOnlySerializer(BsonType.Document);

0 commit comments

Comments
 (0)