Basic introduction to time in python
In Python, modules related to time processing include: time, datetime and calendar.
Related recommendations: "python video"
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)格式化字符串。
它们之间的转化如图所示:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
