在Mongo中存储Utc和本地日期时间 [英] Storing Utc and Local datetime in Mongo

查看:1473
本文介绍了在Mongo中存储Utc和本地日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Mongo C#实现,将datetime存储为UTC。

I have a Mongo C# implementation that stores datetime as UTC.

MongoDB.Bson.Serialization.Options.DateTimeSerializationOptions options = 
    MongoDB.Bson.Serialization.Options.DateTimeSerializationOptions.UtcInstance;

var serializer = 
    new MongoDB.Bson.Serialization.Serializers.DateTimeSerializer(options);

MongoDB.Bson.Serialization.BsonSerializer.RegisterSerializer(
    typeof(DateTime),
    serializer);

我还需要存储用户本地时区以及UTC。
要解释,我有两个属性,如

I also have a need to store the user local timezone along with the UTC. To explain, I have two properties that goes like

DateTime WorkItemToCompleteBy{get; set;}
[BsonDateTimeOptions(Kind = DateTimeKind.Unspecified)]
DateTime WorkItemToCompleteByLocal{get; set;}

我想将澳大利亚/美国/印度/其他时间存储在本地财产和另一个UTC的相应UTC值。由于我处理了几十个时区,因此我有代码将UTC转换为所需的时区,并将其存储在WorkItemToCompleteByLocal属性中。
我希望Mongo按原样存储这个值,并将其返回给我。问题是Mongo总是将它存储为ISODate,并将其转换为Utc版本。
解释。
如果UTC是0730小时,我将布里斯班时间计算到1730小时,并将其设置为WorkitemToCompleteByLocal,
,它们将被保存为

I'd like to store Australian/American/Indian/Other times in the Local property and the respective UTC value in the other one. Since am dealing with dozens of time zones, I have code that converts the UTC to the desired timezone and stores it in the WorkItemToCompleteByLocal property. I'd like Mongo to store this value 'as-is' and return it to me. The problem is that Mongo always stores it as ISODate and converts the value to Utc version. To explain. If UTC is 0730 Hours and I compute Brisbane Time to 1730Hours and set it to WorkitemToCompleteByLocal, they get saved as

"WorkItemToCompleteBy" : ISODate("2013-06-05T07:30:00Z"),
"WorkItemToCompleteByLocal" : ISODate("2013-06-05T12:00:00Z"),

Mongo解释当地提供的时间,服务器在印度,并将其覆盖到等效UTC为1200小时。虽然它检索值为1730(IST Albeit)它违反了我的目的,阻止我在Mongo上运行任何本地基于时间的查询。我出于想法任何帮助不胜感激,帮助存储WorkItemToCompleteByLocal日期As-Is

Mongo interprets the time provided as local, the server being in India and coverts it to the equivalent UTC of 1200 hours. While it retrieves values back as 1730 (IST Albeit) It defeats my purpose and prevents me from running any local time based queries on Mongo. Am out of ideas. Any help is appreciated to help store the WorkItemToCompleteByLocal date 'As-Is' without modification

推荐答案

新版本的C#driver =>

New version of C# driver =>

    public class MyMongoDBDateTimeSerializer : DateTimeSerializer
{
    //  MongoDB returns datetime as DateTimeKind.Utc, which cann't be used in our timezone conversion logic
    //  We overwrite it to be DateTimeKind.Unspecified
    public override DateTime Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
    {
        var obj = base.Deserialize(context, args);
        return new DateTime(obj.Ticks, DateTimeKind.Unspecified);
    }

    //  MongoDB stores all datetime as Utc, any datetime value DateTimeKind is not DateTimeKind.Utc, will be converted to Utc first
    //  We overwrite it to be DateTimeKind.Utc, becasue we want to preserve the raw value
    public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, DateTime value)
    {
        var utcValue = new DateTime(value.Ticks, DateTimeKind.Utc);
        base.Serialize(context, args, utcValue);
    }
}

这篇关于在Mongo中存储Utc和本地日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆