Original: Taste of Little Sister (wechat official ID: XjjDog), welcome to share, please reserve the source.
Cleaning up the mail today, I found such a junk message. I don’t have any money to spend on this nonsense, despite the recent racy performance of the STOCK market. I have A sum of money, squatting on A delisted A share. Because of the loss of too much, so now only dream.
Daylight saving time
Magic time
For the time issue, let’s start with a bit of magic code.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sTime = "The 2020-03-08 00:00:00";
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
Date time = sdf.parse(sTime);
System.out.println(time.getTime());
System.out.println(time);
Calendar cd = Calendar.getInstance();
cd.setTime(time);
// What time is it in 2 hours?
cd.add(Calendar.HOUR, 2);
time = cd.getTime();
System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
System.out.println(time.getTime());
System.out.println(time);
Copy the code
We generated a time from the string, which is the time mentioned in the above email. Then I added two hours to that. When the results run, something magical happens: the printed results show that the difference between the two times is 3 hours!
There are pictures to prove it.
This is the magical side of time.
Daylight saving time can result in an extra or less hour on a given day.
To understand daylight saving time, you first need to understand the concept of time zones. In The Order of Time, he explains that time zones are the result of trade-offs. The world was eventually divided into 24 time zones, each 15° across longitude.
Among them, Beijing time, China adopts the international time zone east 8 hours zone as the standard time, which is also the time we use now, but its location is in Pucheng, Shanxi Province.
However, in the Qing Dynasty, the drum Tower on the central axis of Beijing was used as the standard time. Xjjdog spent some time there, and it’s a beautiful, secluded place.
Time to time
With all this nonsense, let’s take a look at the common GMT and UTC.
UTC and GMT are generally described in obscure terms, but there are four common times encountered in code, which are explained in human terms.
GMT
Greenwich Mean Time refers to the standard time at the Royal Greenwich Observatory on the outskirts of London, as the prime meridian is defined by the longitude that passes through there.
The reason why it is prime meridian is not geographical, it is historical. If I had my say, I would set the amazing Bermuda Triangle, now 64°45 ‘west, at zero degrees.
UTC
UTC and GMT are generally regarded as undifferentiated representations. But GMT, which measures time by the Earth’s rotation, isn’t that accurate.
UTC is atomic time, which is more reliable. Each year the Greenwich Observatory sends out time messages based on UTC.
So what you see here in GMT is the old world method of timing, the most advanced method of timing, which is UTC.
Coordinated Universal time (UTC) is a time measurement system that approximates universal time as closely as possible in time, based on the length of atomic time seconds. (This involves steady atomic radiation, which is more accurate than the earth’s rotation.)
The UTC atomic clock, located in Boulder, Colorado, is the most advanced aluminum-ion optical clock with a deviation of one second every 23.7 billion years! Don’t forget to dig me out of time.
Unix Time
Unix timestamp. The number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), excluding leap seconds.
It means that the timestamp before 1970 is negative.
Why 1970? Because early machines were all 32 bits. The maximum interval in 32-bit TIME is 68 years, and the earliest UNIX operating system takes into account the age of the computer and the TIME limit of the application as the era of UNIX TIME on January 1, 1970.
Daylight saving time DST
And then there’s the damn daylight saving time. This is an artificial time.
Generally in the dawn of the early summer, artificial time to speed up an hour. This can make people get up early and go to bed early, reduce the amount of lighting, in order to make full use of lighting resources, thus saving lighting electricity. Nearly 110 countries around the world observe daylight saving time every year.
It is also known as “daylight saving time” and “Daylight Saving Time”. The uniform time used during this system is called “Daylight saving time”.
Time is an abstract, invisible and intangible thing. Even our clocks are distorted at the limit. Daylight saving time makes time a little more complicated.
impact
1) A day doesn’t always have 24 hours, it could be 23, it could be 25. The Date handling class does not always print as we expect.
public static void dayTime(TimeZone timeZone) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Time Zone is " + timeZone.getDisplayName() + "" + timeZone.getID());
Calendar start = Calendar.getInstance(timeZone);
//UTC 1970-01-01
start.setTime(new Date(0));
System.out.println("start=" + fmt.format(start.getTime()));
//current time
long end = Calendar.getInstance(timeZone).getTimeInMillis();
boolean find = false;
for (long i = start.getTimeInMillis(); i < end; i = start.getTimeInMillis()) {
start.add(Calendar.DATE, 1);
if ((start.getTimeInMillis() - i) % TimeUnit.DAYS.toMillis(1) != 0) {
find = true;
System.out.println("from " + fmt.format(new Date(i)) +
"to " + fmt.format(start.getTime()) +
" has " + (start.getTimeInMillis() - i) + "ms" +
"[" + (start.getTimeInMillis() - i) / (3600 * 1000L) + "hours]"); }}if(! find) { System.out.println("Every day is ok."); }}Copy the code
The code shows that during the period 1986-1991 in China, a day was not always 24 hours long.
These are certainly more personalized data. So, where is this data coming from? Tracking code, we can find sun. Util. Calendar. ZoneInfo class. The operating system stores the specific configuration files of each time zone, through their coordination, can complete the display of different regions and different times.
** Time zone bugs happen from time to time, especially for international projects. Due to the time zone difference, it is possible to find an extra or missing day when entering the birth date of some people! In China, for example, people born between 1986 and 1991 May encounter this kind of ghost problem.
For those of you in this section, at some point, while alive, you may not exist!
All kinds of time means this round and round, always make people dizzy. A good idea is to adjust all the time zones to be the same. Even mysql provides the serverTimezone parameter for harmonization.
&serverTimezone=Asia/Shanghai
Copy the code
The JVM also provides parameters.
-Duser.timezone=GMT+8
Copy the code
Almost everywhere is awash with the damn Timezone. We’re so weak, we don’t even have a single planet.
In fact, daylight saving time was only implemented in China for 6 years from 1986 to 1992, and then it was abolished. Thank god, I can sleep every day without worrying about these supernatural events.
With technology, the city lights up at 6:00. There is no day or night. It’s time to eliminate daylight saving time altogether, but it’s not my call.
End
Next time someone asks you, are there 24 hours in a day? Don’t shout yes like a pupil just knowing numbers. Thanks to the addition of daylight saving time, “an hour” can pass before you blink.
Time is a relative product, different people have different views. A stewardess’s watch, for example, always moves slower than yours — that’s why they’re younger.
As we learn more about the world, many things that we thought were true have slowly begun to rot. We just get used to it, but the truth never changes.
Just like you think I’m a programmer typing now, but I’m actually a dog.
Ideological things, who can say.
Xjjdog is a public account that doesn’t allow programmers to get sidetracked. Focus on infrastructure and Linux. Ten years architecture, ten billion daily flow, and you discuss the world of high concurrency, give you a different taste. My personal wechat xjjdog0, welcome to add friends, further communication.