Preface
In development work, we often need to use date and time, such as:
as log information The content output
Calculate the execution time of a certain function
Name the name of a log file with date
Record or display the publishing or modification time of an article
Others
Python provides multiple functions for date and Built-in modules for time operations: time module, datetime module and calendar module. The time module is implemented by calling the C library, so some methods may not be called on some platforms, but most of the interfaces it provides are basically consistent with the C standard library time.h. Compared with the time module, the interface provided by the datetime module is more intuitive, easier to use, and more powerful.
1. Explanation of related terms
UTC time Coordinated Universal Time, also known as Greenwich Astronomy Time, Universal Time. Corresponding to UTC time is the local time of each time zone. The time in East N zone is N hours earlier than UTC time, so UTC time + N hours is the local time of East N zone; while the time in West N zone is N hours later than UTC time. hours, that is, UTC time - N hours is the local time in West N zone; China is in East 8 zone, so it is 8 hours earlier than UTC time and can be expressed as UTC+8.
epoch time represents the starting point of the beginning of time; it is a specific time, and the value of this time point is different on different platforms. For Unix, epoch time is 1970-01 -01 00:00:00 UTC.
timestamp (timestamp) Also known as Unix time or POSIX time; it is a time representation that represents 0:00:00 on January 1, 1970 GMT The number of milliseconds that have elapsed since the beginning. Its value is of type float. However, the related methods of some programming languages return the number of seconds (this is the case with Python). This requires reading the documentation of the method. It should be noted that the timestamp is a difference value, and its value has nothing to do with the time zone.
2. The expression form of time
The common time expression form is:
Time stamp
Formatted time string
There are other time representations in Python:
Time.struct_time of the time module
The datetime class of the datetime module
About the datetime class of the datetime module It will be explained in detail below, but here we will briefly talk about time.struct_time.
time.struct_time contains the following attributes:
Attribute name | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
tm_year | Year, such as 2017 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tm_mon | Month, the value range is [1, 12] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tm_mday | The day of the month, the value range is [1-31] | ##3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
hours, the value range is [0-23] | 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
minutes, the value range is [0, 59] | 5 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
seconds, the value range is [0, 61] | 6 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The day of the week, the value range is [0-6], 0 means Monday | 7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The day of the year, the value range is [1, 366] | ##8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Whether it is daylight saving time, the possible values are: 0, 1 or -1 |
There are two ways to obtain attribute values:
It should be noted that each attribute of the struct_time instance is read-only and cannot be modified. 3. Time module The time module is mainly used for time access and conversion. This module provides various time-related functions. 1. Function list
object date datetime time timedelta tzinfo timezone 2. Constants defined in the datetime module
Definition of datetime.date class class datetime.date(year, month, day) year, month and day are all required parameters, and the value range of each parameter is:
实例 >>> import time >>> from datetime import date >>> >>> date.max datetime.date(9999, 12, 31) >>> date.min datetime.date(1, 1, 1) >>> date.resolution datetime.timedelta(1) >>> date.today() datetime.date(2017, 2, 4) >>> date.fromtimestamp(time.time()) datetime.date(2017, 2, 4) >>> >>> d = date.today() >>> d.year 2017 >>> d.month 2 >>> d.day 4 >>> d.replace(2016) datetime.date(2016, 2, 4) >>> d.replace(2016, 3) >>> d.replace(2016, 3, 2) datetime.date(2016, 3, 2) >>> d.timetuple() time.struct_time(tm_year=2017, tm_mon=2, tm_mday=4, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=35, tm_isdst=-1) >>> d.toordinal() 736364 >>> d.weekday() 5 >>> d.isoweekday() 6 >>> d.isocalendar() (2017, 5, 6) >>> d.isoformat() '2017-02-04' >>> d.ctime() 'Sat Feb 4 00:00:00 2017' >>> d.strftime('%Y/%m/%d') '2017/02/04' 4. datetime.time类 time类的定义 class datetime.time(hour, [minute[, second, [microsecond[, tzinfo]]]]) hour为必须参数,其他为可选参数。各参数的取值范围为:
类方法和属性
对象方法和属性
实例 >>> from datetime import time >>> >>> time.max datetime.time(23, 59, 59, 999999) >>> time.min datetime.time(0, 0) >>> time.resolution datetime.timedelta(0, 0, 1) >>> >>> t = time(20, 5, 40, 8888) >>> t.hour 20 >>> t.minute 5 >>> t.second 40 >>> t.microsecond 8888 >>> t.tzinfo >>> >>> t.replace(21) datetime.time(21, 5, 40, 8888) >>> t.isoformat() '20:05:40.008888' >>> t.strftime('%H%M%S') '200540' >>> t.strftime('%H%M%S.%f') '200540.008888' 5. datetime.datetime类 datetime类的定义 复制代码 代码如下:
year, month 和 day是必须要传递的参数, tzinfo可以是None或tzinfo子类的实例。 各参数的取值范围为:
If a parameter exceeds these ranges, a ValueError exception will be caused. Class methods and properties
实例方法和属性
实例 >>> import datetime >>> >>> datetime.timedelta(365).total_seconds() # 一年包含的总秒数 31536000.0 >>> dt = datetime.datetime.now() >>> dt + datetime.timedelta(3) # 3天后 datetime.datetime(2017, 2, 8, 9, 39, 40, 102821) >>> dt + datetime.timedelta(-3) # 3天前 datetime.datetime(2017, 2, 2, 9, 39, 40, 102821) >>> dt + datetime.timedelta(hours=3) # 3小时后 datetime.datetime(2017, 2, 5, 12, 39, 40, 102821) >>> dt + datetime.timedelta(hours=-3) # 3小时前 datetime.datetime(2017, 2, 5, 6, 39, 40, 102821) >>> dt + datetime.timedelta(hours=3, seconds=30) # 3小时30秒后 datetime.datetime(2017, 2, 5, 12, 40, 10, 102821) 五、时间格式码 time模块的struct_time以及datetime模块的datetime、date、time类都提供了strftime()方法,该方法可 以输出一个指定格式的时间字符串。具体格式由一系列的格式码(格式字符)组成,Python最终调用的是各个平台C库的strftme()函数,因此各平台对全套格式码的支持会有所不同,具体情况需要参考该平台上的strftime(3)文档。下面列出了C标准(1989版)要求的所有格式码,它们在所有标准C实现的平台上都可以工作: 六、总结 那么Python中处理时间时,使用time模块好,还是用datetime模块好呢?就我个人而言,datetime模块基本上可以满足需要,且用起来确实比较方便。对于time模块,我只是在取当前时间的时间戳时会用到time.time()方法,当然也可以通过datetime.datetime.now().timestamp()来获取,只是显得复杂一点。我觉得还是看个人习惯吧,没有什么绝对的好坏之分。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHP中文网。 更多Python date and time processing module (date and datetime)相关文章请关注PHP中文网! |

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools