Última atividade 10 months ago

此 Python 類別可根據《勞動基準法》第 38 條規定,計算勞工應享有的特休天數,適用於 HR、人資系統或勞工自行查詢特休日數,確保符合法規標準。

annual_leave_calculator.py Bruto
1class AnnualLeaveCalculator:
2 def __init__(self, employment_date, current_date):
3 """
4 初始化特休計算器
5 :param employment_date: 入職日期,格式為 'YYYY-MM-DD'
6 :param current_date: 目前日期,格式為 'YYYY-MM-DD'
7 """
8 self.employment_date = employment_date
9 self.current_date = current_date
10
11 def calculate_years_of_service(self):
12 """計算服務年資"""
13 from datetime import datetime
14 start_date = datetime.strptime(self.employment_date, '%Y-%m-%d')
15 end_date = datetime.strptime(self.current_date, '%Y-%m-%d')
16 delta = end_date - start_date
17 years_of_service = delta.days / 365.25 # 考慮閏年
18 return years_of_service
19
20 def calculate_annual_leave(self):
21 """根據服務年資計算特休天數"""
22 years = self.calculate_years_of_service()
23 if years < 0.5:
24 return 0
25 elif 0.5 <= years < 1:
26 return 3
27 elif 1 <= years < 2:
28 return 7
29 elif 2 <= years < 3:
30 return 10
31 elif 3 <= years < 5:
32 return 14
33 elif 5 <= years < 10:
34 return 15
35 else:
36 additional_days = min(int(years - 10), 15)
37 return 15 + additional_days
38
39# 測試範例
40if __name__ == "__main__":
41 calculator = AnnualLeaveCalculator('2016-08-08', '2025-02-07')
42 print(f"應享有的特休天數: {calculator.calculate_annual_leave()}")
43
44