If I have ClusterTime returned from the Change Streams (via the .net driver and using C#), I can get BsonTimestamp values like:
Timestamp1 = 1587672311
Increment1 = 1
BsonType1 = Timestamp
Value1 = 6819000652509741057
===> DateTime = 4/23/2020 8:05:11 PM (no milliseconds, only seconds granulariyt)
Timestamp2 = 1587672311
Increment2 = 3
BsonType2 = Timestamp
Value2 = 6819000652509741059
===> DateTime = 4/23/2020 8:05:11 PM (no milliseconds, only seconds granularity)
I can make a DateTime from the BsonTimestamp by doing:
/// <summary>
/// GetDateTimeFromBsonTimestamp
/// Conversion from a BsonTimestamp into DateTime
/// </summary>
/// <param name="timestamp">input BsonTimestamp timestamp</param>
/// <returns>compatible DateTime result</returns>
public static DateTime GetDateTimeFromBsonTimestamp(BsonTimestamp timestamp)
{
var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return unixEpoch.AddSeconds(timestamp.Timestamp);
}
What I’m struggling with is that this is only second granularity when these two ClusterTime’s (BsonTimestamps) have Value data that shows much more granularity. Can I translate that granularity in my DateTime values? How can BsonTimestamp.Value be used to offer the greatest time granularity in a conversion to DateTime?
Please let me know if you have pointers - thanks!
Jeremy
PS - @wan - if you have a pointer here, I’m all ears! Thanks!