Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add ical file support for daily planner #104

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
extend tests and skip non Z timezone dates
  • Loading branch information
ipapast committed Jan 3, 2024
commit 3a6aee9c38095b27422b0eac5901250ee6fc7e1f
14 changes: 14 additions & 0 deletions app/components/icsparser/example_calendar.ics
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ UID:[email protected]
SUMMARY:Daily Morning Briefing
END:VEVENT

BEGIN:VEVENT
DTSTART:20240106T140000Z
DTEND:20240106T150000Z
UID:[email protected]
SUMMARY:Afternoon Meeting
END:VEVENT

BEGIN:VEVENT
DTSTART;TZID=Asia/Bangkok:20240107T150000
DTEND;TZID=Asia/Bangkok:20240107T160000
UID:[email protected]
SUMMARY:Evening Workshop
END:VEVENT

BEGIN:VEVENT
DTSTART:20240106T120000Z
DTEND:20240106T130000Z
Expand Down
11 changes: 8 additions & 3 deletions app/components/icsparser/icsparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package icsparser
import (
"fmt"
"os"
"strings"
"time"

ical "github.com/arran4/golang-ical"
Expand Down Expand Up @@ -30,16 +31,20 @@ func ParseICSFile(filePath string) ([]Event, error) {
var events []Event
for _, component := range calendar.Events() {
dateStr := component.GetProperty(ical.ComponentPropertyDtStart).Value

// Skip events that don't end with 'Z' (not in UTC format)
if !strings.HasSuffix(dateStr, "Z") {
continue
}

parsedDate, err := time.Parse("20060102T150405Z", dateStr)
if err != nil {
return nil, fmt.Errorf("could not convert Event.Date: %w", err)
}
localLocation := time.Now().Location() // to present events based on currently generated location
localLocation := time.Now().Location() // to present events based on current location
localTime := parsedDate.In(localLocation)
formattedTime := localTime.Format("15:04")
formattedDate := localTime.Format("02-01-2006")
fmt.Println(formattedDate)
fmt.Println(formattedTime)
event := Event{
Date: parsedDate,
FormattedDate: formattedDate,
Expand Down
19 changes: 18 additions & 1 deletion app/components/icsparser/icsparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,28 @@ func TestParseICSFile(t *testing.T) {
events, err := ParseICSFile(filePath)
assert.NoError(t, err)
assert.NotNil(t, events)
assert.Equal(t, 21, len(events), "Number of events does not match expected length")
assert.Equal(t, 22, len(events), "Number of events does not match expected length")

firstEvent := events[0]
assert.Equal(t, "Daily Morning Briefing", firstEvent.Summary, "Summary does not match")

assert.Equal(t, "05-01-2024", firstEvent.FormattedDate, "FormattedDate does not match")
assert.Equal(t, "09:00", firstEvent.FormattedTime, "FormattedTime does not match")

secondEvent := events[1]
expectedFormattedTime := "14:00" // this works on GMT timezone, test is brittle but needed to test it
expectedFormattedDate := "06-01-2024"

assert.Equal(t, "Afternoon Meeting", secondEvent.Summary, "Summary does not match")
assert.Equal(t, expectedFormattedDate, secondEvent.FormattedDate, "FormattedDate does not match")
assert.Equal(t, expectedFormattedTime, secondEvent.FormattedTime, "FormattedTime does not match")

thirdEvent := events[2] // the third event is using timezone based location, so we skip it. we can not currently handle it

expectedFormattedTimeForThirdEvent := "12:00" // Local time (GMT) in GMT+7
expectedFormattedDateForThirdEvent := "06-01-2024"

assert.Equal(t, "Weekly Yoga and Wellness Workshop", thirdEvent.Summary, "Summary does not match")
assert.Equal(t, expectedFormattedDateForThirdEvent, thirdEvent.FormattedDate, "FormattedDate does not match")
assert.Equal(t, expectedFormattedTimeForThirdEvent, thirdEvent.FormattedTime, "FormattedTime does not match")
}