Skip to content

Instantly share code, notes, and snippets.

@johnbumgarner
Created August 29, 2022 13:13
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 johnbumgarner/717877031ec23f4d127094798e79ac18 to your computer and use it in GitHub Desktop.
Save johnbumgarner/717877031ec23f4d127094798e79ac18 to your computer and use it in GitHub Desktop.
from datetime import datetime
def validate_workday(date_to_check):
# valid work days
# Monday = 1
# Tuesday = 2
# Wednesday = 3
# Thursday = 4
# Friday = 5
work_days = [1, 2, 3, 4, 5]
if date_to_check.isoweekday() in work_days:
return True
else:
return False
def reformatted_date_value(date_string):
date_time_obj = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f')
date_time_iso_string = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f').strftime('%Y, %m, %d, %H, %M, %S, %f')
return date_time_obj, date_time_iso_string
def get_work_hours(reformatted_date):
date_string_split = reformatted_date.split(',')
year = int(date_string_split[0])
month = int(date_string_split[1])
day = int(date_string_split[2])
hour = int(date_string_split[3])
minute = int(date_string_split[4])
microsecond = int(date_string_split[5])
date_stamp = datetime(year=year,
month=month,
day=day,
hour=hour,
minute=minute,
microsecond=microsecond)
work_start_time = date_stamp.replace(hour=9, minute=0, second=0, microsecond=0)
work_end_time = date_stamp.replace(hour=18, minute=0, second=0, microsecond=0)
return work_start_time, work_end_time
def time_diff(date_stamp, work_start_time, work_end_time):
if work_start_time <= date_stamp <= work_end_time:
print('The time is inside normal work hours')
start_time_diff = date_stamp - work_start_time
end_time_diff = work_end_time - date_stamp
print(f'Workday started {start_time_diff} hours ago')
print(f'Workday ends in {end_time_diff}')
print('\n')
else:
print('The time is outside normal work hours')
print('\n')
date_strings = ['2022-08-02 17:02:07.645241', '2022-08-06 17:02:07.645241']
for date_string in date_strings:
reformatted_dates = reformatted_date_value(date_string)
is_work_day = validate_workday(reformatted_dates[0])
if is_work_day:
print(f'Date stamp: {date_string}')
work_hours = get_work_hours(reformatted_dates[1])
start_time = work_hours[0]
end_time = work_hours[1]
date_var = reformatted_dates[0]
time_diff(date_var, start_time, end_time)
else:
print(f'Date stamp: {date_string}')
print('Not a work day')
print('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment