time库的使用
时间: 2023-07-20 19:08:47 浏览: 45
time库是Python中用于处理时间的标准库,它提供了许多函数和类,可以用来获取当前时间、格式化时间、延迟执行等。
下面是一些常用的time库函数:
1. time.time(): 返回当前时间的时间戳,即从1970年1月1日午夜起到现在的秒数。
```python
import time
print(time.time())
```
2. time.sleep(s): 延迟执行程序,单位为秒。
```python
import time
print("开始执行")
time.sleep(5)
print("5秒后执行")
```
3. time.localtime(seconds): 将时间戳转换为本地时间,返回一个元组。
```python
import time
t = time.localtime(1629133265)
print(t)
```
4. time.strftime(format_string, t): 将时间元组转换为指定格式的字符串。
```python
import time
t = time.localtime()
print(time.strftime("%Y-%m-%d %H:%M:%S", t))
```
5. time.strptime(date_string, format_string): 将格式化的时间字符串转换为时间元组。
```python
import time
t = time.strptime("2021-08-16 10:05:25", "%Y-%m-%d %H:%M:%S")
print(t)
```
以上仅是time库的一些常用函数,更多的函数和用法可以查看Python官方文档。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)