Advertisement
CftfKees2x

WorkSpanCalculator.py

Oct 29th, 2021
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.91 KB | None | 0 0
  1. from datetime import datetime
  2. import sys
  3.  
  4. START_DAY = datetime(2021, 7, 3)
  5. WORK_SPAN_LENGTH = 4
  6. DAYS_OFF_PER_SPAN = 1
  7.  
  8. def main() -> None:
  9.     if helpMessageIsRequested():
  10.         displayHelpMessage()
  11.         return
  12.    
  13.     startDate, span, rest = readSysArgs()
  14.     result, remainder = isBreakDay(datetime.now(), startDate, span, rest)
  15.     printResults(result, remainder)
  16.  
  17.  
  18. def helpMessageIsRequested() -> bool:
  19.     if (len(sys.argv) > 1) and (sys.argv[1] == "-h"):
  20.         return True
  21.     return False
  22.  
  23.  
  24. def displayHelpMessage() -> None:
  25.     print("===Work Span Calculator===\n"+\
  26.         "Determines whether a given day is a work or rest day for a given work rhythm.\n"+\
  27.         "Default values: 4 day cycles with 1 rest day per cycle, starting on 2021-10-30.\n"+\
  28.         "Rest days are consecutive at the start of each cycle.\n\nSYNTAX:\n"+\
  29.         "python WorkSpanCalculator.py [cycle length] [rest days] [start date]")
  30.  
  31.  
  32. def readSysArgs() -> (datetime, int, int):
  33.     startDate = START_DAY
  34.     span = WORK_SPAN_LENGTH
  35.     rest = DAYS_OFF_PER_SPAN
  36.    
  37.     if len(sys.argv) > 1:
  38.         span = int(sys.argv[1])
  39.         if len(sys.argv) > 2:
  40.             rest = int(sys.argv[2])
  41.             if len(sys.argv) > 3:
  42.                 startDate = datetime.strptime(sys.argv[3], "%Y-%m-%d")
  43.     return (startDate, span, rest)
  44.  
  45.  
  46. def printResults(result: bool, remainder: int) -> None:
  47.     if result:
  48.         print("Today is a break day.")
  49.     else:
  50.         print("Today is a work day. The next rest day is in " + str(remainder) + " day(s).")
  51.  
  52.  
  53. def getDaysSinceStart(targetDate: datetime, startDate: datetime = START_DAY) -> int:
  54.     return (targetDate.date() - startDate.date()).days
  55.  
  56.  
  57. def isBreakDay(targetDate: datetime, startDate: datetime = START_DAY, spanLength: int = WORK_SPAN_LENGTH, restDayCount: int = DAYS_OFF_PER_SPAN) -> (bool, int):
  58.     cycleProgress = getDaysSinceStart(targetDate, startDate) % spanLength
  59.     isRestDay = cycleProgress < restDayCount
  60.     return (isRestDay, spanLength - cycleProgress)
  61.  
  62.  
  63. if __name__ == "__main__":
  64.     main()
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement