python中的时间模块(time)

版权声明:转载请附此链接https://blog.csdn.net/qq_42393859 https://blog.csdn.net/qq_42393859/article/details/83574653

可以从源码中看到time有如下的方法:

Functions:
time() -- return current time in seconds since the Epoch as a float
clock() -- return CPU time since process start as a float
sleep() -- delay for a number of seconds given as a float
gmtime() -- convert seconds since Epoch to UTC tuple
localtime() -- convert seconds since Epoch to local time tuple
asctime() -- convert time tuple to string
ctime() -- convert time in seconds to string
mktime() -- convert local time tuple to seconds since Epoch
strftime() -- convert time tuple to string according to format specification
strptime() -- parse string to time tuple according to format specification
tzset() -- change the local timezone

以下是实际应用:

import time

#print(time.altzone)  # 返回格林威治西部的夏令时地区的偏移数
#print(time.asctime()) # 接受一个时间元组,并返回一个格式化之后的时间---时间字符串
#print(time.asctime(time.localtime()))

# print(time.clock()) # 第一次调用表示进程时间,第三次调用,表示第三次到第二次进程的时间
# time.sleep(1) #阻塞当前线程n秒
# print(time.clock())
# time.sleep(1)
# print(time.clock())
# time.sleep(2)
# print(time.clock())
#
# print(time.ctime()) # 返回当前时间的字符串
# print(time.time()) # 返回当前时间的时间戳---从1970年1月1日0点0分0秒到现在所经历的秒数
# print(time.ctime(time.time())) # ctime() 接受一个时间戳,转换成对应的时间字符串
#
# print(time.gmtime()) # 返回一个时间元组,时间为夏令时
# print(time.localtime()) # 返回一个时间元组,时间为正常时间
#
# print(time.mktime(time.localtime()))  # 接受时间元组,转换成时间戳
# print(time.strftime('%Y*%m*%d %H:%M:%S',time.localtime())) # 接受一个时间元组,按照指定的格式返回时间字符串
# print(time.strptime('2018-6-28 16:0:0','%Y-%m-%d %H:%M:%S')) # 接受一个含有时间信息的字符串,通过指定格式进行解析,并返回对应的时间元组

猜你喜欢

转载自blog.csdn.net/qq_42393859/article/details/83574653