. In -depth discussion of the instantaneous time and calendar time in the calendar in the calendar in.
In the .NET framework, DateTime and DateTimeoffset were two different ways of time, each with advantages and disadvantages and applicable scenarios.
datetime
Datetime means "calendar time", the specific location on the calendar (for example, "Eastern Time"). It has two attributes, <.> .Kind and .UTCDATETIME :
.kind The specified time is UNSPECIFIED (no connected calendar),
LOCAL (based on the current time zone) or - UTC (coordinated In the world).
.utcdatetime It is an equivalent UTC representation of the calendar time.
datetimeoffset
- Datetimeoffset means "instantaneous time", which is the general time point for everyone. It captures local and UTC
Time as a single entity:
.LocalDateTime indicates the local time of the specified time zone.
.utcdates means the same moment in UTC.
.offset is the difference between local time and UTC.
- When will it be used?
datetime: -
indicates the time (for example, "" East time at 9:00 in the morning ").
- Convert between time and UTC.
However, be careful to treat hypothesis .Kind is the situation of UNSPECIED.
dateTimeOffset:
Make sure the accuracy of a certain time. -
Tracking the time with associated local time (for example, "" Eastern time at 9:00 am on January 1, 2023 ").
When modifying the DateTimeOffset recorded earlier, only the offset is not enough to ensure accuracy. -
-
- Activities: The perspective of the photographer
Imagine that a photographer stands on the calendar timeline and uses a camera to stand on the person who stands on the instantaneous timeline. The perspective of the camera represents the .offset of DateTimeOffset. Even if the time zone may change, everyone is pointing to the same moment. However, it is known that the amount of offset is not enough to determine the specific time zone. -
At the same time, UTC represents the camera installed on a tripod, with a solid hand and zero displacement.
Best practice -
-
When comparing the DateTimeOffset values, standardize it into zero displacement. -
For unit testing, test DateTimeOffset values and .offset properties.
Be careful of the hidden conversion of DateTime to DateTimeOffset because they may lead to unexpected results.
The above is the detailed content of DateTime vs. DateTimeOffset: When Should You Use Instantaneous vs. Calendar Time in .NET?. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn