本帖最后由 q3125418 于 2024-7-26 11:39 编辑
设置退休时间:retirement_age = 65 (啊,太惨了)
设置 通知 Webhook URL:去企业微信创建一个机器人就能获取了
啊,什么,,,,,,,我tm![](https://static.52pojie.cn/static/image/smiley/mogu/xzl.gif)
[Python] 纯文本查看 复制代码 # -*- coding: utf8 -*-
# python >=3.8
import requests
from datetime import datetime, timedelta
# 固定的出生日期和退休年龄
birth_date_str = "2000/01/01"
retirement_age = 65
# 解析出生日期
birth_date = datetime.strptime(birth_date_str, "%Y/%m/%d")
# 计算退休日期
retirement_date = birth_date.replace(year=birth_date.year + retirement_age)
# 计算剩余时间
today = datetime.now()
time_difference = retirement_date - today
if time_difference.total_seconds() < 0:
result = "您已经达到了退休年龄。"
else:
years = retirement_date.year - today.year
months = retirement_date.month - today.month
days = retirement_date.day - today.day
hours = retirement_date.hour - today.hour
minutes = retirement_date.minute - today.minute
seconds = retirement_date.second - today.second
if seconds < 0:
seconds += 60
minutes -= 1
if minutes < 0:
minutes += 60
hours -= 1
if hours < 0:
hours += 24
days -= 1
if days < 0:
previous_month = retirement_date.replace(day=1) - timedelta(days=1)
days += previous_month.day
months -= 1
if months < 0:
months += 12
years -= 1
result = f"距离退休日期 {retirement_date.strftime('%Y/%m/%d')} 还有: {years} 年, {months} 月, {days} 天, {hours} 小时, {minutes} 分钟, {seconds} 秒。"
# 企业微信机器人 Webhook URL
weixin_webhook_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx"
# 推送到企业微信机器人
payload = {
"msgtype": "text",
"text": {
"content": result
}
}
response = requests.post(weixin_webhook_url, json=payload)
if response.status_code == 200:
print("消息推送成功")
else:
print(f"消息推送失败,状态码: {response.status_code}, 返回信息: {response.text}")
|