Python中常见的一些模块(续)

时间time模块

在Python中,与时间处理有关的模块就包括:time,datetime以及calendar。这篇文章,主要讲解time模块!

# -*- coding: UTF-8 -*-
import time

(1)time()--返回当前时间的时间戳

print("calculate the Seconds form 1/1/1970 to now = ", time.time())
Python中常见的一些模块(续)

(2)sleep()--线程推迟指定的时间运行。单位为秒!

start_time = time.time()
time.sleep(1)
endtime = time.time()
print(endtime - start_time)
Python中常见的一些模块(续)

(3)asctime()--把一个表示时间的元组或者struct_time表示为这种形式:'Mon Jun 13 21:30:57 2022 '。如果没有参数,将会将time.localtime()作为参数传入!

#系统时间
print(time.asctime())
Python中常见的一些模块(续)

(4)localtime()--将一个时间戳转换为当前时区的struct_time。参数未提供,则以当前时间为准。

#时间戳
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
Python中常见的一些模块(续)

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章