Skip to content

Commit

Permalink
Update is_AM and is_PM
Browse files Browse the repository at this point in the history
  • Loading branch information
PhongT16 committed Sep 12, 2023
1 parent 44208a1 commit d80fd86
Showing 1 changed file with 56 additions and 36 deletions.
92 changes: 56 additions & 36 deletions SimpleEvent.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from dataclasses import dataclass
from datetime import datetime
from datetime import timedelta
import yaml
import os
import utils


configs = utils.get_configurations()
AM_config = configs['AM_config']
PM_config = configs['PM_config']
# AM_config = configs['AM_config']
# PM_config = configs['PM_config']
start_of_workday = configs['start_of_work_day']
end_of_workday = configs['end_of_work_day']
start_of_lunch = configs['start_of_lunch']
end_of_lunch = configs['end_of_lunch']
duration = configs['duration']

@dataclass
class SimpleEvent:
Expand All @@ -34,6 +37,7 @@ def create_event_for_individual_calendars(cls, event, start_date, end_date, net_
'''

events = []
# TODO: Change the variable names to make them more specific
start = SimpleEvent.make_datetime(event['start']['dateTime'])
end = SimpleEvent.make_datetime(event['end']['dateTime'])

Expand All @@ -55,6 +59,7 @@ def create_event_for_individual_calendars(cls, event, start_date, end_date, net_
new_end = start + timedelta(days=i)

# Adjust the time so that we can create an accurate subject for the split up event
# Manipulating the time for the simple event here
if (i == 0):
new_end = new_end.replace(hour=23,minute=59,second=59)
elif (i == dates_interval.days):
Expand Down Expand Up @@ -139,12 +144,28 @@ def is_event_valid(user_start, user_end, start, end):
if (user_start <= start and user_end > start) and (SimpleEvent.is_AM(start, end) or SimpleEvent.is_PM(start,end)):
return True
return False


@staticmethod
def make_datetime(date):
'''
Create and return a datetime object given a date (YYYY-MM-DD)
@staticmethod
# TODO: Have the user input the time values in the yaml file
# is_AM assumes that start and end are on the same day, so it's just checking their times
Args:
date (str): The given date in the form YYYY-MM-DD
Returns:
A datetime object representing the date
'''

if "T" in date:
# The format of date is 2023-03-18T00:00:00.0000000
# The split is to remove the microseconds b/c datetime only take microseconds up to 6 digits,
# but the response date format has 7 digits for microseconds
return datetime.strptime(date.split('.')[0], "%Y-%m-%dT%H:%M:%S")
else:
return datetime.strptime(date, "%Y-%m-%d")

@staticmethod
def is_AM(start, end):
'''
Verify whether the event duration fit within the AM specification
Expand All @@ -158,14 +179,23 @@ def is_AM(start, end):
False if not
'''

if ((start.hour * 60) + start.minute <= AM_config['start'] and (end.hour * 60) + end.minute >= AM_config['end']):
start_time = (start.hour * 60) + start.minute
end_time= (end.hour * 60) + end.minute

if start_time > start_of_lunch or end_time < start_of_workday:
return False

if start_time < start_of_workday:
start_time= start_of_workday

if end_time > start_of_lunch:
end_time = start_of_lunch

if end_time - start_time >= duration:
return True
return False

@staticmethod
# start and end are datetime objects
# TODO: Have the user input the time values in the yaml file
# is_PM assumes that start and end are on the same day, so it's just checking their times

@staticmethod
def is_PM(start, end):
'''
Verify whether the event duration fit within the PM specification
Expand All @@ -177,33 +207,23 @@ def is_PM(start, end):
Returns:
True if the event is PM
False if not
'''

if ((start.hour * 60) + start.minute <= PM_config['start'] and (end.hour * 60) + end.minute >= PM_config['end']):
return True
return False


@staticmethod
def make_datetime(date):
'''
Create and return a datetime object given a date (YYYY-MM-DD)

start_time = (start.hour * 60) + start.minute
end_time= (end.hour * 60) + end.minute

Args:
date (str): The given date in the form YYYY-MM-DD
if start_time > end_of_workday or end_time < end_of_lunch:
return False

Returns:
A datetime object representing the date
'''
if start_time < end_of_lunch:
start_time= end_of_lunch

if "T" in date:
# The format of date is 2023-03-18T00:00:00.0000000
# The split is to remove the microseconds b/c datetime only take microseconds up to 6 digits,
# but the response date format has 7 digits for microseconds
return datetime.strptime(date.split('.')[0], "%Y-%m-%dT%H:%M:%S")
else:
return datetime.strptime(date, "%Y-%m-%d")
if end_time > end_of_workday:
end_time = end_of_workday

if end_time - start_time >= duration:
return True
return False

# event = {
# 'isPrivate': False,
Expand Down

0 comments on commit d80fd86

Please sign in to comment.