Python theme-time module

Source: Internet
Author: User
Tags month name

Common time functions

I recently participated in a python project and found that I often encountered some common modules. I have to check them every time I use them. Finally, I decided to break through and summarize the common Python knowledge. The following summarizes common time processing functions in Python.

Before you begin, you must first describe these points:

  1. In python, there are usually nine elements in time: 1), timestamp 2), formatted time string 3), and tuples (struct_time. Since the time module of Python mainly calls the C library, different platforms may be different.
  2. UTC (Coordinated Universal Time) is the Greenwich Mean Time, the world standard time. UTC + 8 in China. DST (Daylight Saving Time) is the daylight saving time.
  3. Timestamp: Generally, the timestamp indicatesJanuary 1, 1970 08:00:00The offset that starts to be calculated in seconds. Run "type (Time. Time ()" and return the float type. Functions that return timestamp mainly include time () and clock.
  4. _Time: there are nine elements in struct_time. The main functions that return struct_time include gmtime (), localtime (), and strptime ().

There are many time functions, but they are commonly used. Before using the time function, import the time module as an example. Before introducing common functions, you must first understand the most basic functions:

(1) Time Function -- returns the timestamp of the current time, floating point format

>>> From time import * # import all functions of the python module >>> time () 1370485361.442

(2) localtime function-localtime ([secs]), converts a timestamp to struct_time of the current time zone. If the secs parameter is not provided, the current time prevails.

# The default value is the current time.>>>Localtime ()Time. struct_time (tm_year = 2013, tm_mon = 6, tm_mday = 6, tm_hour = 10, tm_min = 39, tm_sec = 49, tm_wday = 3, tm_yday = 157, tm_isdst = 0) # equivalent to localtime ()>>>Localtime (Time ())Time. struct_time (tm_year = 2013, tm_mon = 6, tm_mday = 6, tm_hour = 10, tm_min = 40, tm_sec = 37, tm_wday = 3, tm_yday = 157, tm_isdst = 0) # specify a timestamp parameter >>> localtime (1370485361.442) time. struct_time (tm_year = 2013, tm_mon = 6, tm_mday = 6, tm_hour = 10, tm_min = 22, tm_sec = 41, tm_wday = 3, tm_yday = 157, tm_isdst = 0) # The default value is from January 1, January 1, 1970> localtime (345) time. struct_time (maid = 1970, tm_mon = 1, tm_mday = 1, tm_hour = 8, tm_min = 5, tm_sec = 45, tm_wday = 3, tm_yday = 1, tm_isdst = 0) >>> localtime (1) time. struct_time (maid = 1970, tm_mon = 1, tm_mday = 1, tm_hour = 8, tm_min = 0, tm_sec = 1, tm_wday = 3, tm_yday = 1, tm_isdst = 0)

Localtime () output parameter description:

Index) Attribute) Value (values)
0 Tm_year (year) For example, 2013
1 Tm_mon (month) 1-12
2 Tm_mday) 1-31
3 Tm_hour (hour) 0-23
4 Tm_min (points) 0-59
5 Tm_sec (seconds) 0-61
6 Tm_wday (weekday) 0-6 (0 indicates Sunday)
7 Tm_yday (the day of the year) 1-366
8 Tm_isdst (whether it is timeout) The default value is-1.

 

 

 

 

 

 

(3) gmtime () function -- similar to the localtime () method, the gmtime () method converts a timestamp to struct_time of the UTC time zone (0 time zone.

>>> gmtime()time.struct_time(tm_year=2013, tm_mon=6, tm_mday=6, tm_hour=4, tm_min=2, tm_sec=47, tm_wday=3, tm_yday=157, tm_isdst=0)>>> gmtime(time())time.struct_time(tm_year=2013, tm_mon=6, tm_mday=6, tm_hour=4, tm_min=2, tm_sec=53, tm_wday=3, tm_yday=157, tm_isdst=0)

(4) The asctime () function-asctime ([tuple]) represents a time tuples or struct_time as follows:'Sun Jun 20 23:21:05 123'. If no parameter exists, time. localtime () is passed in as the parameter.

>>> Asctime () # The default value is the current time's struct time 'thu Jun 06 10:55:40 11' >>> asctime (localtime () 'thu Jun 06 10:56:02 100'

(5) The ctime function-ctime ([secs]) converts a timestamp (floating point number calculated by second) into the form of time. asctime. If the parameter is not provided or is none, the default time. Time () is used as the parameter. It is equivalent to time. asctime (time. localtime (SECs )).

>>> Ctime () # equivalent to ctime (Time () 'thu Jun 06 11:05:34 100' >>> ctime (Time ()) 'thu Jun 06 11:05:44 100' # default start from 08:00:00 on April 9, 2013 >>> ctime (238) 'thu Jan 01 08:03:58 100' >>> ctime (1) 'thu Jan 01 08:00:01 123'

The above are basic time functions, and below are common time processing functions.

 

In actual storage, more is the storage timestamp (this is easy to calculate time and has better storage performance), rather than the time format that is easier to read, we have the impulse to convert the timestamp into a common time format-The strftime function.

(6) strftime function -- strftime (Format[,Tuple]): Converts a time string or struct_time (such as returned by time. localtime () and time. gmtime () to a formatted time string. If tuple is not specified, time. localtime () is passed in (). If any element in the tuples crosses the border, the valueerror will be thrown.

>>> Strftime ('% Y-% m-% d % H: % m: % s', localtime (Time ())) '2017-06-06 11:10:57 '# This is our favorite time format >>> strftime (' % Y-% m-% d', localtime ()) '2017-06-06 '>>> strftime (' % Y-% m-% d % x', localtime ()) ==> % x gives the default local time '2017-06-06 11:12:39'

Format parameter description:

Format Description
% Local (locale) Simplified week name
% Local full week name
% B Simplified local month name
% B Local full month name
% C Local Date and Time Representation
% D The day of the month (01-31)
% H The hour of the day (in 24-hour format, 00-23)
% I Hour (in 12-hour format, 01-12)
% J Day of the year (001-366)
% M Month (01-12)
% M Minutes (00-59)
% P The identifier of the local am or PM.
% S Seconds (01-61)
% U The number of weeks in a year. (00-53 Sunday is the beginning of a week .) All days before the first Sunday are placed in week 0th.
% W The day of a week (0-6, 0 is Sunday)
% W Similar to % u, % W starts from Monday as a week.
% X Local date
% X Local time
% Y Remove the year of the century (00-99)
% Y Complete year
% Z Name of the time zone (if it does not exist, it is a null character)
% '%' Character

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(7) mktime () function -- mktime (Tuple) To convert a struct_time or the time tuples of nine parameters into timestamps.

>>> Mktime (localtime () 1370490962.0 >>> mktime (2013, 6, 6, 13, 3, 38, 1, 48, 0) # In this case, the error traceback (most recent call last) is written: file "<pyshell #28>", line 1, in <module> mktime (2013, 6, 6, 13, 3, 38, 1, 48, 0) typeerror: mktime () Takes exactly one argument (9 Given) >>>Mktime (2013, 6, 6, 13, 3, 38, 1, 48, 0 ))# Note that the tuples of the nine parameters serve as a parameter 1370495018.0 >>>

(8) strptime () function -- strptime (s)Tring[,Format]): Converts a formatted time string to struct_time. It is the inverse operation of strftime.

>>> strptime('2013-06-06 11:12:39','%Y-%m-%d %X')time.struct_time(tm_year=2013, tm_mon=6, tm_mday=6, tm_hour=11, tm_min=12, tm_sec=39, tm_wday=3, tm_yday=157, tm_isdst=-1)

 

Other common time functions:

(9), sleep () function-sllep (SECs), you can call time. sleep to suspend the current process. Time. Sleep receives a floating point parameter, indicating the time when the process is suspended.

(10) Clock () function-this function is on different systemsDifferent meanings. In Linux, it returns "process time", which is a floating point number (timestamp) expressed in seconds ). In Windows, time. Clock () returns the number of seconds since the method was called for the first time. The accuracy is higher than 1 microsecond. You can use this function to record the execution time of the program.

To sum up, this function has two functions:
During the first call, the actual running time is returned;
After the second call, the returned time interval from the first call to the current call is

>>> Clock () 1.655286571487689e-06 # The first call is the program running time> clock () 8.366160976619078 # The second call is the same as the first call >>> clock () # The third call is the first time interval of 12.056175791433892 >>> sleep (1)

 

Summary:

(1) common applications

1) obtain the current time:

Time. Time () Get the current Timestamp
Time. localtime () struct_time form of the current time
Time. ctime () string format of the current time

2) convert the timestamp into a string:

Time. strftime ("% Y-% m-% d % H: % m: % s", time. localtime ())

3) convert a string to a timestamp:

Time. mktime (time. strptime ('2017-06-06 ',' % Y-% m-% d % H: % m '))

(2) There are three expressions in Python and the conversion between these three methods:

1) Timestamp

2) tuple or struct_time

3) format the string.

 

Of course, there are other functions in the time module, which are not used yet and can be supplemented later.

 

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.