secmi / utils /times.py
tgey's picture
APP test
2145052
from datetime import datetime, timedelta, time
import locale
from babel.dates import format_date, get_day_names
locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
def calculate_night_time(start_time: time, end_time: time, pause_time: time, date: datetime) -> float:
start_datetime = datetime.combine(date, start_time)
end_datetime = datetime.combine(date, end_time)
night_start = datetime.combine(date, datetime.strptime("21:00:00", "%H:%M:%S").time())
night_end = datetime.combine(date, datetime.strptime("06:00:00", "%H:%M:%S").time())
night_worktime = timedelta()
if end_datetime < start_datetime :
end_datetime += timedelta(days=1)
night_end += timedelta(days=1)
night_worktime = min(end_datetime, night_end) - max(start_datetime, night_start)
else:
if end_datetime >= night_start:
night_worktime = max(end_datetime, night_end ) - max(start_datetime, night_start)
else:
night_worktime = min(end_datetime, night_end) - min(start_datetime, night_start)
night_worktime = max(night_worktime, timedelta())
pause_delta = timedelta(hours=pause_time.hour, minutes=pause_time.minute)
night_worktime -= pause_delta if night_worktime > pause_delta else night_worktime
total_seconds = night_worktime.total_seconds()
hours = float(total_seconds / 3600)
return hours
def calculate_work_time(start_time: time, end_time: time, pause_time: time, date: datetime) -> float:
start_datetime = datetime.combine(date, start_time)
end_datetime = datetime.combine(date, end_time)
if end_datetime < start_datetime:
end_datetime += timedelta(days=1)
worktime = end_datetime - start_datetime
pause_delta = timedelta(hours=pause_time.hour, minutes=pause_time.minute)
worktime -= pause_delta if worktime > pause_delta else worktime
total_seconds = worktime.total_seconds()
hours = float(total_seconds / 3600)
return hours
def date_to_week_number(date: datetime) -> int:
return date.isocalendar()[1]
def date_to_week_day(date: datetime) -> int:
# weekday_english = date.strftime("%A")
weekday_french = format_date(date, 'EEEE', locale='fr')
return weekday_french