0. 前言
这个是我最近决定开设的栏目,用于记录一些突发奇想,或者闲来无事,用Python开发制作的小工具小游戏,希望能在练习中提升一下自己!
1. 概念
拿着月薪都在想日薪多少多少,但那些大佬就不一样了,用秒来计算每秒挣了多少钱!
那我们能不能在Python中实现一下?
2. 开发
先贴代码:
import base64
import os
import time
from datetime import datetime, timedelta
import chinese_calendar as holiday # 判断是否为中国节假日 目前版本2024
############ 配置区 ############
entry = "2022/12/10" # 入职日期
mySalary = b'NjAwMA==' # 薪资 base64编码格式 此处为6000
workTime = "09:00" # 上班时间
closeTime = "18:00" # 下班时间
myHoliday = 4 # 月休息天数 考虑大小月
###############################
today = datetime.now()
MINIMUM_WAGE = 22.2 # 广州最低时薪
class timeFormat():
def sec2Text(self, sec: int):
m, s = divmod(sec, 60)
h, m = divmod(m, 60)
return "%02d小时%02d分钟%02d秒" % (h, m, s)
def time2Sec(self, time):
h, m, s = map(int, time.split(":"))
return h * 3600 + m * 60 + s
def getMonthDays(self, year, month) -> int:
if month in [1, 3, 5, 7, 8, 10, 12]:
return 31
elif month in [4, 6, 9, 11]:
return 30
elif (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return 29
else:
return 28
def whatDay(self, date: datetime) -> dict:
if holiday.is_workday(date.date()):
return {"type": 1, "name": "工作日"}
elif holiday.is_holiday(date.date()):
return {"type": 2, "name": "休息日"}
def welcome():
delta = today-datetime.strptime(entry, "%Y/%m/%d")
def time2Text():
if today.hour < 6:
return "凌晨好"
if 6 < today.hour < 12:
return "早上好"
if 12 <= today.hour < 18:
return "下午好"
if 18 <= today.hour < 24:
return "晚上好"
msg = f"""
___ ___ __ _____ _____ _ _
/ _ \ / _ \ / /|_ _/ ____| | | |
| (_) | (_) |/ /_ | || | | | | |
\__, |\__, | '_ \ | || | | | | |
/ / / /| (_) || || |____| |__| |
/_/ /_/ \___/_____\_____|\____/
{time2Text()}
今天是你上班的第{delta.days}天"""
print(msg)
time.sleep(3)
os.system("cls")
def loop():
tool = timeFormat()
salary = int(base64.b64decode(mySalary).decode("utf-8")) # 薪资
start = timedelta(hours=int(workTime.split(
":")[0]), minutes=int(workTime.split(":")[1])) # 上班时间
end = timedelta(hours=int(closeTime.split(
":")[0]), minutes=int(closeTime.split(":")[1])) # 下班时间
offwork = False # 下班
while True:
now = datetime.now() # 当前时间
# 时薪:月工资收入÷(月计薪天数×8小时)
myWage = salary/((tool.getMonthDays(now.year, now.month)-myHoliday)*8)
deltaNow = timedelta(
hours=now.hour, minutes=now.minute, seconds=now.second)
schedule = deltaNow - start # 当前进度
todayGet = salary/tool.getMonthDays(now.year, now.month) # 今日应得
todayProcess = schedule.seconds/(end-start).seconds # 今日进度
if deltaNow >= start and deltaNow < end: # 上班时间内
msg = f"""
当前时间:{datetime.now().strftime("%Y/%m/%d %H:%M:%S")}
你已经上了:{(tool.sec2Text(schedule.seconds))}
还有 {tool.sec2Text((end-deltaNow).seconds)} 下班
今天已经挣了:{format(todayProcess*todayGet,'.2f')}元 还需努力挣:{format((1-todayProcess)*todayGet,'.2f')}元"""
print(msg)
try:
time.sleep(1)
except:
exit()
if deltaNow >= end:
dayType = tool.whatDay(now)
if offwork == False:
offwork = True
msg = f"""
____ ______ ________ ______ _____ _ __
/ __ \| ____| ____\ \ / / __ \| __ \| |/ /
| | | | |__ | |__ \ \ /\ / / | | | |__) | ' /
| | | | __| | __| \ \/ \/ /| | | | _ /| <
| |__| | | | | \ /\ / | |__| | | \ \| . \
\____/|_| |_| \/ \/ \____/|_| \_\_|\_\
下班啦!
"""
print(msg)
try:
time.sleep(3)
except:
exit()
else:
msg = f"""
当前时间:{datetime.now().strftime("%Y/%m/%d %H:%M:%S")}
你已经上了:{(tool.sec2Text(schedule.seconds))}
已经下班 {tool.sec2Text(deltaNow.seconds - end.seconds)} 啦!
今天是:{dayType['name']}
"""
overtime = ((MINIMUM_WAGE if myWage < MINIMUM_WAGE else myWage) /
3600)*(deltaNow.seconds-end.seconds)
if dayType['type'] == 1:
msg += f"\n工作日加班费:{format(overtime*1.5,'.2f')}元"
elif dayType['type'] == 2:
msg += f"休息日加班费:{format(overtime*2,'.2f')}元\n\n法定节假日加班费:{format(overtime*3,'.2f')}元"
print(msg)
try:
time.sleep(1)
except:
exit()
os.system("cls")
def main():
welcome()
loop()
if __name__ == "__main__":
main()
在代码中,我们实现了上班欢迎语,下班提示语,以及薪资的计算。
启动脚本后,我们就能看到今天是当牛马的第xx天?
也能根据当前时间判断早上好中午好晚上好。
其中,配置区我们配置了入职日期,base64编码的工资(以免有人看到心生嫉妒),上下班时间,以及月休息天。实际脚本运行中,还考虑了大小月(28天/30天/31天)的问题,让秒薪计算的更加精确。
欢迎语结束后实际运行效果如下:
真是越来越有盼头了!
下班后会有大横幅提示。
接下来重头戏!
脚本使用了chinese_calendar库判断当前是否为国内周末休息日、调休以及法定节假日,并按照设定的当地最低日薪计算当前的加班费。如果公司有加班费的相关补贴还好,不然就是实际亏损的薪资!?
(看看谁没有加班费~)
脚本所有功能介绍完毕,希望能帮到你。
发表回复