3

I'm calling the Office 365 Calendar API and receiving the start date of the event back in the following format:

2016-05-20T08:00:00Z

I want to check when an event is near e.g. 5 minutes time, and use something along the lines of the following solution: [Python - Do (something) when event is near

What's the best way of determining the number of seconds between my event in the date/time format above and the current time now (in UTC taking into account daylight savings)?

Community
  • 1
  • 1
user2823030
  • 109
  • 2
  • 6
  • Convert the current datetime to epoch and convert the date time of your even to epoch then do event epoch - current epoch and check if it is smaller then 60 * 5 . Now i don't know the appropriate syntax for python that is why i wrote a comment but that is the basic idea. – Maantje May 22 '16 at 16:21

3 Answers3

4

Here's stdlib-only solution (combination of @tschale's and @minocha's answers):

#!/usr/bin/env python
from datetime import datetime, timedelta

then = datetime.strptime('2016-05-20T08:00:00Z', '%Y-%m-%dT%H:%M:%SZ')
now = datetime.utcnow()
if abs(now - then) < timedelta(minutes=5):
    "within 5 minutes"

Related: Find if 24 hrs have passed between datetimes.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
3

The time you get needs to be cnoverted to datetime format -

>>import datetime
>>import iso8601
>>datetime_variable = iso8601.parse_date('2016-05-20T08:00:00Z')
>>(datetime.replace(tzinfo=None) - datetime.datetime.utcnow()).seconds
>>56086

You can do this if the response always contains the timezone as 'Z' which translates to UTC. since the normal datetime.datetime object is timezone naive you have to strip the other object of the tzinfo too, but this shouldn't be a problem since both are in UTC.

Hope this works

minocha
  • 1,043
  • 1
  • 12
  • 26
  • @user2823030 datetime.datetime.utcnow() as mentioned in my answer takes care of daylight savings too. Also since the time you retrieve and this time are in utc, the absolute difference will give you the seconds. – minocha May 22 '16 at 17:01
  • 1
    the given utc time (`Z`) could be parsed using only stdlib as shown in [@tschale's answer](http://stackoverflow.com/a/37376782/4279). – jfs May 23 '16 at 14:03
  • @J.F.Sebastian that is good. but i wanted to use this because of less dependence on libraries like pytz. doing `tzinfo=None` does it in this user's case since his timezone is always UTC – minocha May 23 '16 at 15:10
1

In python there is the datetime module. You can use pytz to get a timezone specific now-time. However, if you can you should work with UTC time and only convert it to the local time for output to be read by humans.

from datetime import datetime
import pytz
time = '2016-05-20T08:00:00Z'
formatted_time = datetime.strptime(time, '%Y-%m-%dT%H:%M:%SZ')
tz = pytz.timezone('Europe/Berlin')
time_now = datetime.now(tz)
time_delta = formatted_time - time_now()
delta_in_seconds = time_delta.total_seconds()

The format parameters for datetime.strptime are specified datetime.strftime

tschale
  • 975
  • 1
  • 18
  • 34
  • This works well but the time is an hour out due to daylight savings. How can I resolve that thanks? – user2823030 May 22 '16 at 16:39
  • Just found out about `pytz`. With it you can get timezone specific datetimes: `import pytz`, `berlin = pytz.timezone('Europe/Berlin')`, `time_now = datetime.now(berlin)`. Does this help you? – tschale May 22 '16 at 16:48
  • @user2823030: `Z` means UTC time. To find whether the given time is within 5 minutes (assuming the clocks are in sync): `abs(formatted_time - datetime.utcnow()) < timedelta(minutes=5)` (similar to [@minocha's answer](http://stackoverflow.com/a/37376778/4279)). See [Find if 24 hrs have passed between datetimes - Python](http://stackoverflow.com/q/26313520/4279) – jfs May 23 '16 at 14:02