编写代码实现一个装饰器,该装饰器具有为被装饰的函数实现统计函数运行时间的功能
编写代码实现效果:
time
-
获取 UNIX 时间戳,输出(带上单位,秒?微秒?毫秒?),使用 ctime 获取本地日期时间字符串,输出
-
由 UNIX 时间戳,使用 gmtime 获取 UTC/GMT struct_time 对象,输出
-
由 2 获得的 struct_time 对象,使用 asctime 获取简单格式化的日期时间字符串,输出
-
由 2 获得的 struct_time 对象,使用 strftime 获取格式为 ‘Sat Mar 28 22:24:24 2016’(星期,月,日,时,分,秒,年) 的日期时间字符串,输出
-
由 4 获得的日期时间字符串,使用 strptime 获取 struct_time 对象,输出
-
由 5 获得的 struct_time 对象,使用 calendar.timegm() 获取 UNIX 时间戳,输出(带上单位,秒?微秒?毫秒?),输出
-
由 UNIX 时间戳,使用 localtime 获取 本地时间 struct_time 对象,输出
-
由 7 获得的 struct_time 对象,使用 asctime 获取简单格式化的日期时间字符串,输出
-
由 7 获得的 struct_time 对象,使用 strftime 获取格式为 ‘Sat Mar 28 22:24:24 2016’ (星期,月,日,时,分,秒,年)的日期时间字符串,输出
-
由 9 获得的日期时间字符串,使用 strptime 获取 struct_time 对象,输出
-
由 10 获得的 struct_time 对象,使用 mktime 获取 UNIX 时间戳,输出(带上单位,秒?微秒?毫秒?),输出
-
计算 本地时间 和 UTC 时间的时差,输出(带上单位,x 小时 x 分 x 秒),输出
datetime
- 使用 now 函数,获取当前的 datetime,输出
- 由 1 获得的 datetime 对象,获取当前的时间戳,输出(带单位)
- 由 2 获得的时间戳,获得本地(不是UTC) datetime,输出
- 生成 1 小时 40 分 的 timedelta 对象,输出
- 由 3 获得的 datetime 对象和 4 获得的 timedelta 对象,计算 1 小时 40 分后的 datetime 对象,输出
- 将 5 获得的新的 datetime 对象,设置时区为北京时间,使用函数 astimezone 转换为京东时区,输出
import time
import calendar
import datetime
from datetime import timezone, timedelta
from functools import wraps
def time_this_function(func):
# 作为装饰器使用,返回函数执行需要花费的时间
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
start_str = str(start)
print("TIME模块")
print("1 当前时间戳为: " + start_str + " 秒")
print("ctime 获取本地日期时间字符串 : %s" % time.ctime(start))
struct_time = time.gmtime(start)
print("2 gmtime获取UTC/GMT struct_time对象 :", struct_time)
print("3 使用 asctime 获取简单格式化的日期时间字符串 :", time.asctime(struct_time))
print("4 使用 strftime 获取日期时间字符串 :", time.strftime("%a %b %d %H %M %S %Y", struct_time))
struct_time_0 = time.strptime(time.strftime("%a %b %d %H %M %S %Y", struct_time), "%a %b %d %H %M %S %Y")
print("5 使用 strptime 获取 struct_time 对象 :", struct_time_0)
print("6 使用 calendar.timegm() 获取 UNIX 时间戳 :", str(calendar.timegm(struct_time_0)) + "秒")
struct_time_1 = time.localtime(start)
print("7 使用 localtime 获取 本地时间 struct_time 对象 :", struct_time_1, "秒")
print("8 使用 asctime 获取简单格式化的日期时间字符串 :", time.asctime(struct_time_1))
print("9 使用 strftime 获取日期时间字符串 :", time.strftime("%a %b %d %H %M %S %Y", struct_time_1))
struct_time_2 = time.strptime(time.strftime("%a %b %d %H %M %S %Y", struct_time_1), "%a %b %d %H %M %S %Y")
print("10 使用 strptime 获取 struct_time 对象 :", struct_time_2)
print("11 使用 mktime 获取 UNIX 时间戳:", time.mktime(struct_time_2), "秒")
local_time = datetime.datetime.fromtimestamp(start)
utc_time = datetime.datetime.utcfromtimestamp(start)
offset = local_time - utc_time
print("12 计算 本地时间 和 UTC 时间的时差:", offset)
print("DATETIME模块")
datetime_now = datetime.datetime.now()
print("1 使用 now 函数,获取当前的 datetime", datetime_now)
datetime_now_ = datetime_now.strftime("%a %b %d %H %M %S %Y")
struct_time_3 = time.strptime(datetime_now_, "%a %b %d %H %M %S %Y")
time_l = time.mktime(struct_time_3)
print("2 由 1 获得的 datetime 对象,获取当前的时间戳", time_l, "秒")
print("3 由 2 获得的时间戳,获得本地 datetime", datetime.datetime.utcfromtimestamp(time_l))
delta = datetime.timedelta(hours=1, minutes=40)
print("4 生成 1 小时 40 分 的 timedelta 对象", delta)
new_time = datetime.datetime.utcfromtimestamp(time_l) + delta
print("5 计算 1 小时 40 分后的 datetime 对象", new_time)
utc_time = new_time.astimezone(timezone(timedelta(hours=-8)))
tokyo = timezone(timedelta(hours=9))
jan_dt = utc_time.astimezone(tokyo)
print("6 使用函数 astimezone 转换为东京时区", jan_dt)
result = func(*args, **kwargs)
end = time.time()
print("装饰器测试")
print("计算函数运行时间 ", func.__name__, end - start, "秒")
return result
return wrapper
if __name__ == '__main__':
@time_this_function
def count(n):
while n > 0:
time.sleep(0.1)
n += -1
count(10)
本文详细展示了如何使用Python编写一个装饰器,用于统计函数执行时间,并演示了时间戳与日期时间的各种操作,包括UTC/GMT、本地时间、不同格式的日期转换。通过实例,介绍了datetime模块的now函数、时间戳处理和时区转换。
1876

被折叠的 条评论
为什么被折叠?



