在 ABP 中覆盖 Clock.Now 时间

Overriding Clock.Now time in ABP

如何覆盖 Abp.Timing.Clock 的 DateTime 值?这些项目都使用 NuGet 包。我无法更改或编辑 NuGet 并发布我自己的 nuget.dll,因为那样的话整个项目都会出现“错误,缺少程序集引用”。那么我究竟如何在 UTC 和 Local 之间来回切换?

official link 表示您可以在整个项目的全局范围内更改所有 Clock.Now。但永远不要在挑选的基础上指定如何改变。

This is generally done at the beginning of an application (do it in the Application_Start of a web application).

以及:

ABP automatically normalizes DateTimes received from the database based on the current clock provider, when EntityFramework or NHibernate modules are used.

但是如果我想从数据库中提取一个存储为 UTC 而其他值是 Local 的值呢?反之亦然?

就像聊天演示使用 Clock.Now 但它没有解释任何机制。如果一条消息在我当地的早上 7 点进入聊天室,那么对于世界另一端阅读它的人来说,它会显示为早上 7 点吗?

Creators say:

...if you want to support users with multiple timezones, you need to store a timezone setting for users. So, you can use this setting.

但整个对话都是关于 JavaScript,而没有关于使用 UTC/Local 值存储和读取数据库。

我是否最好只删除 Clock.Now 的所有实例并改用 DateTime.Now/UTC?使用 Clock.Now 的 NuGet 程序集的其余部分呢?

Clock is the main class used to work with DateTime values. It defines the following static properties and methods...

So, instead of using DateTime.Now, we use Clock.Now, which abstracts it...

真的想要更多关于此的资源和信息。这两段文字并不足以支持我的担忧。我需要更多关于 EntityFramework/Database 的信息,而不是 JavaScript.


编辑以回应 。我不想尝试将其塞入评论文本。

public ChatMessage(
        UserIdentifier user,
        UserIdentifier targetUser,
        ChatSide side,
        string message,
        ChatMessageReadState readState,
        Guid sharedMessageId,
        ChatMessageReadState receiverReadState)
    {
        UserId = user.UserId;
        TenantId = user.TenantId;
        TargetUserId = targetUser.UserId;
        TargetTenantId = targetUser.TenantId;
        Message = message;
        Side = side;
        ReadState = readState;
        SharedMessageId = sharedMessageId;
        ReceiverReadState = receiverReadState;

        CreationTime = Clock.Now;
    }

如果此 Clock.Now 请求是 UTC 或本地,我将修改此条目中的何处?不影响项目其他地方的任何代码。 Clock.NowClock.Provider一样是静态方法。我前面提到的选择是区分遵循不同规则或模式的数据库条目的多个实例。你能帮忙澄清一下这个困惑吗?谢谢。

也许对于我的 Chat 我希望所有 DateTime 值都是本地的,但是对于我的登录系统我希望所有 DateTime 值都是 UTC。我将如何将一个静态 Clock.Now 与另一个分开?时钟系统在代码中处处相同。但是,如果我希望它在某些地方有所不同怎么办?比如,如果我想要服务器的本地时间,而不是用户的时间怎么办?如果我想要用户的本地时间,然后给他们一个另一个时区的日期时间怎么办?如果我想记录本地时间,但知道我记录的是哪个本地时间怎么办?我如何为 ONE 值而不是所有值执行此操作?

How do you override the DateTime value of Abp.Timing.Clock? ... So how exactly do i go back and forth between UTC and Local?

The official link says that you can alter ALL the Clock.Now on a global scale across the entire project. But never specify how to alter on a pick-and-choose basis.

But what about for situations where i want to pull a value from the database that is stored as UTC while other values are Local and vice versa?

Clock.Provider = ClockProviders.Utc;
// Clock.Now is UTC
// DateTime is UTC if unspecified

Clock.Provider = ClockProviders.Local;
// Clock.Now is Local
// DateTime is Local if unspecified

Like the chat demo uses Clock.Now but it doesn't explain any of the mechanics. If a message goes in the chat at my local 7am, will it show up as 7am for the person reading it on the other side of the world?

是,如果你使用 ClockProviders.Local

Am i better off just removing all instances of Clock.Now and use DateTime.Now/UTC instead? What about the rest of the nuget assembly that is using Clock.Now?

没有

回复编辑

Where in this entry would i modify if this Clock.Now request is UTC or Local? Without affecting any of the code elsewhere in the project. The pick-and-choose i mentioned earlier, was to distinguish between multiple instances of database entries following different rules or patterns. Can you please help to clarify that confusion?

Maybe for my Chat i want all DateTime values to be Local, but for my login system i want all DateTime values to be UTC. How would i go about separating one static Clock.Now from another? The clock system is the same everywhere in the code. But what if i want it to be different at certain places?

直接使用ClockProviders.*.Now

CreationTime = ClockProviders.Local.Now;

Clock.Now 是为了应用程序范围内的一致性。

Like, what if i wanted the local time of the server, and not the time of the user? What if i wanted local time of User, but then give them a DateTime for another timezone? What if i wanted to record Local time, but know which local i record?

ClockProviders.Local.Now 是服务器本地时间。如果你想要用户的时间或者如果你想支持具有多个时区的用户,你需要为用户存储一个时区设置。所以,你可以使用这个设置。

How do i do that for ONE value, instead of all?

直接使用ClockProviders.*.Now