Skip to content

Commit

Permalink
Implement generation of ics file from CLI args
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmorio committed Dec 12, 2022
1 parent 924cce0 commit 75a04b8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/kevinmorio/holidays2ical

go 1.19

require (
github.com/arran4/golang-ical v0.0.0-20221122102835-109346913e54 // indirect
github.com/google/uuid v1.3.0 // indirect
)
51 changes: 47 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ package main
import (
"flag"
"fmt"
"os"
"sort"
"strings"
"time"

ics "github.com/arran4/golang-ical"
"github.com/google/uuid"
)

/// Calculation of the easter date
Expand Down Expand Up @@ -37,6 +42,24 @@ type Holiday struct {
Date time.Time
}

func (h *Holiday) holidayToEvent(lang string) *ics.VEvent {
event := ics.NewEvent(strings.ToUpper(uuid.NewString()))
// event.SetAllDayStartAt(h.Date)
// event.SetAllDayEndAt(h.Date.AddDate(0, 0, 1))

event.SetProperty(ics.ComponentPropertyDtStart, h.Date.UTC().Format("20060102"), ics.WithValue(string(ics.ValueDataTypeDate)))
event.SetProperty(ics.ComponentPropertyDtEnd, h.Date.AddDate(0, 0, 1).UTC().Format("20060102"), ics.WithValue(string(ics.ValueDataTypeDate)))

// event.SetProperty(ics.ComponentPropertyDtStart, h.Date.UTC().Format("20060102"))
// event.SetProperty(ics.ComponentPropertyDtEnd, h.Date.AddDate(0, 0, 1).UTC().Format("20060102"))

event.SetTimeTransparency(ics.TransparencyTransparent)
event.SetSummary(h.Name[lang])
event.SetDtStampTime(time.Now())

return event
}

func NewYear(year int) Holiday {
return Holiday{
Name: map[string]string{
Expand Down Expand Up @@ -287,19 +310,39 @@ func HolidaysForYear(year int) []Holiday {
return holidays
}

var calendarName = map[string]string{
"de-DE": "Feiertage",
"en-US": "Holidays",
}

func main() {
fromYear := flag.Int("from", time.Now().Year(), "year to start from")
tillYear := flag.Int("till", time.Now().Year(), "year to end")
lang := flag.String("lang", "de-DE", "the language used for the holidays")

defaultOutfilePath := fmt.Sprintf("./%s.ics", calendarName[*lang])
outfilePath := flag.String("outfile", defaultOutfilePath, "the outfile of the calendar")

flag.Parse()

for year := *fromYear; year <= *tillYear; year++ {
fmt.Printf("---------------- YEAR %d ----------------\n", year)
cal := ics.NewCalendarFor("-//Kevin Morio//holidays2ics")
cal.SetCalscale("GREGORIAN")
cal.SetXWRCalName(calendarName[*lang])

for year := *fromYear; year <= *tillYear; year++ {
for _, holiday := range HolidaysForYear(year) {
fmt.Printf(" %s: %v\n", holiday.Name[*lang], holiday.Date)
cal.AddVEvent(holiday.holidayToEvent(*lang))
}
fmt.Printf("-------------------------------------------\n")
}

outfile, err := os.Create(*outfilePath)
if err != nil {
panic(err)
}
defer outfile.Close()

if err := cal.SerializeTo(outfile); err != nil {
panic(err)
}
fmt.Printf("Saved calendar to %s\n", *outfilePath)
}

0 comments on commit 75a04b8

Please sign in to comment.