Files
lm_code/test1.py

28 lines
1.0 KiB
Python
Raw Normal View History

2025-10-31 11:33:34 +08:00
import time
2025-11-04 12:10:36 +08:00
import datetime
2025-10-31 11:33:34 +08:00
2025-11-04 12:10:36 +08:00
# 获取当前时间戳
current_timestamp = time.time()
# 将当前时间戳转换为 datetime 对象
current_datetime = datetime.datetime.fromtimestamp(current_timestamp)
2025-10-31 11:33:34 +08:00
2025-11-04 12:10:36 +08:00
# 计算距离当前时间最近的整点或 30 分时刻
if current_datetime.minute < 30:
target_datetime = current_datetime.replace(minute=0, second=0, microsecond=0)
else:
target_datetime = current_datetime.replace(minute=30, second=0, microsecond=0)
2025-10-31 11:33:34 +08:00
2025-11-04 12:10:36 +08:00
# 将目标 datetime 对象转换为时间戳
target_timestamp = target_datetime.timestamp()
2025-10-31 11:33:34 +08:00
2025-11-04 12:10:36 +08:00
print(f"当前时间戳: {current_timestamp}")
print(f"目标时间戳: {target_timestamp}")
# 进行时间戳比对
if current_timestamp == target_timestamp:
print("当前时间就是目标时间。")
elif current_timestamp < target_timestamp:
print(f"当前时间早于目标时间,还需等待 {target_timestamp - current_timestamp} 秒。")
else:
print(f"当前时间晚于目标时间,已经过去了 {current_timestamp - target_timestamp} 秒。")