# PythonTime **Repository Path**: qyydeep/python-time ## Basic Information - **Project Name**: PythonTime - **Description**: Python 中 time 模块的学习部分 - **Primary Language**: Python - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-11-20 - **Last Updated**: 2025-11-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # PythonTime ## time时间模块 时间模块是 Python 中重要的一部分,在很多项目中都会用到。 ## 时间的3大数据类型 1. 时间戳(timestamp) 2. struct_time元组 3. 格式化的时间字符串(Format string) ![时间格式化符号](/images/TimeFormatSymbol.png) 1. %p 只有和 %I 配合使用才有效果 2. 文档中强调确实是 0~61,闰年占秒。 3. 使用 strptime()函数时,只有这一年的周数和天数确定时 %U 和 %w 才会计算。 9个元素:年、月、日、时、分、秒、一年中第几周、一年中第几天、是否为夏令时。 ![元组元素](/images/tupleProperty.png) ## 时间 3 个类型可以相互转化 ![1](/images/threeTypeConversion1.png) ![2](/images/threeTypeConversion2.png) ## time 模块的方法 ### time()函数 ```python import time time.time() ``` 输出:时间戳 ### strftime()函数 接收时间元组,返回格式字符串 ### strptime()函数 接收一个时间字符串解析为时间元组,返回的是 struct_time元组对象。 ### localtime()函数 格式化时间戳为本地时间,返回 struct_time元组对象。包含 年、月、日、时、分、秒 ### sleep()函数 用于推迟调用线程的运行。没有返回值。 ### gmtime()函数 将一个时间戳转换为 UTC 时区的 struct_time元组对象。返回struct_time元组对象 ### mktime()函数 接收struct_time元组对象,返回秒数表示时间的浮点数。 ### asctime()函数 ### ctime()函数 返回一个时间字符串