github.com/wtfutil/wtf@v0.43.0/modules/gcal/cal_event.go (about) 1 package gcal 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/wtfutil/wtf/utils" 8 "google.golang.org/api/calendar/v3" 9 ) 10 11 type CalEvent struct { 12 event *calendar.Event 13 } 14 15 func NewCalEvent(event *calendar.Event) *CalEvent { 16 calEvent := CalEvent{ 17 event: event, 18 } 19 20 return &calEvent 21 } 22 23 /* -------------------- Exported Functions -------------------- */ 24 25 func (calEvent *CalEvent) AllDay() bool { 26 return len(calEvent.event.Start.Date) > 0 27 } 28 29 func (calEvent *CalEvent) ConflictsWith(otherEvents []*CalEvent) bool { 30 hasConflict := false 31 32 for _, otherEvent := range otherEvents { 33 if calEvent.event == otherEvent.event { 34 continue 35 } 36 37 if calEvent.Start().Before(otherEvent.End()) && calEvent.End().After(otherEvent.Start()) { 38 hasConflict = true 39 break 40 } 41 } 42 43 return hasConflict 44 } 45 46 func (calEvent *CalEvent) Now() bool { 47 return time.Now().After(calEvent.Start()) && time.Now().Before(calEvent.End()) 48 } 49 50 func (calEvent *CalEvent) Past() bool { 51 if calEvent.AllDay() { 52 // FIXME: This should calculate properly 53 return false 54 } 55 56 return !calEvent.Now() && calEvent.Start().Before(time.Now()) 57 } 58 59 func (calEvent *CalEvent) ResponseFor(email string) string { 60 for _, attendee := range calEvent.event.Attendees { 61 if attendee.Email == email { 62 return attendee.ResponseStatus 63 } 64 } 65 66 return "" 67 } 68 69 /* -------------------- DateTimes -------------------- */ 70 71 func (calEvent *CalEvent) End() time.Time { 72 var calcTime string 73 var end time.Time 74 75 if calEvent.AllDay() { 76 calcTime = calEvent.event.End.Date 77 end, _ = time.ParseInLocation("2006-01-02", calcTime, time.Local) 78 } else { 79 calcTime = calEvent.event.End.DateTime 80 end, _ = time.Parse(time.RFC3339, calcTime) 81 } 82 83 return end 84 } 85 86 func (calEvent *CalEvent) Start() time.Time { 87 var calcTime string 88 var start time.Time 89 90 if calEvent.AllDay() { 91 calcTime = calEvent.event.Start.Date 92 start, _ = time.ParseInLocation("2006-01-02", calcTime, time.Local) 93 } else { 94 calcTime = calEvent.event.Start.DateTime 95 start, _ = time.Parse(time.RFC3339, calcTime) 96 } 97 98 return start 99 } 100 101 func (calEvent *CalEvent) Timestamp(hourFormat string, showEndTime bool) string { 102 if calEvent.AllDay() { 103 startTime, _ := time.ParseInLocation("2006-01-02", calEvent.event.Start.Date, time.Local) 104 return startTime.Format(utils.FriendlyDateFormat) 105 } 106 107 startTime, _ := time.Parse(time.RFC3339, calEvent.event.Start.DateTime) 108 endTime, _ := time.Parse(time.RFC3339, calEvent.event.End.DateTime) 109 110 timeFormat := utils.MinimumTimeFormat24 111 if hourFormat == "12" { 112 timeFormat = utils.MinimumTimeFormat12 113 } 114 115 if showEndTime { 116 return fmt.Sprintf("%s-%s", startTime.Format(timeFormat), endTime.Format(timeFormat)) 117 } 118 119 return startTime.Format(timeFormat) 120 }