Profy
Profy

Reputation: 111

Time() library asking for unreachable format

While doing test on google agenda i wanted to make a test to see if an event is currently happening. My approch was to check if the current time (given by the time librairy)

datetime.now().time()

And then using the description comparing it using once more the time librairy

print(event['start'].get('dateTime', event['start'].get('date'))) # Will output 2021-02-09T10:45:00+01:00

Then i parse this to obtain :

event_start = (start.split("T", 1)[1]).split("+")[0][:-3].replace(":", ",")
print(event_start) # Will output 10,45 which is the beginning of the event

Finally i use this function i found on stackoverflow to see if we are in event by checking is current time is between the start and the end of the event

def is_time_between(begin_time, end_time, check_time=None):
check_time = check_time or datetime.now().time()
if begin_time < end_time:
    return check_time >= begin_time and check_time <= end_time
else:
    return check_time >= begin_time or check_time <= end_time

My problem lies in the input format. To compare the time the format must me be XX:XX:XX which you can obtain using time(X,X):

#example
print(time(10,45)) # Will output 10:45:00

But when i use it on the previous event_start i obtain

time(event_start)
#TypeError: an integer is required (got type str)

I know that you are going to tell me to convert it using int but i will loose the data after the coma 10.1 -> 10 and a float is not accepted

Finally my main question is : How using the googe api input can you determine if you are during the current event using a boolean as an output considering that the input is :

{'dateTime': '2021-02-09T12:15:00+01:00', 'timeZone': 'Europe/Paris'}, 'end': {'dateTime': '2021-02-09T14:15:00+01:00', 'timeZone': 'Europe/Paris'}

Upvotes: 0

Views: 68

Answers (1)

Brimbo
Brimbo

Reputation: 156

First of, generally 2021-02-09T10:45:00+01:00 does not mean that it lasted an hour but that you have a timezone offset of +1 hour, which is the timezone of many central european countries (CET). So maybe you want to get a different representation. Are you sure that the +01:00 really stands for the duration?

If the +01:00 truely is you duration, you can achieve what you want to get, by simplying doing string comparison if you got rid of the +01:00. So for example, you can do this:

import datetime 

checktime = str(datetime.datetime.now()).replace(' ', 'T')
eventtime = event['start'].get('dateTime', event['start'].get('date')) # Will output 2021-02-09T10:45:00+01:00

starttime = eventtime.split('+')[0]
duration = eventtime.split('+')[1].split(':')
duration = datetime.timedelta(hours=duration[0], minutes=duration[1])
starttime_obj = datetime.datetime.strptime(starttime, '%Y-%m-%dT%H:%M:%S')
endtime = str(starttime_obj + duration)

happened_between = checktime > starttime and checktime < endtime

Note that this is not the cleanest solution, a better way would be to use datetime objects as @DeepSpace pointed out.

Update

After you confirmed that it is in fact the offset, you could instead do the following:

import datetime
import pytz

starttime_str = event['start'].get('dateTime', event['start'].get('date')) # Will output 2021-02-09T10:45:00+01:00
endtime_str = event['end'].get('dateTime', event['end'].get('date')) 

checktime = pytz.utc.localize(datetime.datetime.utcnow())
starttime = datetime.datetime.strptime(starttime_str, '%Y-%m-%dT%H:%M:%S%z')
endtime = datetime.datetime.strptime(endtime_str, '%Y-%m-%dT%H:%M:%S%z')

happened_between = checktime > starttime and checktime < endtime

Upvotes: 1

Related Questions