Home > Web Front-end > JS Tutorial > body text

Time in Javascript

高洛峰
Release: 2016-11-08 14:23:42
Original
1262 people have browsed it

When we program, we inevitably deal with time. For example, if you want to complete a flash sale event, it will involve many details such as time access, time synchronization and time difference calculation. A simple example of JavsScript. When we enter new Date() in the browser console, we will get the following result:

Sun Oct 30 2016 16:27:04 GMT+0800 (CST)

The first half of the output has a very meaningful meaning It is easy to understand. It represents Sunday, October 30, 2016, 16:27:04 pm. What does the second half of GMT, +0800, and CST stand for? In order to find out, this article will show you Learn more about time in JS.

1. Representation of time

In ancient times, one of the most important tasks of astronomy was to measure time and formulate a calendar. In ancient my country, the year was divided into twelve months, twenty-four solar terms, and the day was divided into twelve hours. The purpose was to create an efficient time expression method to guide farming. With the development of modern society, the expression of time also requires a more scientific and accurate method.

Measurement

Starting from timing tools

In ancient times, human beings worked and rested based on the angle changes between the sun and the earth. People worked at sunrise and rested at sunset. Human beings have always followed this inherited work and rest schedule. law. People at this time had no special concept of time and did not use timing tools.

In the Zhou Dynasty of China, people divided a day into 12 hours: Zi, Chou, Yin, Mao, Chen, Si, Wu, Wei, Shen, You, Wu, and Hai, which corresponded to midnight, cockcrow, Pingdan, and day respectively. Out, Eating Time, Yu Zhong, Sun Noon, Sun Midi, Afternoon, Sun In, Dusk, Ren Ding. Used to guide people's daily life patterns. At this time, we use hourglasses and sundials to record the approximate time. The shortest time that can be distinguished is 1 hour (2h).

Later, the Industrial Revolution brought mankind into the industrial age. At this time, people had a more precise division of time. One day was divided into 12 hours, with 60 minutes per hour and 60 seconds per minute. In order to produce more efficiently, we use clocks to record time, and our use of time has reached the second level.

Now, our timing tool has been upgraded to a cold atomic clock. For example, my country's Beidou navigation satellite system uses atomic clocks to record time, and its timing accuracy has reached the nanosecond level (1ns=10^-9s). We have combined space and time. We can calculate space through time and convert time in space.

ps: I once listened to Xu Ying, PhD supervisor of the Chinese Academy of Sciences, give a speech about Beidou in the park, and I couldn’t help but be impressed by human creativity and China’s strong national power.

With the advancement of human civilization, timing tools are also constantly developing. With the help of these tools, human beings are dividing time more and more finely, and the utilization of time resources is getting higher and higher.

Time unit

We use time units to express specific time intervals. Commonly used time units are: century, year, quarter, month, day, hour, minute, second, millisecond, microsecond, nanosecond, etc. The shortest unit of time that humans can currently measure is attosecond, which is the lifetime of a neutral pion, equivalent to 10^-18 seconds.

It can be said that the process of subdividing people's time and setting standards is actually the process of human beings exploring and utilizing time. Our understanding of time units can reflect our understanding of the time and space in which we live. The unit of time also reflects the development process of human scientific civilization.

Time

We usually say that it is 9 o'clock now, but actually it refers to the time. What we want to express is that it has reached the time point of 9 o'clock. It has neither size nor direction, it only expresses a specific point in time. Just like we use a ruler to measure length, we also use a "ruler" marked with seconds and hours to measure time. And time is just a certain scale on the ruler that we can all understand. The only difference is that the length in space is usually constant, while time is always increasing irreversibly.

2. Time Standardization

First of all, we should realize that time is unified throughout the universe. From the birth of the universe to the present, we can always use a specific time interval value to represent the time we have experienced, which is a fixed value. So we can't help but ask why it is said that it is 9 a.m. here, but it is indeed 1 a.m. in the UK. Aren't these two different times? This leads to the time standard issue we will mention below.

In the past, the scope of human activities was relatively narrow, and the impact of time zones on human work and rest was basically not noticed. We can never imagine that while we are working hard in the sun, people on the other side of the earth have already fallen asleep.

But after the industrial revolution, human production and transportation continued to advance, and global collaboration became an unstoppable trend. At this time, people have realized that the earth is constantly rotating from west to east, and one rotation is one day. The sunrise times are different in areas with different longitudes. For example, it is still noon in Beijing, China, and it is late at night in New York, the United States on the other side of the ocean. We made a phone call at 8 o'clock in the morning and told XXX from the United States that it was time to go to work. The other person must have a confused look on his face, and I was about to go to sleep.

It is precisely because people realize the astronomical laws of the earth's rotation that they need to develop a globally unified time standard. In this way, we can calculate the local time (sunrise and sunset times) in any area of ​​the earth based on local time to facilitate updates. Good collaboration.

Greenwich Mean Time (GMT)

Greenwich is the Royal Greenwich Observatory located in the suburbs of London, England. The Greenwich Time we often refer to refers to a time measurement system based on the local time as the standard time. .

In order to accurately represent the time in various places, in 1884, the International Longitude Conference decided to set the longitude passing through the Astronomical Astronomical Observatory in Greenwich, England, as the starting longitude for calculating longitude, called the prime meridian. Mean solar time, calculated from the prime meridian at midnight, increases or decreases by 12 hours to the east and west respectively. As a unified time standard, it is called Greenwich Mean Time. This is the origin of GMT time. It is a set of time standards defined based on the rotation of the earth. It plays an important role in daily life, astronomical navigation, space flight and other human activities.

The establishment of GMT also gave rise to the concept of time zones. When we listen to news reports about foreign countries, we often hear what major events happened at XX:XX local time, YY:yy local time, and then balabala. This is actually to avoid the differences in time understanding between people in different time zones in the representation of time.

Time Zone (Time Zone) sets the standard time of a region. Theoretically, with the meridian divisible by 15 as the center and extending 7.5 longitudes to the east and west, it is divided into a time zone. In fact, since the territory of each country is irregular and usually spans multiple time zones, the country also stipulates an actual time zone within the entire administrative region, which is called a legal time zone. For example, China spans five time zones from west to east: East 5, East 6, East 7, East 8 and East 9. After the founding of the People's Republic of China, the country uniformly adopted the East 8 time zone where the capital Beijing is located as the standard time. It is called Beijing time.

Coordinated Universal Time (UTC)

UTC is often mistaken for GMT. In fact, the two representation methods are completely different.

The origin of UTC

Human beings have observed the slow decline of the earth's rotation. After a period of time, the time it takes for the earth to rotate once will be longer than the current 24 hours, so GMT time is no longer suitable for use as standard time. Today's standard time is Coordinated Universal Time, which is timed by atomic clocks. This is a time scale maintained by the International Bureau of Weights and Measures and the International Earth Rotation Service (IERS). It is the basis for the coordinated broadcast of various standard frequency and time signals.

Then why do we need to have UTC again? Isn’t it too messy? In fact, no one wants to be so troublesome. This is the "rotation" of the earth itself.

We commonly believe that it takes 24 hours for the Earth to rotate once. However, due to the slower rotation of the Earth, after 24 hours, the Earth may not have completed one full revolution. In order to ensure that 24 hours matches the Earth's complete revolution, it must be The time on the clock waits for the Earth to complete one rotation. This is the meaning of the word "coordinated" in Coordinated Universal Time. UTC time is actually slower than International Atomic Time (TAI).

Note: The second is the basic international unit of time. The current international standard 1 second is: the duration of 9,192,631,770 cycles of radiation corresponding to the transition between the two hyperfine energy levels of the ground state of the cesium 133 atom

To put it bluntly, TAI Time is defined based on atomic laws, and this time will not change for a long time. UTC is a time set to comply with the earth's rotation period. UTC will stop clocks around the world to make up for the difference in the slowing down of the earth's rotation. The correction and synchronization mechanism is IERS. All countries in the world determine their local time based on UTC time and the time zone of their respective countries to ensure low-error synchronization of global time.

Leap seconds

UTC corrects the time by adding leap seconds (leap-secons) to eliminate the impact of the earth's slowing rotation. Leap seconds are deliberately set to balance the time difference with atomic clocks caused by the slowing of the earth's rotation. If leap seconds are not included, maybe tens of thousands of years later, 12 o'clock at noon will become 0:00 am; in a few million years, winter on the calendar will turn into summer, and then the entire production and life will be chaotic. .

The introduction time of leap seconds is in June and the end of December every year, depending on the evolution difference of UTC-TAI, and every six months it will be announced whether leap seconds need to be added (this is what IERS does) .

The last leap second introduction time was UTC 2015-6-30 23:59:60 and the next leap second introduction will be at:

2016-12-31 23:59:592016-12-31 23:59 :602017-01-01 00:00:00

The time difference from 2015-07-01 00:00:00 UTC to 2017-01-01 00:00:00 UTC is: UTC - TAI = -36s. Also That is to say, our UTC time standard is artificially 36 seconds slower than the real atomic clock time.

PS1: Don’t you feel like you’ve made a profit? You’ve lived an extra 36 seconds! PS2: Most of the time currently used by humans is UTC time, but in many cases the time zone division rules of GMT are still retained, so GMT time is still displayed in many places.

Beijing Time (China Standard Time, CST)

Let’s talk about my country’s time base again. Beijing time is not determined in Beijing, but is determined by nine cesium atomic clocks (cesium) at the National Time Service Center of the Chinese Academy of Sciences (National Time Service Station) located in Lintong, Shaanxi Province and Pucheng, Shaanxi Province, the geometric center of China’s territory. The clock) and two hydrogen atomic clock sets are realized through precise comparison and calculation, and are determined by real-time comparison with time service departments around the world through satellites. Domestic equipment can be synchronized with local time through the NTP (Network Time Protocol) server provided by the National Time Service Center. Specific usage can be found here.

On Mac, you can adjust the time through the following command and the specified NTP server:

➜ ~ sudo ntpdate -u cn.pool.ntp.org
1 Nov 03:20:08 ntpdate[84376]: adjust time server 59.46.44.253 offset 0.014804 sec
➜ ~

3. Summary

Go back to what was mentioned at the beginning of the article, new Date() is outputting data, and all the content will be clear:

GMT, which represents Greenwich mean time , but what GMT here means is: the time zone used for the time is the GMT standard, and the time used is the UTC time

+0800 represents the time difference between the current time and the standard time, accurate to the minute. +0800, means that the current time is 8 hours earlier than UTC time

CST refers to Beijing time

done!

4. Postscript

There are many details in a programming language, which often contain great wisdom. Take the time mentioned in this article as an example. A few simple words contain the achievements of mankind's scientific and technological civilization development in recent hundreds of years, and also reflect mankind's respect and awe for time. Every part of it is worth savoring.


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!