Python Module Learning--time module

Source: Internet
Author: User

The time-related modules in the Python language are mainly: Time,datetime,calendar (the Python timing module primarily calls the C library, so the platforms may vary)

Noun Explanation: UTC time

UTC time (coordinated Universal time, world co-ordination) is GMT, world standard Time. In China for Utc+8. DST (daylight saving time) i.e. daylight saving time

There are 3 ways in which Python is represented in time, namely:

1. Time stamp

The timestamp represents the offset, measured in seconds, from January 1, 1970 00:00:00 to the current time (float type). The way to return timestamps is mainly time (), clock (), and so on.

The value returned by the time () method : Current UTC time in seconds starting from era (January 1, 1970 00:00:00) (float type)

>>> time.time () #返回当前UTC时间的时间戳

1510885126.5995045

The value returned by the clock () method :

The first comment on this method is written in this way:

Return the CPU time or real time since the start of the process or since
The first call to clock (). This have as much precision as the system
Records.

The clock translates to mean the timer.

Precision translation comes to mean accurate

These two words are a good expression of the function that this method implements.

The clock () method has different meanings 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 QueryPerformanceCounter () on WIN32, which is more accurate than milliseconds)

>>> Time.clock ()
3.5378305527754105e-06
>>> Time.clock ()
5.703646194302607
>>> Time.clock ()
17.30329184519444

The first clock () output is the program run time
The second to third clock () output is the time interval from the first clock

2.struct_time (Time object in tuple format)

There are 9 elements in the Struct_time tuple, and the methods that return Struct_time time objects are mainly gmtime (), localtime (), Strptime ().

Represents what the 9 elements contained in the struct_time tuple mean for each other:

The value of Tm_wday (weekday) is: 0~6 (0 = Sunday)

The gmtime ([secs]) method converts a timestamp to the Struct_time time object format of UTC time. The default current time when no parameters are present

>>> Time.gmtime ()
Time.struct_time (tm_year=2017, tm_mon=11, tm_mday=17, tm_hour=2, tm_min=29, tm_sec=35, tm_wday=4, tm_yday=321, tm_ isdst=0)

The localtime ([secs]) method converts a timestamp to the Struct_time time object format of the current time zone. With no parameters, whichever is the current time
>>> Time.localtime ()
Time.struct_time (tm_year=2017, tm_mon=11, tm_mday=17, tm_hour=10, tm_min=29, tm_sec=42, tm_wday=4, tm_yday=321, tm_ isdst=0)

The strptime () method converts a time string to the Struct_time time object format. and Strftime () is the inverse operation

>>> time.strptime ("2017/11/17", "%y/%m/%d")
Time.struct_time (tm_year=2017, tm_mon=11, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=321, tm_isdst= -1)

3. Formatted string

The strftime (Format[,t]) method converts a time in a Struct_time time object format to a formatted time string. If T is not specified, the Time.localtime () is passed in.

>>> time.strftime ("%y-%m-%d%h:%m:%s") #未指定t参数
' 2017-11-17 11:30:13 '

>>> time.strftime ("%y-%m-%d%h:%m:%s", Time.gmtime ()) #指定 T parameter is UTC time
' 2017-11-17 03:33:15 '

If any of the elements in the struct_time tuple are out of bounds, a valueerror error will be thrown.

Format comparison:

Other methods:

time.sleep ([secs]): Thread delays the specified time run. Unit is seconds

time.asctime ([t]) represents a struct_time representing time as "' Fri 17 11:42:02 2017 '" in this form. If there are no parameters, the Time.localtime () is passed as a parameter

>>> Time.asctime ()
' Fri Nov 17 11:42:02 2017 '
>>> Time.asctime (Time.localtime ())
' Fri Nov 17 11:42:31 2017 '

time.ctime ([secs]) converts a timestamp into the form of a time.asctime (). If there are no arguments, time.time () is passed in as a parameter. Its role is equivalent to

Time.asctime (Time.localtime ())

>>> Time.ctime ()
' Fri Nov 17 11:40:07 2017 '
>>> Time.ctime (Time.time ())
' Fri Nov 17 11:40:54 2017 '

Halo, why using the Time.ctime (Time.time ()) method will get the time of the current time zone. The time stamp returned by the Time.time () method represents UTC time.

>>> time.strftime ("%y-%m-%d%h:%m:%s", Time.gmtime (Time.time ()))
' 2017-11-17 03:46:59 '

>>> time.strftime ("%y-%m-%d%h:%m:%s", Time.gmtime (Time.time () +28800))
' 2017-11-17 11:54:19 '

28800 for 8 hours (China time is UTC time + 8 hours)

Let's put the question here for the moment.

About time modules, these are just the most basic applications. How to use these methods rationally is the most should learn.



Python Module Learning--time module

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.