payment/payment_backend/utils/datetime.py

35 lines
1.1 KiB
Python
Raw Normal View History

import datetime
def current():
return datetime.datetime.now()
def current_timestamp():
datetime.datetime.now().timestamp()
def is_time_difference_greater_than(timestamp1, timestamp2, hours=0, minutes=0, seconds=0):
"""
判断两个时间戳的时间差是否大于指定的小时分钟和秒数
参数:
timestamp1 (int): 第一个时间戳
timestamp2 (int): 第二个时间戳
hours (int): 要比较的小时数默认是0小时
minutes (int): 要比较的分钟数默认是0分钟
seconds (int): 要比较的秒数默认是0秒
返回:
bool: 如果时间差大于指定的小时分钟和秒数返回True否则返回False
"""
# 将时间戳转换为 datetime 对象
time1 = datetime.fromtimestamp(timestamp1)
time2 = datetime.fromtimestamp(timestamp2)
# 计算时间差
time_difference = abs(time2 - time1)
# 计算指定的时间差值
threshold = datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds)
# 判断时间差是否大于指定的时间
return time_difference > threshold