Python Programming Common module records

Source: Internet
Author: User

Time (timing related module)

Logging (log module)

"Time Module" has a datetime, calendar, etc.

The time module is commonly used to get the current system datetime, which can be used for log file naming or any sleep function to suspend a process.

Common usage:

One, now need to know the current time, converted to a string:

# !/usr/bin/env python   # coding=utf-8    Import Time     print time.strftime ('%y-%m-%d%A%x%Z', Time.localtime (Time.time ()))  

  

Second, calculate the time of the execution of one end of the program

Import= time.time () time.sleep (2.3= time.time ()print time.ctime ( T2-T1)         #  t2-t1 is the difference between timestamps when two statements are executed, that is, the time interval. Convert it to the corresponding second with CTime

The time module's summary reference--python in the time module detailed, summarizes very well.

Before you begin, you should first explain these points:

    1. In Python, there are usually several ways to represent time: 1) timestamp 2) formatted time string 3) tuples (struct_time) a total of nine elements. Because Python's time module implementation primarily calls the C library, each platform may be different.
    2. UTC (coordinated Universal time, world co-ordination) is GMT, world standard Time. In China for Utc+8. DST (daylight saving time) is daylight saving time.
    3. Timestamp (timestamp): Typically, a timestamp represents an offset that is calculated in seconds, starting January 1, 1970 00:00:00. We run "type (Time.time ())" and return the float type. The function that returns the Timestamp method mainly has time (), clock () and so on.
    4. Tuple (struct_time) mode: There are 9 elements in the Struct_time tuple, and the functions that return struct_time are mainly gmtime (), localtime (), Strptime (). Several elements in this way tuple are listed below:

Time.time ()

Timestamp of seconds since 1970 timestamp, floating-point number

Time.sleep (secs)

How many seconds the process hangs, floating-point numbers

Time.clock ()

It is important to note that the meanings are different on different systems. On a UNIX system, it returns "process time", which is a floating-point number (timestamp) in seconds. In Windows, the first call returns the actual time that the process is running. The second subsequent call is the elapsed time since the first call to the present. (actually based on WIN32 QueryPerformanceCounter (), which is more accurate than microseconds)

Import Timeif __name__=='__main__': Time.sleep (1)      Print "clock1:%s"%Time.clock () time.sleep (1)      Print "clock2:%s"%Time.clock () time.sleep (1)      Print "clock3:%s"% Time.clock ()

#运行结果 #:
clock1:3.35238137808e-006
clock2:1.00004944763
clock3:2.00012040636

Time.localtime ()

The time represented by the time_struct tuple

Time.ctime ()

Represent timestamp as "%a%b%d%h:%m:%s%Y"

Time.asctime ()

Struct_time represents time as "%a%b%d%h:%m:%s%Y"

Specific conversion relationships such as:

  

Python Programming Common module records

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.