如何关闭MongoDB的UTC时区 [英] How can I turn UTC Timezone off for MongoDB

查看:340
本文介绍了如何关闭MongoDB的UTC时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 MongoDB,我在MongoDB日期字段中使用了以下扩展名

I'm using the extension below for MongoDB date fields because of MongoDB stores times in UTC by default.

    public static class DateTimeExtensions {
    
        public static DateTime AdjustUtcDiffByOffset(this DateTime time) {
            return time.AddHours(DateTimeOffset.Now.Offset.Hours);
        }
    }
}



问题,尽管我在应用程序级别尝试了几种方法,例如.NET中的属性或序列化方法。我已经决定暂时在.NET中使用此扩展名,但是我认为这也不是一个完整而准确的解决方案。

在数据库级别有没有解决此问题的解决方案,而无需依赖于调整语言或其他调整语言?

All of them cause different problems although I have tried a few ways like attributes or serialization methods in .NET on the application level. I have decided to use this extension in .NET for now but I think that this is not a complete and exact solution too.

Is there any solution for this problem on the database level without being dependent on programming language wtih an adjust or something else?

我认为我应该在下面的评论后再作解释。我已经知道MongoDB在UTC中存储时间,该时间在本文中链接,如上所示。这可能很有用,但我的应用程序不需要UTC时区差异,也不想为每种编程语言分别处理Presentation Layer。而且,由于与基础逻辑或业务相比,我不希望在其他层上仅增加一行或增加功能。

I think that I should an explain more after comments below. I already know MongoDB stores times in UTC that linked in this post as you see above. This can be useful but I don't need any UTC time zone difference in my app and I don't want to deal in Presentation Layer for every programming language separately. And also, I don't even want only one extra row or function in the other layers because of move away than base logic or business.

让我的体系结构来承担这一点。我很懒惰,寿命真的很短,鸟儿还在飞来飞去:)我不希望像字符串转换那样不必要地使用不同的字段。由于需要在应用程序中进行很多时间计算,因此我需要在数据库中使用datetime类型。

Let the my architecture undertaking this. I'm pretty lazy, the life is really short and the birds are flying outside :) I don't want the different fields as like as string convertings unnecessarily. I need a datetime type in the db due to I'm doing many time calculation in the app.

我现在正在使用.NET,因此 MongoDB .NET驱动程序。我尝试了不同的序列化方法,但是这在数据访问体系结构中引起了另一个问题。 最后,我可以在另一个应用程序中使用UTC,但现在我不想要它,我更喜欢分配给该字段的本地时间。我决定特别使用下面的C#封装。

I'm using .NET now and therefore MongoDB .NET driver. I have tried different serialization methods but it cause another problems in the Data Access architecture. In conclusion, I can use UTC in my another app but I don't want it now and I prefer the local time when I assign to the field. I have decided to use the encapsulation below for C# especially.

    private DateTime _startTime;
    public DateTime StartTime {
        get => _startTime;
        set => _startTime = new DateTime(value.Ticks, DateTimeKind.Utc);
    }


推荐答案

我不认为这是从数据库级别可能。我要做的是为日期属性编写自定义设置器,这将迫使mongoDB假定时间已经在UTC中,从而避免如下所示的转换:

I don't think it's possible from the db level. What I did, was to write custom setter for date properties which will force mongoDB to assume the time is already in UTC, thus avoid a conversion like below:

private DateTime _createdUTC;
public DateTime CreatedUtc
  {
     get
       {
         return _createdUTC;
       }
     set
       {
         _createdUTC = new DateTime(value.Ticks, DateTimeKind.Utc);
       }
  }

这篇关于如何关闭MongoDB的UTC时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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