A method of time processing in Python __python

Source: Internet
Author: User
Tags benchmark time in milliseconds

1. Overview

The DateTime module is mainly used to express the date, that is, we often say that the month and a minute, the Calendar module is mainly used to express the year and the day, is the week, such as information, Time module, the main focus in time seconds, roughly from the function, we can think of three is a complementary relationship Each focus on one piece. It is convenient for the user to choose the module of the hand according to the different use.

2. From time module

In order to learn the time module, we need to know a few times-related concepts first:

1) Epoch

Let's say we're going to represent the time in milliseconds, say 1000000 milliseconds, and there's a problem that has to be solved, what is the starting point for this 1000000 millisecond, and what time is our time datum point? If I say you are 1.8 meters tall, that height is relative to the ground you stand on. This time datum point is epoch, in the UNIX system, this datum point is January 1, 1970 0 o'clock the whole time point.

2) GMT, UTC

Above we say that epoch represents the starting point of 1970, then which is the 1970 relative to which base time? In general, it is also called GMT (Greenwich Mean time) Relative to GMT, also known as UTC (coordinated Universal Time), why is there two names for a timeline? Historically, the first GMT, and then the UTC.

UTC is the time standard we use now, and GMT is the old time metric. UTC calculates time based on an atomic clock, and GMT calculates the time according to the rotation and revolution of the Earth.

So, you can think of UTC as the true base time, and the deviation of GMT relative UTC is 0.

In practice, our computer has a hardware module RCT, which records UTC time in real time, which has a separate battery power, even if the shutdown does not affect.

With this time benchmark of epoch and the benchmark of UTC, we can accurately represent a time.

3) DST, Tzone

Although we can accurately express a time, in many cases, we still have to according to the actual situation of the region to adjust the time, the most common is the time zone, Tzone, I believe we are more familiar.

At this point, when we say 5:5 this time, also need to add which time zone of 5:5 to accurately describe a time.

Another adjustment to time is DST.

DST's full name is daylight saving time, which means that in order to make full use of daylight and reduce electricity consumption, an artificial adjustment to the timing depends on the policies and regulations of different countries and regions. For example, let's say you get up at 7 o ' clock in the winter, but it's 6 o ' clock in the summer, so it's time to add 1 hours to the summer, so you can still feel up 7 o'clock, but actually it's one hours ahead.

So, curious of us, be sure to ask, how does Python know the value of Tzone and DST? The answer is through the environment variable.

Here we only take Linux as an example to illustrate.

There are TZ environment variables in Linux, and their values are similar:

cst+08edt,m4.1.0,m10.5.0, this string can be interpreted as follows, separated by spaces, divided into three parts

cst+08 EDT, m4.1.0,m10.5.0

1. The CST in part one indicates the name of the time zone, that is, China Standard Times, which is the time we say Beijing, and +8 indicates that the 8-hour time is UTC.

2. The second part of EDT is the name of DST, we say that DST is different from each country's regional policies and regulations, and EDT can be followed by a time adjustment value like the back of the CST, but due to our domestic only period of 86 to 92 DST, now abolished, So there is no time to adjust the back.

3. The third part is about the start and end time of DST, and we are not reading it carefully.

4, the expression of time, acquisition, conversion

The basic method of acquiring time in the hour module is

t = Time.time ()

It returns the number of seconds from epoch to the present (expressed in floating-point numbers), with UTC time.

We naturally want to turn this second number into a minute and a minute, which is divided into two, one in UTC and one in our time zone.

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.