-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhowmanydays.py
executable file
·529 lines (415 loc) · 16.7 KB
/
howmanydays.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# https://github.com/dr-prodigy/python-holidays/blob/master/holidays/countries/austria.py
# https://stackoverflow.com/questions/2224742/most-recent-previous-business-day-in-python
# https://www.timeanddate.com/date/workdays.html?d1=14&m1=8&y1=2022&d2=1&m2=11&y2=2030
# From: Sonntag, 14. August 2022, 11:00:00
# To: Freitag, 1. November 2030, 00:00:00
# Result: 3000 days, 13 hours, 0 minutes and 0 seconds
# The duration is 3000 days, 13 hours, 0 minutes and 0 seconds
# Or 8 years, 2 months, 17 days, 13 hours
# Or 98 months, 17 days, 13 hours
# Alternative time units
# 3000 days, 13 hours, 0 minutes and 0 seconds can be converted to one of these units:
# 259 246 800 seconds
# 4 320 780 minutes
# 72 013 hours
# 3000 days (rounded down)
# 428 weeks (rounded down)
# Result: 2050 days
# 3001 calendar days – 951 days skipped:
# Excluded 428 Saturdays
# Excluded 429 Sundays
# Excluded 94 holidays:
# October 2022–December 2022: 60 days included
# Year 2023: 247 days included
# Year 2024: 252 days included
# Year 2025: 249 days included
# Year 2026: 250 days included
# Year 2027: 251 days included
# Year 2028: 248 days included
# Year 2029: 249 days included
# January 2030–September 2030: 188 days included
#
# pli, 14.08.2022, initial prog
# pli, 01.05.2023, calculation of vacation days, incl. aliquote nb of days for broken years
# pli, 07.01.2025, add ATZ calculation
#---------------------------------------------------------
# install dependencies
# sudo apt-get install python3-dateutil
# sudo apt-get install python3-pandas
# sudo pip3 install holidays
import sys, os
import getopt
import locale
from time import strftime, localtime
from datetime import datetime, timedelta, date
from dateutil.relativedelta import relativedelta
from dateutil.rrule import rrule, DAILY
from dateutil.parser import parse
import pandas as pd
import numpy as np
import holidays
import math
import json
# format day string
date_fmt = '%Y-%m-%d %H:%M:%S'
date_fmt2 = '%A, %d. %B %Y, %H:%M:%S'
# default Urlaubstage
URLAUBS_TAGE = 25
# default Krankenstand
KRANK_TAGE = 5
# default week mask
WEEKMASK = [1,1,1,1,1,0,0]
# geplanter Pensionsantritt
RETIRE_DATE = '2030-11-01 00:00:00'
# HL->Wien->HL
KM_PER_DAY = 110
# price per week
FUEL_PER_WEEK = 50
# de_AT.ISO8859-1
locale.setlocale(locale.LC_ALL, "")
# only for debugging
DEBUG = False
#--------------------------------------------------------------------------------
def yordinal(year, month, day):
return ((year-1)*365 + (year-1)//4 - (year-1)//100 + (year-1)//400
+ [ 0,31,59,90,120,151,181,212,243,273,304,334][month - 1]
+ day
+ int(((year%4==0 and year%100!=0) or year%400==0) and month > 2))
def ydaysBetweenDates(date1,date2):
y1 = date1.year
m1 = date1.month
d1 = date1.day
y2 = date2.year
m2 = date2.month
d2 = date2.day
x = date.toordinal(date1)
y = date.toordinal(date2)
return int(y-x)
#---------------------------------------------------------------------------------------------
def calc_interval(date_now, rt_date, vac_days, sick_days, atz=5):
# start date
dts = datetime.strptime(date_now, date_fmt)
# end date
dte = datetime.strptime(rt_date, date_fmt)
dtdiff = dte - dts
# Arbeitstage
#-----------------------------------------------------------
date1 = pd.to_datetime(dts,format=date_fmt).date()
date2 = pd.to_datetime(dte,format=date_fmt).date()
# get austrian holidays
at_holidays = []
at_vac = holidays.AT()
# iterate over range, build holiday list
for dt in rrule(DAILY, dtstart=dts, until=dte):
if dt in at_vac:
at_holidays.append(dt.strftime("%Y-%m-%d"))
# workdays MO-FR
# e.g.: 1519
bdays = np.busday_count( date1 , date2)
# exclude holidays
# e.g.: 1460
bdays_no_vac = np.busday_count( date1 , date2, holidays = at_holidays)
# convert parameter to int or list
try:
atz = int(atz)
except:
atz = json.loads(atz)
weekmask = WEEKMASK
if isinstance(atz, list):
# weekmask from commandline
weekmask = atz
atz = len([i for i in weekmask if i == 1])
else:
# nb of working days from commandline
if atz == 4:
# Fri free
weekmask = [1,1,1,1,0,0,0]
elif atz == 3:
# Mon, Fri free
weekmask = [0,1,1,1,0,0,0]
elif atz == 2:
# Mon, Thu, Fri free
weekmask = [0,1,1,0,0,0,0]
atz_days = np.busday_count( date1 , date2, weekmask = weekmask, holidays = at_holidays)
# calc vacation days
# start till end of year
dts_ye = datetime.strptime(str(dts.year)+'-12-31 00:00:00', date_fmt)
# start of last year till end
dte_yb = datetime.strptime(str(dte.year)+'-01-01 00:00:00', date_fmt)
# calc diffs
dt_first = dts_ye - dts
dt_last = dte - dte_yb
# aliquote vac days for first and last year
vac_part1 = math.ceil(vac_days/365*dt_first.days)
vac_part2 = math.ceil(vac_days/365*dt_last.days)
# aliquote sick leave days for first and last year
sick_part1 = math.ceil(sick_days/365*dt_first.days)
sick_part2 = math.ceil(sick_days/365*dt_last.days)
# nb of years for the rest of the timespan
mid_diff = relativedelta(dte_yb,dts_ye)
mid_years = mid_diff.years
# sum up vacation days
nb_vac_days = int(vac_part1 + vac_part2 + mid_years*vac_days)
# sum up sick leave days
nb_sick_days = int(sick_part1 + sick_part2 + mid_years*sick_days)
#nb_netto_days = int(bdays_no_vac)-int(nb_vac_days)-int(nb_sick_days)
nb_netto_days = int(atz_days)-int(nb_vac_days)-int(nb_sick_days)
# return list
# 0: dts ... start date
# 1: dte ... end date
# 2: dtdiff ... differenc of the two dates
# 3: bdays ... businessdays
# 4: bdays_no_vac ... working days (5 days a week)
# 5: atz_days ... working days during ATZ
# 6: nb_vac_days ... vacation days
# 7: nb_sick_days ... estimated sick days
# 8: nb_netto_days ... net working days
result = [dts, dte, dtdiff, bdays, bdays_no_vac, atz_days, nb_vac_days, nb_sick_days, nb_netto_days]
return result
#---------------------------------------------------------------------------------------------
def calc_dates(date_now, rt_date, vac_days, sick_days, mode=1, atz=5, atzdate='', costs=False):
# convert parameter to int or list
try:
atz = int(atz)
except:
atz = json.loads(atz)
weekmask = WEEKMASK
if isinstance(atz, list):
# weekmask from commandline
weekmask = atz
atz = len([i for i in weekmask if i == 1])
# calculate results
if atzdate != '':
calc1 = calc_interval(date_now, atzdate, vac_days, sick_days)
calc2 = calc_interval(atzdate, rt_date, vac_days, sick_days, atz)
if DEBUG:
print("DEBUG: calc1: %s" % calc1)
print("DEBUG: calc2: %s" % calc2)
dts = calc1[0]
dte = calc2[1]
dtdiff = calc1[2] + calc2[2]
bdays = calc1[3] + calc2[3]
bdays_no_vac = calc1[4] + calc2[4]
atz_days = calc1[5] + calc2[5]
# estimation
nb_vac_days = calc1[6] + calc2[6]
nb_sick_days = calc1[7] + calc2[7]
nb_netto_days = calc1[8] + calc2[8]
else:
calc1 = calc_interval(date_now, rt_date, vac_days, sick_days, atz)
if DEBUG:
print("DEBUG: calc1: %s" % calc1)
dts = calc1[0]
dte = calc1[1]
dtdiff = calc1[2]
bdays = calc1[3]
bdays_no_vac = calc1[4]
atz_days = calc1[5]
# estimation
nb_vac_days = calc1[6]
nb_sick_days = calc1[7]
nb_netto_days = calc1[8]
nb_km = nb_netto_days * KM_PER_DAY
fuel_costs = nb_netto_days/5*FUEL_PER_WEEK
if DEBUG:
print("DEBUG: atz : %s" % atz)
print("DEBUG: weekmask : %s" % weekmask)
print("DEBUG: bdays : %s" % bdays)
print("DEBUG: bdays_no_vac: %s" % bdays_no_vac)
print("DEBUG: atz_days : %s" % atz_days)
#-----------------------------------------------------------
# print result
# some statistics
# netto dauer
reldiff = relativedelta(dte,dts)
years = reldiff.years
months = reldiff.months
days = reldiff.days
hours = reldiff.hours
minutes = reldiff.minutes
seconds = reldiff.seconds
# dauer in unterschiedlichen Groeßen
# dtdiff.days
g_months = years * 12 + months
#g_days = ydaysBetweenDates(dts,dte)
g_days = dtdiff.days
g_hours = g_days * 24 + hours
g_minutes = g_hours * 60 + minutes
g_seconds = g_minutes * 60 + seconds
# quick and dirty_ without locales
gf_months = "{:,}".format(g_months).replace(',', '.')
gf_weeks = "{:,}".format(g_days // 7).replace(',', '.')
gf_week_days = "{:,}".format(g_days % 7).replace(',', '.')
gf_days = "{:,}".format(g_days).replace(',', '.')
gf_hours = "{:,}".format(g_hours).replace(',', '.')
gf_minutes = "{:,}".format(g_minutes).replace(',', '.')
gf_seconds = "{:,}".format(g_seconds).replace(',', '.')
gf_nb_km = "{:,}".format(nb_km).replace(',', '.')
gf_fuel_costs = "{:_}".format(fuel_costs).replace('.', ',').replace('_','.')
print("=================================================")
if mode == 1:
# mode 1 = table
print("Starttag : %s" % dts)
print("Zieltag : %s" % dte)
print("Dauer : %s" % reldiff)
#print("Dauer in Tagen : %7s" % dtdiff.days)
if DEBUG:
print("years : %s" % years)
print("months : %s" % months)
print("days : %s" % days)
print("hours : %s" % hours)
print()
print("gesamt Monate : %9s" % gf_months)
print("gesamt Wochen : %9s" % gf_weeks)
print("gesamt Tage : %9s" % gf_days)
print("gesamt Stunden : %9s" % gf_hours)
print("gesamt Minuten : %9s" % gf_minutes)
if costs:
print("\ngesamt km : %9s" % (gf_nb_km))
print("gesamt Benzinkosten: %11s" % (gf_fuel_costs))
print()
print("Urlaubstage p/a : %9s" % vac_days)
print("gesamt Urlaubst. : %9s" % nb_vac_days)
print("gesch. Krankenst. : %9s" % nb_sick_days)
print("Werktage(MO-FR) : %9s" % bdays)
if atz < 5:
print("Arbeitstage (ATZ) : %9s (ATZ = %s)" % (atz_days, atz))
else:
print("Arbeitstage : %9s" % bdays_no_vac)
print("------------------------------")
print("netto Arbeitstage : %9s" % int(nb_netto_days))
print("------------------------------")
if mode == 2:
# mode 2 = similar to www.timeanddate.com
print("Starttag: %s" % dts.strftime(date_fmt2))
print("Zieltag : %s" % dte.strftime(date_fmt2))
print("Ergebnis: %s Tage, %s Stunden, %s Minuten und %s Sekunden\n" % (dtdiff.days, hours, minutes, seconds))
print("Oder %s Jahre, %s Monate, %s Tage, %s Stunden" % (years, months, days, hours))
print("Oder %s Monate, %s Tage, %s Stunden" % (g_months, days, hours))
print("Oder %s Wochen, %s Tage\n" % (gf_weeks, gf_week_days))
print("%s Tage, %s Stunden, %s Minuten und %s Sekunden sind:" % (dtdiff.days, hours, minutes, seconds))
print(" %12s Sekunden" % (gf_seconds))
print(" %12s Minuten" % (gf_minutes))
print(" %12s Stunden" % (gf_hours))
print()
saso = dtdiff.days-bdays
nwda = dtdiff.days-bdays_no_vac
fday = nwda-saso
print("%s Tage bis Ende, sind:" % (dtdiff.days))
print(" %4s Werktage(MO-FR)" % bdays)
print(" - %4s Wochenenden(SA-SO)" % saso)
print(" - %4s Feiertage" % fday)
if atz < 5:
print(" = %4s Arbeitstage (ATZ = %s)" % (atz_days, atz))
else:
print(" = %4s Arbeitstage" % bdays_no_vac)
print(" - %4s Urlaubstage (%s p/a)" % (nb_vac_days, vac_days))
print(" - %4s Krankenstandstage (gesch.) (%s p/a)" % (nb_sick_days, sick_days))
print("------------------------------")
print(" %4s netto Arbeitstage" % int(nb_netto_days))
print("------------------------------")
if costs:
print("\n-- KOSTEN ---------------------------")
print("%7s km bis Ende" % (gf_nb_km))
print("%9s € voraussicht. Benzinkosten" % (gf_fuel_costs))
print("-------------------------------------")
#print("\n=================================================")
print()
return
#----------------------------------------------------------------------
def usage():
msg = "\nusage: " + os.path.basename(__file__) + " -e yyyy-mm-dd [-s yyyy-mm-dd] [-v n] [-m n] [-a n ] [ -c ]\n"
msg += "\t\t-e|--enddate yyyy-mm-dd \t.... End date\n"
msg += "\t\t-s|--startdate yyyy-mm-dd \t.... Start date\n"
msg += "\t\t |--startatzdate yyyy-mm-dd \t.... Start ATZ date (if different from startdate)\n"
msg += "\t\t-v|--vacationdays n \t\t.... number of vacation days\n"
msg += "\t\t-a|--atz n \t\t\t.... atz workdays per week or weekmask (see np.busday_count)\n"
msg += "\t\t-m|--displaymode n \t\t.... displaymode [1|2]\n"
msg += "\t\t-c|--costs \t\t\t.... show costs\n\n"
msg += "\te.g.:\n"
msg += "\t " + os.path.basename(__file__) + " -e "+RETIRE_DATE[:-9]+"\n"
msg += "\t " + os.path.basename(__file__) + " -e "+RETIRE_DATE[:-9]+" -v 30\n"
msg += "\t " + os.path.basename(__file__) + " -e "+RETIRE_DATE[:-9]+" -s "+sdate[:-9]+" -v 30\n"
msg += "\t " + os.path.basename(__file__) + " -e "+RETIRE_DATE[:-9]+" -s "+sdate[:-9]+" -m 2 -v 30\n"
msg += "\t " + os.path.basename(__file__) + " -e "+RETIRE_DATE[:-9]+" -s "+sdate[:-9]+" -m 2 -a 2 --startatzdate 2026-11-01\n"
msg += "\t " + os.path.basename(__file__) + " -e "+RETIRE_DATE[:-9]+" -s "+sdate[:-9]+" -m 2 -a [0,1,1,1,0,0,0] --startatzdate 2026-11-01\n\n"
print(msg)
sys.exit(1)
#----------------------------------------------------------------------
#
#----------------------------------------------------------------------
if __name__ == "__main__":
# init
p_sdate = ''
p_edate = ''
p_satzdate = ''
p_vdays = 0
p_mode = 1
p_costs = False
p_atz = 5
# use current day as start unless -s is used
sdate = strftime(date_fmt, localtime())
satzdate = ''
# check for options
try:
opts, args = getopt.getopt(sys.argv[1:], "hs:e:v:m:a:t:c", ["help", "startdate=", "enddate=", "startatzdate=", "vacationdays=", "displaymode=", "atz=", "costs"])
except getopt.GetoptError as err:
# print help information and exit:
print(("%s" % str(err))) # will print something like "option -a not recognized"
usage()
for o, a in opts:
if o in ("-s", "--startdate"):
p_sdate = a
elif o in ("-e", "--endate"):
p_edate = a
elif o in ("-t", "--startatzdate"):
p_satzdate = a
elif o in ("-v", "--vacationdays"):
p_vdays = a
elif o in ("-a", "--atz"):
p_atz = a
elif o in ("-m", "--displaymode"):
p_mode = int(a)
elif o in ("-c", "--costs"):
p_costs = True
elif o in ("-h", "--help"):
usage()
else:
assert False, "unhandled option"
if p_sdate != '':
try:
if parse(str(p_sdate)):
sdate = p_sdate + ' 00:00:00'
except:
print("\nWARN: invalid start-date given\n")
usage()
if p_satzdate != '':
try:
if parse(str(p_satzdate)):
satzdate = p_satzdate + ' 00:00:00'
except:
print("\nWARN: invalid start-atz-date given\n")
usage()
if p_edate != '':
try:
if parse(str(p_edate)):
RETIRE_DATE = p_edate + ' 00:00:00'
except:
print("\nWARN: invalid end-date given\n")
usage()
else:
usage()
if p_vdays != 0:
if p_vdays.isdigit():
URLAUBS_TAGE = int(p_vdays)
else:
print("\nWARN: invalid number of vacaction days given\n")
usage()
if p_mode < 1:
p_mode = 1
if p_mode > 2:
p_mode = 2
print()
calc_dates(sdate, RETIRE_DATE, URLAUBS_TAGE, KRANK_TAGE, p_mode, p_atz, satzdate, p_costs)