doc/WebSite/Timing.md
While some applications target a single timezone, others target many different timezones. To satisfy such needs and centralize datetime operations, ABP provides a common infrastructure for datetime operations.
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:
DateTime now = Clock.Now;
Clock uses the clock providers inside it. There are three types of built-in clock providers:
You can set Clock.Provider in order to use a different clock provider:
Clock.Provider = ClockProviders.Utc;
This is generally done at the beginning of an application (do it in the Application_Start of a web application).
The clock can be used on the client-side using the abp.clock object in JavaScript. When you set Clock.Provider on the server-side, ABP automatically sets the value of abp.clock.provider on the client-side.
ABP defines a setting named Abp.Timing.TimeZone (TimingSettingNames.TimeZone constant) for storing the selected timezone of the host, tenant and user. ABP assumes that the value of a timezone setting is a valid Windows timezone name. It also defines a timezone mapping file to convert a Windows Timezone to an IANA timezone since some common libraries are using the IANA timezone id. UtcClockProvider must be used in order to support multiple timezones. Because if UtcClockProvider is used, all datetime values will be stored in UTC and all datetimes will be sent to clients in UTC format. Then on the client-side we can convert a UTC datetime to the user's timezone by using the user's current timezone setting.
ABP creates a JavaScript object named abp.timing.timeZoneInfo which contains timezone information for the current user. This information contains Windows and IANA timezone ids and some extra information for windows timezone info. This information can be used to make client-side datetime convertions by showing a datetime to the user in his/her timezone.
If the UTC clock provider is used, then all DateTimes stored in the database are assumed as UTC values, and all DateTimes received from clients are assumed as UTC values unless explicitly specified.
In some cases, you might want ABP not to normalize your DateTime values. DisableDateTimeNormalizationAttribute is designed for such cases. This attribute can be used on classes, properties of classes or parameters of Controller Actions. when DisableDateTimeNormalizationAttribute is used, ABP will not normalize DateTime values.
[DisableDateTimeNormalization]
public class Location : IHasCreationTime
{
public string Lat { get; set; }
public string Lng { get; set; }
public DateTime CreationTime { get; set; }
}
public class Location : IHasCreationTime
{
public string Lat { get; set; }
public string Lng { get; set; }
[DisableDateTimeNormalization]
public DateTime CreationTime { get; set; }
}
[HttpGet]
public DateModel GetDateTimeKindProperty([DisableDateTimeNormalization]DateTime date)
{
return new SimpleDateModel
{
Date = date
};
}