Detailed explanation of Date and Time Processing in Python, and detailed explanation of python

Source: Internet
Author: User

Detailed explanation of Date and Time Processing in Python, and detailed explanation of python

In Python, there are three processing libraries for time and date: time, datetime, and Calendar. datetime has three classes: datetime. date, datetime. time, And datetime. datetime. The time can be divided into timestamp, local time, and UTC time (World Standard Time ). Does it sound messy? So what are the differences between them? What is the connection? How can we convert it?

Time Module

In the time module, there are three manifestations of time:

Timestamp, usually Unix timestamp, is the number of seconds from January 1, 1970 to the present.
Struct_time form of local time: a name tuples with a length of 11, the first is the year, the second is the month ....
Struct_time format of UTC time: a name tuples with a length of 11, similar to the previous one, but only for UTC time
The latter two are of the same type. The difference is that one is local time and the other is utc time.

Methods for obtaining time formats:

Print ("time stamp:", time. time () # timestamp: 1479193181.829338 print ("local time:", time. localtime () # struct_time local time. struct_time (tm_year = 2016, tm_mon = 11, tm_mday = 15, tm_hour = 14, tm_min = 59, tm_sec = 41, tm_wday = 1, tm_yday = 320, tm_isdst = 0) print ("utc time:", time. gmtime () # struct_time type utc time. struct_time (tm_year = 2016, tm_mon = 11, tm_mday = 15, tm_hour = 6, tm_min = 59, tm_sec = 41, tm_wday = 1, tm_yday = 320, tm_isdst = 0)

The difference between the local time (Beijing time) and UTC time is 8 hours.

Conversions of various time formats:

Time_stamp = time. time () # timestamp local_time = time. localtime (time_stamp) # time stamp to struct_time type local time utc_time = time. gmtime (time_stamp) # time stamp to struct_time type utc time time_stamp_1 = time. mktime (local_time) # struct_time-type local time to time_stamp_2 = calendar. timegm (utc_time) # struct_time-type utc time to timestamp print (time_stamp, time_stamp_1, time_stamp_2)

Conversion between various time formats and strings:

Print (time. ctime (time_stamp) # time stamp to string (local time string) print (time. asctime (local_time) # convert local time of struct_time type to print (time. asctime (utc_time) # convert struct_time type utc time to string # convert struct_time type local time to string: custom format print (time. strftime ("% Y-% m-% d, % H: % M: % S, % w", local_time) # convert utc _time type utc time to string: custom format print (time. strftime ("% Y-% m-% d, % H: % M: % S, % w", utc_time) struct_time = time. strptime (", 15:32:12, 2", "% Y-% m-% d, % H: % M: % S, % w") # convert string to struct_time type

Datetime Module

Next, let's look at the datetime module. This module contains four main classes:

Datetime. time: time class, which only contains time information such as hour, minute, second, And microsecond.
Datetime. date: date type, which only contains date information such as year, month, day, and week.
Datetime. datetime: Date and Time class, which contains all the information of the preceding two.
Datetime. timedelta: time and date difference class, used to indicate the difference between two datetime values.
The basic usage of each class is relatively simple. We recommend that you read the official document: datetime module.

Here we mainly talk about the usage of the datetime. datetime class. The usage of the other two is similar, only slightly different:

A_datetime_local = datetime. datetime. now () # obtain datetime. datetime local time a_datetime_utc = datetime. datetime. utcnow () # obtain datetime. datetime type utc time # datetime. datetime type to string print (a_datetime_local.strftime ("% Y-% m-% d, % H: % M: % S, % w") # datetime. datetime type to string print (a_datetime_utc.strftime ("% Y-% m-% d, % H: % M: % S, % w") a_datetime = datetime. datetime. strptime (", 15:32:12, 2", "% Y-% m-% d, % H: % M: % S, % w") # String Conversion

Datetime. datetime format

Conversions between datetime, timestamp, and struct_time

Datetime. datetime and struct_time can also be converted to each other:

Time_stamp = a_datetime_local.timestamp () # convert datetime type to print (time_stamp) a_datetime_local = datetime. datetime. fromtimestamp (time. time () # convert the timestamp to datetime. datetime local time a_datetime_utc = datetime. datetime. utcfromtimestamp (time. time () # convert the timestamp to datetime. datetime-type utc time print (a_datetime_local, a_datetime_utc) print (a_datetime_local.timetuple () # convert datetime type to struct_time Type print (a_datetime_utc.utctimetuple () # convert datetime type to struct_time type

This article describes the basic usage of various time and date types, the methods for mutual conversion between them, and the methods for mutual conversion between them and strings.

Old rules, the Code has been uploaded to github: https://github.com/xianhu/LearnPython

Site backup: http://xz.jb51.net: 81/201611/yuanma/learnpython(jb51.net0000.zip

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.