Skip to content

Instantly share code, notes, and snippets.

@jorgeteixe
Created June 17, 2021 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorgeteixe/992276efebcccd69e8d5e98fd51aeba6 to your computer and use it in GitHub Desktop.
Save jorgeteixe/992276efebcccd69e8d5e98fd51aeba6 to your computer and use it in GitHub Desktop.
Calculates at what time I have to leave the office given an start time and the pending time.
#!/usr/bin/env python3
import sys
time_spaces = ['AM', 'PM']
def main(argv):
try:
start = argv[0]
time_space = argv[1]
pending = argv[2]
except:
print('usage: hours <start_hour> <AM|PM> <pending_time>\n')
sys.exit(2)
try:
start_h, start_m = map(int, start.split(':'))
pending_h, pending_m = map(int, pending.split(':'))
except:
print('format for dates is: HH:MM\n')
sys.exit(2)
try:
time_spaces.index(time_space)
except:
print('format for timespace is AM or PM\n')
sys.exit(2)
finish_h = start_h + pending_h
finish_m = start_m + pending_m
hours_plus, finish_m = divmod(finish_m, 60)
finish_h += hours_plus
hours_plus, finish_h = divmod(finish_h, 12)
if finish_h + finish_m == 0:
finish_h = 12
try:
finish_time_space = time_spaces[time_spaces.index(time_space) + hours_plus]
except:
finish_time_space = time_spaces[0]
print(f'{finish_h:02}:{finish_m:02} {finish_time_space}\n')
if __name__ == "__main__":
main(sys.argv[1:])
@jorgeteixe
Copy link
Author

To install:
Copy to /usr/local/bin without the extension.
Should look like this: /usr/local/bin/hours
To add execution permissions: chmod +x /usr/local/bin/hours
Run directly in your terminal by typing: hours

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment