Skip to content

Commit

Permalink
Merge pull request #14 from stenjo/13-fixdate-handling-cant-subtract-…
Browse files Browse the repository at this point in the history
…offset-naive-and-offset-aware-datetimes

fix(date handling): can't subtract offset-naive and offset-aware date…
  • Loading branch information
stenjo authored Nov 4, 2023
2 parents 004309b + 025f8af commit e3b318a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
12 changes: 7 additions & 5 deletions dateHandling.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import datetime
from datetime import datetime, timezone, timedelta

def isNowInTimePeriod(startTime, endTime, nowTime):
if startTime < endTime:
Expand All @@ -7,13 +7,15 @@ def isNowInTimePeriod(startTime, endTime, nowTime):
#Over midnight:
return nowTime >= startTime or nowTime <= endTime

def dayText(event, today=datetime.datetime.today()):
def dayText(event, today=datetime.now(timezone.utc)):

weekday = ['Mandag', 'Tirsdag', 'Onsdag', 'Torsdag', 'Fredag','Lørdag','Søndag']
text = ''
dt = datetime.datetime.fromisoformat(event['start'].get('dateTime', event['start'].get('date')))
# today = datetime.datetime.today()
tomorrow = today + datetime.timedelta(1)
# The line `dt = datetime.datetime.fromisoformat(event['start'].get('dateTime',
# event['start'].get('date')))`
# is parsing the start time of an event from a dictionary called `event`.
dt = datetime.fromisoformat(event['start'].get('dateTime', event['start'].get('date')))
tomorrow = today + timedelta(1)
delta = dt - today

if dt.date() == today.date() :
Expand Down
26 changes: 26 additions & 0 deletions tests/dateHandling_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,29 @@ def test_dayText_friday_plus_one_week():

assert dayText(event, today) == "Fredag 03/11: Event summary"

def test_dayText_today_with_time_and_timezone():
event = {
'start': {
'date': '2023-10-23',
'dateTime': '2023-10-23T10:22:18+01:00'
},
'summary': 'Event summary'
}
today = datetime.datetime.strptime("2023-10-23T10:12 +0100", "%Y-%m-%dT%H:%M %z")
# print(today)
# dt: 2023-11-09 13:15:00+01:00
# today: 2023-11-04 18:54:37.512734

assert dayText(event, today) == "I dag: Event summary kl. 10:22"

def test_dayText_today_with_datetime_and_timezone():
event = {
'start': {
'date': datetime.datetime.today().strftime('%Y-%m-%d'),
'dateTime': datetime.datetime.today().strftime('%Y-%m-%d') + 'T10:22:18+01:00'
},
'summary': 'Event summary'
}

assert dayText(event) == "I dag: Event summary kl. 10:22"

0 comments on commit e3b318a

Please sign in to comment.