2

How do you override the DateTime value of Abp.Timing.Clock? The projects are all using NuGet packages. I can't change or edit NuGet and release my own personal nuget.dll because then the whole project will give "Error, missing assembly reference." 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.

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

As well as:

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

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?

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?

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.

But the entire conversation was about JavaScript, and nothing about about storing and reading from database with UTC/Local values.

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?

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...

Really would like some more resources and information on this. The two paragraphs of text don't really do enough to sustain my concerns. I need more information on EntityFramework/Database, and less on JavaScript.


Edit in response to @aaron's answer. I did not want to try to cram this into a comment text.

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;
    }

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. Clock.Now is a static method, as well as Clock.Provider. 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? Thank you.

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? 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? How do i do that for ONE value, instead of all?

Community
  • 1
  • 1

1 Answers1

4

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?

Yes if you use 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?

No.

Responses to edit

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?

Use ClockProviders.*.Now directly.

CreationTime = ClockProviders.Local.Now;

Clock.Now is for application-wide consistency.

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 is local time of the server. If you want the time of the user or if you want to support users with multiple timezones, you need to store a timezone setting for users. So, you can use this setting.

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

Use ClockProviders.*.Now directly.

Community
  • 1
  • 1
aaron
  • 17,311
  • 4
  • 27
  • 63