2
头图

Hello, I'm crooked.

I would like to share with you a little knowledge that I have discovered recently, which is a bit interesting, but does not require much use of eggs.

Let me make a program for you to see:

public class MainTest {

    public static void main(String[] args) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse("1900-01-01 08:00:00");
        System.out.println(simpleDateFormat.format(date));
    }
}

You said the above program logic is a simple time formatting, what do you say is the output result?

It just takes a glance to know that it must be the output of this result:

1900-01-01 08:00:00

However, if you take the above program out and run it directly, you will find that the output is unexpectedly like this:

1900-01-01 08:05:43

At that time, I was stunned.

I know that the time difference is 8 hours because of time zone issues.

I know that the time difference is 1 hour because of daylight saving time.

But there is a difference of 5 minutes and 43 seconds, there are zeros and there are corrections, which makes me a little confused.

The above case is shared to me by a reader. Their default time in the database is 1900-01-01, plus the time zone issue, it just becomes 1900-01-01 08:00:00, so they are doing it through the program This inexplicable timing issue was stepped on during data migration.

At the same time, he also sent me a link about this bug:

https://bugs.openjdk.java.net/browse/JDK-8266262

At first glance, I saw that this bug is quite new, and it belongs to this year.

I took a closer look and found that it was a duplicate of the previous bug:

But the reasons are mentioned here:

He said that you can look at this link https://www.timeanddate.com/time/zone/china/shanghai?year=1900

Here, in 1900, a change took place:

The timezone offset was UTC +8:05:43 hours all of the period.

Although I didn't really understand what it meant, I saw "5 minutes and 43 seconds":

I understand that the time has been reset due to the change of time zone.

Then I followed the vine and found this on stackoverflow:

https://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result

I was shocked at the time.

This question that was posed 10 years ago has been viewed 746k times. It is a very popular question. I didn't even notice:

The specific question is this:

Just take a glance, and I will translate for you.

The questioner said that he found that there was a difference of 353 seconds between 1927-12-31 23:54:07 and 1927-12-31 23:54:08. It should be 1 second, right?

But changing the time to the following is normal again:

String str3 = "1927-12-31 23:54:07";  
String str4 = "1927-12-31 23:54:08";

If I paste the program, you can also run it and see the result is very amazing:

public static void main(String[] args) throws ParseException {
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    String str3 = "1927-12-31 23:54:07";  
    String str4 = "1927-12-31 23:54:08";  
    Date sDt3 = sf.parse(str3);  
    Date sDt4 = sf.parse(str4);  
    long ld3 = sDt3.getTime() /1000;  
    long ld4 = sDt4.getTime() /1000;
    System.out.println(ld4-ld3);
}

But after I ran it again, I found out: I go, what about 353 seconds?

How come it takes 1 second to run out, there is nothing wrong with it:

I even suspected that it was a jdk version problem, so I changed jdk 9,11,15 and ran it all in 1 second.

This is very strange.

I feel that there is a problem with this question.

But after I read the most praised answer below, I seemed to have a glimpse of it.

This answer is relatively long, let me take all screenshots to show you:

The longer reason is that the author has revised the answer several times.

Why would the answer be revised?

Let's look back, all the answers are hidden in it.

I will choose the key one to tell you.

First look at the first paragraph:

He said (1927) that Shanghai's time zone changed on December 31.

Regarding the details of Shanghai in 1927, he attached a hyperlink. This hyperlink is the website that appeared before. After clicking it, it looks like this:

But this one shows:

No further time changes in 1927 in Shanghai

The translation is: there was no further change in the time of Shanghai in 1927.

Isn't this incompatible with the lumps of content he said below?

He said below that at midnight at the end of 1927, the clock went back 5 minutes and 52 seconds. Therefore, "1927-12-31 23:54:08" actually happened twice, and Java took the moment of the second time, so there is a difference.

I was actually confused when I saw it. This thing didn't match before and after, so I started searching.

Until I found this:

https://coolshell.cn/articles/5075.html/comment-page-2#comments

This is also an article ten years ago.

The author here took a screenshot of the website at that time:

The screenshots from that year show:

At 23:59:59 on December 31, 1927, the next second should be 0:0:0 on January 1, 1928, but this time has been adjusted for 5 minutes and 52 seconds later, At 23:54:08 on December 31, 1927, the 352-second crossing was completed.

This shows what?

It means that the data has been tampered with, and someone has tampered with the information on the webpage!

what is going on actually?

Let's go back to stackoverflow and look down:

This is the first time he has revised his answer, because History changes...

History has changed...

He said here that if the 2013a version of TZDB is used, the original problem will no longer exhibit the exact same behavior.

In 2013a, the result will be 358 seconds and the transition time will be 23:54:03 instead of 23:54:08.

He mentioned a TZDB, what is this?

I don't know, but I searched.

What he should be talking about is this thing.

https://www.iana.org/time-zones

Look at the name, you know, it is a time zone database, which should contain data related to the time zone that is maintained.

In other words, in this time zone database, using the 2013a version of the data, the previous code is another output.

In other words, the data has indeed changed.

The key answer lies in the next edit:

History has changed again...

History has changed again.

In this time zone database, in the 2014f version, the time of change has been moved to 1900-12-31, and now it is only a change of 343 seconds.

343 seconds?

Isn’t it the 5 minutes and 43 seconds ahead of us?

Okay, now the jet lag can be matched, 343 seconds, but the time is still not matched.

Our test time is 1900-01-01 08:00:00, and the time he wrote here is 1900-12-31.

Is it a whole year away?

Okay, let’s see what he edited the last time:

I personally understand what he meant to express like this.

Java has a one-size-fits-all policy in order to unify standards on time zones.

The unified standard is to make any moment before 1900 in the UTC time zone be the standard time.

As for the jet lag...

Just make up at the very beginning.

So, 1900-01-01 00:00:00 plus 8 hours time difference is 1900-01-01 08:00:00, on this basis, 27 years later, from 1927-12-31, that midnight is due to the time 343 seconds brought by the callback.

1900-01-01 08:05:43, I personally think that's how it came about.

As for the program corresponding to the previous stackoverflow, we are now executing the output 1, but 10 years ago, the output was indeed 353.

Just like I changed the program to this:

The final output is not 1, but -342.

In time, a "backflow" occurred.

Well, it's another point of knowledge that is useless.

Finally, add two cold knowledge.

The first one is that I traced it back in the jdk bug list, and found that the earliest relevant issue was raised in 2005:

https://bugs.openjdk.java.net/browse/JDK-6281408

In this, the official reply is as follows:

This issue will not be fixed to avoid any compatibility issues.

The meaning is: I know the problem, but this thing is not easy to handle. Let the bug become a feature first, so let's do it first.

Don't ask, ask because there are historical reasons in it.

The second piece of cold knowledge is that, as mentioned earlier, the time zone changed in 1927.

Do you know why?

I found this description on a website:

https://zh.wikipedia.org/wiki/%E4%B8%AD%E5%9C%8B%E6%99%82%E5%8D%80

1928 was the 17th year of the Republic of China.

That year, the Nanchang Uprising broke out and the Chinese People's Liberation Army was established.

That year, Mao’s main line established a rural revolutionary base in Jinggangshan.

That year, a single spark has become a prairie fire.

One last word

Okay, I saw it here. Reposting, watching, and liking can be arranged at will. I don't mind if you arrange them all. Writing articles is tiring and requires a little positive feedback.

Knock out for readers and friends:

This article has been included from my personal blog, everyone is welcome to play.

https://www.whywhy.vip/

why技术
2.2k 声望6.8k 粉丝