Defect and substitution of datetime in C # DateTimeOffset

Source: Internet
Author: User
Tags time zones

DateTime in C # has a very serious logical flaw:

var d = DateTime.Now; var d2 = d.touniversaltime (); > d = = D2false> d.equals (D2); false

Enter the above code in C # Interactive mode to find that although one is local time (d), one is UTC time (D2), but the time zone is different, but in this world, it should belong to the same moment. But two times are not equal ...

The reason is that DateTime does not store the time zone, or that it stores only a vague field kind about the time zone, which is the DateTimeKind enumeration type, with three values: Utc/local/unspecified, when the value is Unspecified, Then there will be ambiguity.

But I'm still going to throw the groove if D. Any one of kind or d2.kind is unspecified, then d! = D2 I can understand. But the above d. Kind is local,d2. Kind is UTC, if the time zone logic is not stored by datetime, then the two are uniformly converted to UTC or local, then they should be equal, in fact, as well:

> d = = D2. ToLocalTime ()true

If the local time of D T1 as 9, local time in the time zone Z1 as +8, the corresponding UTC time t0 as the 1,UTC time zone z0 as 0, they do normalization:

Then d = t1-z1 = 8-8 = 0, d2 = t0-z0 = 0-0 = 0.

D! = D2 however. That's where it's weird.

For example, eight east, enter the following code in C # Interactive mode:

>varD3 =NewDateTime (2018,1,1);>d3[2018/1/1 0:xx:xx]>D3. ToLocalTime () [2018/1/1 8:xx:xx]>D3. ToUniversalTime () [ ./ A/ to  -:xx:xx]

It can be found that a simple constructor, in which the developer's mind defaults to local time, is allowed to create a monster that is neither local time nor UTC time.

When the D3 is converted to local time, the D3 is added for 8 hours as UTC time.

When the D3 is converted to UTC time, the D3 is reduced by 8 hours as local time.

So is D3 local time or UTC time? No one knows that unless it exists in a very small local scope and has a very short life cycle, we may be able to assume that it is local time.

However, this local time is also dependent on its operating environment, if there are several time zones inconsistent computer, castrated the time zone information datetime into a string in the network to another time zone (such as the next door 11) in another server, after parsing out, so-called East eight local time 8 o'clock, to Japan , it becomes a monster that is neither local time nor UTC time.

DateTime is deprecated in official documents, but it is recommended to use its substitute DateTimeOffset, which saves time zone information.

Verify in interactive mode:

var dto = datetimeoffset.now; var dto2 = dto. ToUniversalTime (); > dto = = Dto2true

It can be found that DateTimeOffset determines whether the two-time equivalence criterion is judged by the time of the world timeline, regardless of time zone, or even with UTC time. As long as they are in the same time system, can transform each other.

In the actual project, we recommend that you:

    • If a datetime is used, the unification is replaced by DateTimeOffset.
    • If it is useful to a 32-bit UNIX timestamp, the uniform is replaced by a 64-bit long to store the utcticks.

Even if the project itself does not span time zones, it is still possible to encounter time zone problems, such as if MongoDB is used, MongoDB storage is unified into UTC time (as if, forget), and generally with time zone information. But one situation is worse, if your datetime kind is unspecified, the information in the implied time zone will be lost. After the removal, there will be a 8-hour time difference. There are some third-party or your own company's class library and the like, if you do not handle this problem, there are potential time zone loss problems. Unix timestamps are also good for ticks in UTC, both in terms of precision and time span, far beyond UNIX timestamps. Only 64-bit, you can get down to 100 nanosecond precision, over the time span of the years, once and for all, whether it is back to the Unix timestamp or JS timestamp, can be competent. The space cost is also very small, unless your storage space is really flawed.

Defects and substitutes for datetime in C # DateTimeOffset

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.