diff --git a/src/planner.c b/src/planner.c index 656414f..dc0c23a 100644 --- a/src/planner.c +++ b/src/planner.c @@ -269,3 +269,60 @@ llist *genPlan(llist *head, time_t timeAvail) { return events_ll; } + +int exportiCal(llist *events_ll) { + + llist *ev_ll = events_ll; + + llistPrintE(ev_ll); + + printf("%s", iCalHeader); + + time_t now = time(NULL); + struct tm lc; + localtime_r(&now, &lc); + + // gen filename & open for write + char nameBuf[32]; + strftime(nameBuf, 32 - 12, "%F", &lc); + strcat(nameBuf, "dayplan.ics"); + FILE *fp = fopen(nameBuf, "w"); + if (fp == NULL) { + planLog("fopen failed!!", 1); + return 1; + } + // write iCal header to file + fprintf(fp, "%s", iCalHeader); + + // for every event in events_ll create VEVENT str and write to fp + int count = 0; + while (ev_ll != NULL) { + // gen iCal compatible time str + Event *current = ev_ll->data; + struct tm startlc; + struct tm endlc; + localtime_r(¤t->plannedStartTime, &startlc); + localtime_r(¤t->plannedEndTime, &endlc); + char timeStartBuf[17]; + char timeEndBuf[17]; + char timeStamp[17]; + strftime(timeStamp, 17, "%Y%m%dT%k%M%SZ", &lc); + printf("%s\n", timeStamp); + strftime(timeStartBuf, 17, "%Y%m%dT%k%M%SZ", &startlc); + printf("%s\n", timeStartBuf); + strftime(timeEndBuf, 17, "%Y%m%dT%k%M%SZ", &endlc); + printf("%s\n", timeEndBuf); + + fprintf(fp, iCalEvent, current->task->name, count, timeStamp, timeStartBuf, + timeEndBuf, current->task->name); + ev_ll = ev_ll->next; + count += 1; + } + + // after all events are written end cal with + // END:VCALENDAR + fprintf(fp, "END:VCALENDAR\r\n"); + fclose(fp); + + return 0; +}