2023-11-14 22:40:20 -08:00
|
|
|
package coursed
|
2023-11-14 22:35:30 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNoCurrentCourse = errors.New("error: no current course was found")
|
|
|
|
)
|
|
|
|
|
|
|
|
// `Schedule` represents a set of courses that the user is taking
|
|
|
|
//
|
|
|
|
// It stores this as a mapping between the course's short code, and the actual
|
|
|
|
// information about the course
|
|
|
|
type Schedule struct {
|
|
|
|
Courses map[string]CourseConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// `CurrentCourse` returns the current course for the user, based on the
|
|
|
|
// current time, and the schedule that they defined
|
|
|
|
//
|
|
|
|
// It is assumed courses cannot overlap, so this will never return multiple
|
|
|
|
// possible courses
|
|
|
|
//
|
|
|
|
// If no course is found, the error will be of type `ErrNoCurrentCourse`
|
|
|
|
func (s Schedule) CurrentCourse() (string, error) {
|
2023-11-23 00:33:05 -08:00
|
|
|
currentTime, err := time.Parse(time.Kitchen, time.Now().Format(time.Kitchen))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-11-14 22:35:30 -08:00
|
|
|
|
|
|
|
for courseName, courseInfo := range s.Courses {
|
2024-01-18 16:39:12 -08:00
|
|
|
// Skip times that are TBD
|
|
|
|
if courseInfo.StartTime == "TBD" || courseInfo.EndTime == "TBD" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-11-14 22:35:30 -08:00
|
|
|
startTime, err := time.Parse(time.Kitchen, courseInfo.StartTime)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
endTime, err := time.Parse(time.Kitchen, courseInfo.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if currentTime.After(startTime) && currentTime.Before(endTime) {
|
|
|
|
return courseName, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", ErrNoCurrentCourse
|
|
|
|
}
|
|
|
|
|
|
|
|
// `CourseConfig` represents all the information that is needed about a single
|
|
|
|
// course to place it on the schedule
|
|
|
|
type CourseConfig struct {
|
|
|
|
StartTime string `toml:"start_time"`
|
|
|
|
EndTime string `toml:"end_time"`
|
|
|
|
Days string `toml:"days"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CourseConfig) String() string {
|
|
|
|
return fmt.Sprintf("CourseConfig { start: %v, end: %v, days: %v }", c.StartTime, c.EndTime, c.Days)
|
|
|
|
}
|
|
|
|
|
|
|
|
// `LoadSchedule` loads a course schedule from the given file, and returns a
|
|
|
|
// `Schdule` object containing the parsed schedule
|
|
|
|
func LoadSchedule(fileName string) (Schedule, error) {
|
|
|
|
var v Schedule
|
|
|
|
|
2023-11-14 22:56:17 -08:00
|
|
|
configFile, err := os.Open(fileName)
|
2023-11-14 22:35:30 -08:00
|
|
|
if err != nil {
|
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
defer configFile.Close()
|
|
|
|
|
|
|
|
if _, err := toml.NewDecoder(configFile).Decode(&v); err != nil {
|
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}
|