Skip to content

Instantly share code, notes, and snippets.

@danesparza
Last active February 4, 2019 18:02
Show Gist options
  • Save danesparza/f55908f7b19ee98ce55f624e15a91779 to your computer and use it in GitHub Desktop.
Save danesparza/f55908f7b19ee98ce55f624e15a91779 to your computer and use it in GitHub Desktop.
Test of ical library
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/danesparza/ical"
"github.com/teambition/rrule-go"
)
func main() {
file, _ := os.Open("family.ics")
cal, err := ical.Parse(file, nil)
file.Close()
if err != nil {
log.Fatalf("Error with parse: %v", err)
}
// Spit out the number of events found:
fmt.Printf("Found %v events\n\n", len(cal.Events))
/*
// Here is what event dates look like:
fmt.Print("Event sample: \n")
fmt.Printf("Title: %v\n", cal.Events[5].Summary)
year, month, day := cal.Events[4].StartDate.Date()
fmt.Printf("Date: %v/%v/%v\n", int(month), day, year)
*/
// Set the date/time to a specific date (testing operation)
// filterDate := time.Date(2019, 2, 2, 0, 0, 0, 0, time.Local)
// Just set the date/time to now (normal operation)
filterDate := time.Now()
currentStartTime := Bod(filterDate)
fmt.Printf("Start time: %v\n", currentStartTime)
// Get the current end of day:
currentEndTime := Eod(filterDate)
fmt.Printf("End time: %v\n", currentEndTime)
fmt.Printf("\nEvents today: \n\n")
for _, event := range cal.Events {
// If we find a recurring event, flag it:
if event.RecurringRule != "" {
//fmt.Printf("%v -- Rule: %v \n", event.Summary, event.RecurringRule+";DTSTART="+event.StartDate.Format("20060102T150400"))
r, _ := rrule.StrToRRule(event.RecurringRule + ";DTSTART=" + event.StartDate.Format("20060102T150400"))
foundRecurrances := r.Between(currentStartTime, currentEndTime, true)
if len(foundRecurrances) > 0 {
fmt.Printf("*%v\n %+v\n", event.Summary, foundRecurrances)
}
}
// Display if:
// - Between (or equal to) start/end times (single-day event today)
if (event.StartDate.After(currentStartTime) || event.StartDate.Equal(currentStartTime)) && (event.EndDate.Before(currentEndTime) || event.EndDate.Equal(currentEndTime)) {
fmt.Printf("-%v\n", event.Summary)
}
// - Starts before start time and ends after start time (multi-day event already in progress)
if event.StartDate.Before(currentStartTime) && event.EndDate.After(currentStartTime) {
fmt.Printf("+%v\n", event.Summary)
}
// - Starts before end time and ends after end time (multi-day event starting today)
if event.StartDate.Before(currentEndTime) && event.EndDate.After(currentEndTime) {
fmt.Printf(".%v\n", event.Summary)
}
}
}
// Bod returns the 'beginning of the day' for the given time
func Bod(t time.Time) time.Time {
year, month, day := t.Date()
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}
// Eod returns the 'end of the day' for the given time
func Eod(t time.Time) time.Time {
// Add a day:
newDate := t.AddDate(0, 0, 1)
// Extract the information:
year, month, day := newDate.Date()
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment