Skip to content

Instantly share code, notes, and snippets.

@macndesign
Last active May 13, 2022 22:04
Show Gist options
  • Save macndesign/35a15b0bbc9339eefadb890c82c191b3 to your computer and use it in GitHub Desktop.
Save macndesign/35a15b0bbc9339eefadb890c82c191b3 to your computer and use it in GitHub Desktop.
from datetime import timedelta
from time import strftime, gmtime
import re
import os
FOLDER = 'files'
def get_paths():
return [os.path.join(FOLDER, f) for f in os.listdir(FOLDER)]
def extract_datetime(filepath):
regex = r'(?P<date>\d{4}-\d{2}-\d{2})--(?P<time>\d{2}:\d{2})'
match = re.search(regex, filepath)
return [match.group('date'), match.group('time')]
def sum_hours(hours):
total_secs = 0
for tm in hours:
time_parts = [int(s) for s in tm.split(':')]
total_secs += (time_parts[0] * 60 + time_parts[1]) * 60
total_secs, _ = divmod(total_secs, 60)
hr, min = divmod(total_secs, 60)
return "%d:%02d" % (hr, min)
def total_hours_at_files_folder():
paths = get_paths()
times = [extract_datetime(p)[1] for p in paths]
return sum_hours(times)
def sum_total_of_times_in_range(start_date, end_date):
# Start and end date in ISO format YYYY-MM-DD
paths = get_paths()
times = []
start_date_to_number = int(start_date.replace('-', ''))
end_date_to_number = int(end_date.replace('-', ''))
date_range = range(start_date_to_number, end_date_to_number + 1)
for path in paths:
date = extract_datetime(path)[0]
date_number = int(date.replace('-', ''))
if date_number in date_range:
time = extract_datetime(path)[1]
times.append(time)
return sum_hours(times)
def format_seconds_to_hours(seconds):
return strftime('%H:%M', gmtime(seconds))
def report(formatted_total_hours, formatted_start_work, formatted_start_lunch, formatted_end_lunch):
return 'Work duration: {}\nStart work: {}\nLunch time: {} - {}\nTotal hours (at files folder): {}'.format(
formatted_total_hours,
formatted_start_work,
formatted_start_lunch,
formatted_end_lunch,
total_hours_at_files_folder()
)
def build_filepath(total_hours):
filename = input('ISO Date format YYYY-MM-DD (e.g. 2021-05-01): ')
filepath = os.path.join(FOLDER, '{}--{}.txt'.format(filename, total_hours))
return filepath
def save_file(total_seconds, start_work_seconds, start_lunch_seconds, end_lunch_seconds):
formatted_total_hours = format_seconds_to_hours(total_seconds)
formatted_start_work = format_seconds_to_hours(start_work_seconds)
formatted_start_lunch = format_seconds_to_hours(start_lunch_seconds)
formatted_end_lunch = format_seconds_to_hours(end_lunch_seconds)
filepath = build_filepath(formatted_total_hours)
with open(filepath, "w") as f:
rep = report(
formatted_total_hours,
formatted_start_work,
formatted_start_lunch,
formatted_end_lunch
)
f.write(rep)
return rep
def worked_hours(start_work, end_work, start_lunch, end_lunch):
start_work = timedelta(hours=start_work[0], minutes=start_work[1])
end_work = timedelta(hours=end_work[0], minutes=end_work[1])
start_lunch = timedelta(hours=start_lunch[0], minutes=start_lunch[1])
end_lunch = timedelta(hours=end_lunch[0], minutes=end_lunch[1])
total_work_seconds = end_work.seconds - start_work.seconds
total_lunch_seconds = end_lunch.seconds - start_lunch.seconds
total_seconds = total_work_seconds - total_lunch_seconds
return save_file(
total_seconds,
start_work.seconds,
start_lunch.seconds,
end_lunch.seconds
)
start_work_hour = int(input('Start work hour: '))
start_work_minute = int(input('Start work minute: '))
print('---')
start_lunch_hour = int(input('Start lunch hour: '))
start_lunch_minute = int(input('Start lunch minute: '))
print('---')
end_lunch_hour = int(input('End lunch hour: '))
end_lunch_minute = int(input('End lunch minute: '))
print('---')
end_work_hour = int(input('End work hour: '))
end_work_minute = int(input('End work minute: '))
print('---')
print(
worked_hours(
[start_work_hour, start_work_minute],
[end_work_hour, end_work_minute],
[start_lunch_hour, start_lunch_minute],
[end_lunch_hour, end_lunch_minute]
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment