Home>Article>Backend Development> Basic introduction to time in python

Basic introduction to time in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼 Original
2019-06-17 10:11:02 13463browse

In Python, modules related to time processing include: time, datetime and calendar.

Related recommendations: "python video"

Basic introduction to time in python

In Python, there are usually several ways to express time:

1) Timestamp

2) Formatted time string

3) Tuple (struct_time) with nine elements in total.

Since Python’s time module implementation mainly calls the C library, each platform may be different. UTC (Coordinated Universal Time) is Greenwich Astronomical Time, world standard time. In China it is UTC 8. DST (Daylight Saving Time) is daylight saving time. Timestamp (timestamp) method: Generally speaking, a timestamp represents the offset in seconds from 00:00:00 on January 1, 1970. We run "type(time.time())" and the return type is float. Functions that return timestamps mainly include time(), clock(), etc. Tuple (struct_time) method: The struct_time tuple has a total of 9 elements. The functions that return struct_time mainly include gmtime(), localtime(), and strptime(). Several elements in the tuple in this way are listed below:

Index (Index) . )

0 2011

1 Since m_min (minutes) 59

5 TM_SEC (seconds) 0-61

6 TM_WDAY (weekday) 0-6 (0 indicates Sunday)

7 TM_YDAY (the first day of the year) 1 -366

8 TM_ISDST (whether it is summer time) default -1

Then introduce a few commonly used in the Time module Function:

1) time.localtime([secs]): Convert a timestamp to struct_time in the current time zone. If the secs parameter is not provided, the current time shall prevail.

>>> time.localtime() time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=14, tm_min=14, tm_sec=50, tm_wday=3, tm_yday=125, tm_isdst=0) >>> time.localtime(1304575584.1361799) time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=14, tm_min=6, tm_sec=24, tm_wday=3, tm_yday=125, tm_isdst=0)

2) time.gmtime([secs]): Similar to the localtime() method, the gmtime() method converts a timestamp to a struct_time in the UTC time zone (0 time zone).

>>>time.gmtime() time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=6, tm_min=19, tm_sec=48, tm_wday=3, tm_yday=125, tm_isdst=0)

3) time.time(): Returns the timestamp of the current time.

>>> time.time() 1304575584.1361799

4) time.mktime(t): Convert a struct_time into a timestamp.

>>> time.mktime(time.localtime()) 1304576839.0

5) time.sleep(secs): The thread delays running for the specified time. The unit is seconds.

6) time.clock(): Please note that this has different meanings on different systems. On UNIX systems, it returns the "process time", which is a floating point number (timestamp) expressed in seconds. In WINDOWS, the first call returns the actual time the process is running. The calls after the second time are the running time since the first call to the present. (It is actually based on QueryPerformanceCounter() on WIN32, which is more accurate than millisecond representation)

import time if __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()

Running result:

clock1:3.35238137808e-006 clock2:1.00004944763 clock3:2.00012040636

The first clock( ) outputs the program running time
The second and third clock() outputs are the time intervals from the first clock

7) time.asctime([t]): represents a A tuple or struct_time of time is represented in this form: 'Sun Jun 20 23:21:05 1993'. If there are no parameters, time.localtime() will be passed in as a parameter.

>>> time.asctime() 'Thu May 5 14:55:43 2011'

8) time.ctime([secs]): Convert a timestamp (a floating point number calculated in seconds) into the form of time.asctime(). If the parameter is not given or is None, time.time() will be used as the parameter by default. Its function is equivalent to time.asctime(time.localtime(secs)).

>>> time.ctime() 'Thu May 5 14:58:09 2011' >>> time.ctime(time.time()) 'Thu May 5 14:58:39 2011' >>> time.ctime(1304579615) 'Thu May 5 15:13:35 2011'

9) time.strftime(format[, t]): Convert a tuple representing time or struct_time (such as returned by time.localtime() and time.gmtime()) into formatted Time string. If t is not specified, time.localtime() will be passed in. If any element in the tuple goes out of bounds, a ValueError will be thrown.

meanings

%A Local (Locale) Simplified Week name

%A Local Full Week name

%B Local Simplified Month Monthly

%b Local complete month name

%C Local corresponding date and time representation

%d's day of one month (01-31)

%H The hour of the day (24-hour clock, 00 - 23) 1 year Number of days (001 - 366)

%m Month (01 - 12)

%M Number of minutes (00 - 59)

%p 本地am或者pm的相应符 一

%S 秒(01 - 61) 二

%U 一年中的星期数。(00 - 53星期天是一个星期的开始。)

第一个星期天之前的所有天数都放在第0周。 三

%w 一个星期中的第几天(0 - 6,0是星期天) 三

%W 和%U基本相同,不同的是%W以星期一为一个星期的开始。

%x 本地相应日期

%X 本地相应时间

%y 去掉世纪的年份(00 - 99)

%Y 完整的年份

%Z 时区的名字(如果不存在为空字符)

%% ‘%’字符

备注:

“%p”只有与“%I”配合使用才有效果。文档中强调确实是0 - 61,而不是59,闰年秒占两秒(汗一个)。当使用strptime()函数时,只有当在这年中的周数和天数被确定的时候%U和%W才会被计算。

举个例子:

>>> time.strftime("%Y-%m-%d %X", time.localtime()) '2011-05-05 16:37:06'

10)time.strptime(string[, format]):把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。

>>> time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X') time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6, tm_wday=3, tm_yday=125, tm_isdst=-1)

在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。

最后,我们来对time模块进行一个总结。根据之前描述,在Python中共有三种表达方式:1)timestamp 2)tuple或者struct_time 3)格式化字符串。

它们之间的转化如图所示:

Basic introduction to time in python

The above is the detailed content of Basic introduction to time in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn