简体   繁体   English

将MongoDB BsonTimestamp转换为C#DateTime

[英]Convert MongoDB BsonTimestamp to C# DateTime

What is the proper way to convert a BsonTimestamp field to a C# DateTime type? 将BsonTimestamp字段转换为C#DateTime类型的正确方法是什么?

This is for data in MongoDB's oplog collection and using the MongoDB C# driver. 这用于MongoDB的oplog集合中的数据,并使用MongoDB C#驱动程序。

I believe the accepted answer in slightly off as Unix Epoch must be in UTC format. 我相信,由于Unix Epoch必须采用UTC格式,因此可接受的答案略有不同。

var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

From BsonTimestamp to DateTime 从BsonTimestamp到DateTime

var target = unixEpoch.AddSeconds(bsonTimestamp.Timestamp - 18000);

From DateTime to BsonTimestamp 从DateTime到BsonTimestamp

var target = DateTime.UtcNow;
var diff = target.ToUniversalTime() - unixEpoch;
var seconds = (diff.TotalMilliseconds + 18000000) / 1000;
var ts = new BsonTimestamp((int)seconds, 1);

You need to use target.ToUniversalTime() to ensure the incoming parameter, if any, is always positioned for UTC. 您需要使用target.ToUniversalTime()来确保传入参数(如果有)始终位于UTC位置。

MongoDB's Timestamp is the elapsed seconds since the Unix epoch (1970/1/1). MongoDB的Timestamp是自Unix时代(1970/1/1)起经过的秒数。 Therefore, the conversion from Timestamp to DateTime is like this: 因此,从TimestampDateTime的转换是这样的:

DateTime datetime = new DateTime(1970, 1, 1).AddSeconds(bsonTimestamp.Timestamp);

In terms of Value / Timestamp properties, they are implmented in both constructors of BsonTimestamp in https://github.com/mongodb/mongo-csharp-driver . Value / Timestamp属性而言,它们都在https://github.com/mongodb/mongo-csharp-driver中BsonTimestamp两个构造函数中BsonTimestamp

Constructor 1: 构造函数1:

public BsonTimestamp(long value)
{
    _value = value;
}

Constructor 2: 构造函数2:

public BsonTimestamp(int timestamp, int increment)
{
    _value = (long)(((ulong)(uint)timestamp << 32) | (ulong)(uint)increment);
}

Property: 属性:

public long Value
{
    get { return _value; }
}

public int Timestamp
{
    get { return (int)(_value >> 32); }
}

Since you are getting the timestamp records from oplog, their format would be like this: 由于您是从oplog获取时间戳记录,因此其格式如下:

Timestamp(1406171938, 1) 

AS the second number ( increment ) is an ordinal number to make the Timestamp unique according to MongoDB reference , you should use Timestamp property I think. 由于第二个数字( increment )是一个序号,根据MongoDB参考 ,它使Timestamp唯一,因此您应该使用我认为的Timestamp属性。

If you are using a BSON document: 如果使用的是BSON文档:

DateTime dateTime = doc["BSONdateTime"].AsDateTime;

where "dateTime" is the variable you want to set, "doc" is the BSON document you extracted from MongoDB, and "BSONdateTime" is the key that you want to extract the date and time from. 其中“ dateTime”是要设置的变量,“ doc”是您从MongoDB中提取的BSON文档,“ BSONdateTime”是您要从中提取日期和时间的键。

I haven't tried this myself, but I was able to extract string values from BSON documents in MongoDB using: 我自己没有尝试过,但是我能够使用以下方法从MongoDB中的BSON文档中提取字符串值:

string name = doc["name"].AsString;

I would also recommend you look into POCO, as this makes type conversion a lot easier and less boilerplate. 我还建议您研究POCO,因为这使类型转换变得更加容易并且减少了样板。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM