Skip to content

Commit

Permalink
Extend the BsonWriter to write TimeSpan as Ticks (which is its minima…
Browse files Browse the repository at this point in the history
…l unit).
  • Loading branch information
lanwin committed May 21, 2010
1 parent 1f53b0e commit 9f226e1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 10 additions & 0 deletions source/MongoDB.Tests/UnitTests/Bson/TestBsonWriter.cs
Expand Up @@ -200,5 +200,15 @@ public void TestWriteBytesAsBinary()

Assert.AreEqual("FwAAAAVieXRlcwAGAAAAAgIAAAAKDAA=",bson);
}

[Test]
public void TestWriteTimeSpanAsTicks()
{
var span = TimeSpan.FromSeconds(123456);

var bson = Serialize(new Document("span", span));

Assert.AreEqual("EwAAABJzcGFuAACggnEfAQAAAA==",bson);
}
}
}
7 changes: 6 additions & 1 deletion source/MongoDB/Bson/BsonWriter.cs
Expand Up @@ -68,7 +68,10 @@ public void WriteValue(BsonType type, Object obj)
_writer.Write((int)obj);
return;
case BsonType.Long:
_writer.Write((long)obj);
if(obj is TimeSpan)
_writer.Write(((TimeSpan)obj).Ticks);
else
_writer.Write((long)obj);
return;
case BsonType.Date:
Write((DateTime)obj);
Expand Down Expand Up @@ -579,6 +582,8 @@ protected BsonType TranslateToBsonType(object obj)
return BsonType.Oid;
if(type == typeof(DateTime))
return BsonType.Date;
if(type == typeof(TimeSpan))
return BsonType.Long;
if(type == typeof(MongoRegex))
return BsonType.Regex;
if(type == typeof(DBRef))
Expand Down

0 comments on commit 9f226e1

Please sign in to comment.