Skip to content

Commit

Permalink
Merge pull request #11 from stenjo/10-feat-vis-dato-for-hendelser-mer…
Browse files Browse the repository at this point in the history
…-enn-1-uke-fram

feat: vis dato for hendelser mer enn 1 uke fram
  • Loading branch information
stenjo authored Oct 28, 2023
2 parents f660d00 + a0939cc commit 16a714f
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/*
**/__pycache__/*
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
33 changes: 33 additions & 0 deletions dateHandling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import datetime

def isNowInTimePeriod(startTime, endTime, nowTime):
if startTime < endTime:
return nowTime >= startTime and nowTime <= endTime
else:
#Over midnight:
return nowTime >= startTime or nowTime <= endTime

def dayText(event, today=datetime.datetime.today()):

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)
delta = dt - today

if dt.date() == today.date() :
text = text + 'I dag: '
elif dt.date() == tomorrow.date() :
text = text + 'I morgen: '
elif delta.days > 6:
text = text + weekday[dt.weekday()] + ' ' + dt.strftime('%d/%m') + ': '
else :
text = text + weekday[dt.weekday()] + ': '

text = text + event['summary']

if dt.hour > 0:
text = text + dt.strftime(' kl. %H:%M')

return text
33 changes: 3 additions & 30 deletions middag.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,12 @@
from luma.core.legacy.font import proportional, CP437_FONT, TINY_FONT, SINCLAIR_FONT, LCD_FONT
from backports.datetime_fromisoformat import MonkeyPatch
from pathlib import Path
from dateHandling import isNowInTimePeriod, dayText

MonkeyPatch.patch_fromisoformat()
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']

def isNowInTimePeriod(startTime, endTime, nowTime):
if startTime < endTime:
return nowTime >= startTime and nowTime <= endTime
else:
#Over midnight:
return nowTime >= startTime or nowTime <= endTime

def dayText(event):

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)

if dt.date() == today.date() :
text = text + 'I dag: '
elif dt.date() == tomorrow.date() :
text = text + 'I morgen: '
else :
text = text + weekday[dt.weekday()] + ': '

text = text + event['summary']

if dt.hour > 0:
text = text + dt.strftime(' kl. %H:%M')

return text

def main(argv):
loop = None
Expand All @@ -64,7 +37,7 @@ def main(argv):
sys.exit()
elif opt in ("-m", "--mode"):
if arg in ('loop'):
loop = True
loop = True

print (loop)
"""Shows basic usage of the Google Calendar API.
Expand Down Expand Up @@ -138,4 +111,4 @@ def main(argv):
time.sleep(2)

if __name__ == '__main__':
main(sys.argv[1:])
main(sys.argv[1:])
Empty file added tests/__init__.py
Empty file.
69 changes: 69 additions & 0 deletions tests/dateHandling_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import datetime

from dateHandling import dayText, isNowInTimePeriod

def test_isNowInTimePeriod():
startTime = datetime.datetime.strptime("2023-10-22T10:12", "%Y-%m-%dT%H:%M")
endTime = datetime.datetime.strptime("2023-10-25T10:12", "%Y-%m-%dT%H:%M")
today = datetime.datetime.strptime("2023-10-23T10:12", "%Y-%m-%dT%H:%M")
before = datetime.datetime.strptime("2023-10-22T10:11", "%Y-%m-%dT%H:%M")
after = datetime.datetime.strptime("2023-10-25T10:13", "%Y-%m-%dT%H:%M")

assert isNowInTimePeriod(startTime, endTime, today) == True
assert isNowInTimePeriod(startTime, endTime, before) == False
assert isNowInTimePeriod(startTime, endTime, after) == False


today = datetime.datetime.strptime("2023-10-23T10:12", "%Y-%m-%dT%H:%M")

def test_dayText_tomorrow():
event = {
'start': {
'date': '2023-10-24'
},
'summary': 'Event summary'
}

assert dayText(event, today) == "I morgen: Event summary"

def test_dayText_today():
event = {
'start': {
'date': '2023-10-23'
},
'summary': 'Event summary'
}

assert dayText(event, today) == "I dag: Event summary"

def test_dayText_today_with_time():
event = {
'start': {
'date': '2023-10-23',
'dateTime': '2023-10-23T10:22:18'
},
'summary': 'Event summary'
}

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

def test_dayText_friday():
event = {
'start': {
'date': '2023-10-27'
},
'summary': 'Event summary'
}

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

def test_dayText_friday_plus_one_week():
event = {
'start': {
'date': '2023-11-03'
},
'summary': 'Event summary'
}

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

0 comments on commit 16a714f

Please sign in to comment.