Sort and summarize the basic knowledge points for processing dates and times in Python

Source: Internet
Author: User
This article mainly introduces the basic knowledge of date and time processing in Python, you can refer to the Python program to process dates and times in multiple ways. Conversions between date formats are common computer chores. The Python time and calendar module can help with date and time processing.
What is Tick?

The time interval is a number measured in seconds. At a specific time point, the time ranges from 12:00 A.M. to January 1, January 1, 1970 (epoch). The unit is second.

Popular time module available in Python, which provides function conversion. This function time. time () returns the current system time because twelve o'clock A.M., January 1, January 1, 1970 (times ).
Example:

#!/usr/bin/pythonimport time; # This is required to include time module.ticks = time.time()print "Number of ticks since 12:00am, January 1, 1970:", ticks

This produces the following result:

Number of ticks since 12:00am, January 1, 1970: 7186862.73399

Date calculation is very easy. However, the time of the day cannot be expressed in this form. In the distant future, the date cannot represent this way-the demarcation point is a period of time in UNIX and Windows in 2038.
What is TimeTuple?

The processing time of Python time functions is a nine-digit tuples, as shown in:

The above tuples are equivalent to the struct_time structure. This structure has the following attributes:

Get Current time:

Converts a time point from a second epoch floating point value to a time point. The floating point value is passed to the function (for example, local time) to return all nine valid values of the time point.

#!/usr/bin/pythonimport time;localtime = time.localtime(time.time())print "Local current time :", localtime

This will produce the following results, which can be formatted in any other decent form:

Local current time : time.struct_time(tm_year=2013, tm_mon=7, tm_mday=17, tm_hour=21, tm_min=26, tm_sec=3, tm_wday=2, tm_yday=198, tm_isdst=0)

Get the formatting Time:

The time can be formatted at any time as required, but a simple method is used to obtain the time. the readable format is asctime ():

#!/usr/bin/pythonimport time;localtime = time.asctime( time.localtime(time.time()) )print "Local current time :", localtime

This produces the following results:

Local current time : Tue Jan 13 10:17:09 2009

Get calendar month:

The calendar module provides a wide range of methods, such as year and month calendars. Here, we print the calendar for the given month (January 1, January 2015 ):

#!/usr/bin/pythonimport calendarcal = calendar.month(2015, 1)print "Here is the calendar:"print cal;

This produces the following results:

Here is the calendar:  January 2008Mo Tu We Th Fr Sa Su  1 2 3 4 5 6 7 8 9 10 11 12 1314 15 16 17 18 19 2021 22 23 24 25 26 2728 29 30 31

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.