diff --git a/Dockerfile b/Dockerfile index a564d47..ac58114 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,6 +23,8 @@ COPY --from=builder /usr/src/app/bin/vitty ./bin/vitty COPY --from=builder /usr/src/app/credentials ./credentials + +RUN apk --no-cache add tzdata RUN chmod +x ./bin/vitty CMD ["./bin/vitty", "run"] \ No newline at end of file diff --git a/vitty-backend-api/api/initialize.go b/vitty-backend-api/api/initialize.go index 493adec..1640233 100644 --- a/vitty-backend-api/api/initialize.go +++ b/vitty-backend-api/api/initialize.go @@ -1,6 +1,8 @@ package api import ( + "time" + v1 "github.com/GDGVIT/vitty-backend/vitty-backend-api/api/v1" v2 "github.com/GDGVIT/vitty-backend/vitty-backend-api/api/v2" "github.com/gofiber/fiber/v2" @@ -11,8 +13,6 @@ import ( func NewWebApi() *fiber.App { // New fiber app fiberApp := fiber.New() - - fiberApp = fiber.New() fiberApp.Use(logger.New()) fiberApp.Use(cors.New( cors.Config{ @@ -36,6 +36,9 @@ func NewWebApi() *fiber.App { }) }) + local, _ := time.LoadLocation("Asia/Kolkata") + time.Local = local + api := fiberApp.Group("/api") v1.V1Handler(api) v2.V2Handler(api) diff --git a/vitty-backend-api/internal/models/slots.go b/vitty-backend-api/internal/models/slots.go index 40f9e71..72dbb9d 100644 --- a/vitty-backend-api/internal/models/slots.go +++ b/vitty-backend-api/internal/models/slots.go @@ -37,30 +37,30 @@ type Timings struct { } var TheoryTimings = []Timings{ - {StartTime: "08:00", EndTime: "08:50"}, - {StartTime: "09:00", EndTime: "09:50"}, - {StartTime: "10:00", EndTime: "10:50"}, - {StartTime: "11:00", EndTime: "11:50"}, - {StartTime: "12:00", EndTime: "12:50"}, - {StartTime: "14:00", EndTime: "14:50"}, - {StartTime: "15:00", EndTime: "15:50"}, - {StartTime: "16:00", EndTime: "16:50"}, - {StartTime: "17:00", EndTime: "17:50"}, - {StartTime: "18:00", EndTime: "18:50"}, - {StartTime: "19:00", EndTime: "19:50"}, + {StartTime: "0000-01-01T09:00", EndTime: "0000-01-01T09:50"}, + {StartTime: "0000-01-01T10:00", EndTime: "0000-01-01T10:50"}, + {StartTime: "0000-01-01T11:00", EndTime: "0000-01-01T11:50"}, + {StartTime: "0000-01-01T12:00", EndTime: "0000-01-01T12:50"}, + {StartTime: "0000-01-01T08:00", EndTime: "0000-01-01T08:50"}, + {StartTime: "0000-01-01T14:00", EndTime: "0000-01-01T14:50"}, + {StartTime: "0000-01-01T15:00", EndTime: "0000-01-01T15:50"}, + {StartTime: "0000-01-01T16:00", EndTime: "0000-01-01T16:50"}, + {StartTime: "0000-01-01T17:00", EndTime: "0000-01-01T17:50"}, + {StartTime: "0000-01-01T18:00", EndTime: "0000-01-01T18:50"}, + {StartTime: "0000-01-01T19:00", EndTime: "0000-01-01T19:50"}, } var LabTimings = []Timings{ - {StartTime: "08:00", EndTime: "08:50"}, - {StartTime: "08:51", EndTime: "09:40"}, - {StartTime: "09:51", EndTime: "10:40"}, - {StartTime: "10:41", EndTime: "11:30"}, - {StartTime: "11:40", EndTime: "12:30"}, - {StartTime: "12:31", EndTime: "13:20"}, - {StartTime: "14:00", EndTime: "14:50"}, - {StartTime: "14:51", EndTime: "15:40"}, - {StartTime: "15:51", EndTime: "16:40"}, - {StartTime: "16:41", EndTime: "17:30"}, - {StartTime: "17:40", EndTime: "18:30"}, - {StartTime: "18:31", EndTime: "19:20"}, + {StartTime: "0000-01-01T08:00", EndTime: "0000-01-01T08:50"}, + {StartTime: "0000-01-01T08:51", EndTime: "0000-01-01T09:40"}, + {StartTime: "0000-01-01T09:51", EndTime: "0000-01-01T10:40"}, + {StartTime: "0000-01-01T10:41", EndTime: "0000-01-01T11:30"}, + {StartTime: "0000-01-01T11:40", EndTime: "0000-01-01T12:30"}, + {StartTime: "0000-01-01T12:31", EndTime: "0000-01-01T13:20"}, + {StartTime: "0000-01-01T14:00", EndTime: "0000-01-01T14:50"}, + {StartTime: "0000-01-01T14:51", EndTime: "0000-01-01T15:40"}, + {StartTime: "0000-01-01T15:51", EndTime: "0000-01-01T16:40"}, + {StartTime: "0000-01-01T16:41", EndTime: "0000-01-01T17:30"}, + {StartTime: "0000-01-01T17:40", EndTime: "0000-01-01T18:30"}, + {StartTime: "0000-01-01T18:31", EndTime: "0000-01-01T19:20"}, } diff --git a/vitty-backend-api/internal/models/timetable.go b/vitty-backend-api/internal/models/timetable.go index eb74be8..62d04f1 100644 --- a/vitty-backend-api/internal/models/timetable.go +++ b/vitty-backend-api/internal/models/timetable.go @@ -7,6 +7,8 @@ import ( "golang.org/x/exp/slices" ) +const STD_REF_TIME = "2006-01-02T15:04" + type Slot struct { Name string `json:"name"` Code string `json:"code"` @@ -28,11 +30,39 @@ func (t Timetable) GetDaySlots(day time.Weekday) map[string][]Slot { var data []Slot daySlots := DailySlots[day.String()] + var err error // Theory slots for _, slot := range t.Slots { if slot.Type == "Theory" && slices.Contains(daySlots["Theory"], slot.Slot) { + index := slices.Index(daySlots["Theory"], slot.Slot) + slot.StartTime, err = time.ParseInLocation(STD_REF_TIME, TheoryTimings[index].StartTime, time.Local) + if err != nil { + log.Println("Error parsing time: ", err) + return nil + } + + slot.EndTime, err = time.ParseInLocation(STD_REF_TIME, TheoryTimings[index].EndTime, time.Local) + + if err != nil { + log.Println("Error parsing time: ", err) + return nil + } + data = append(data, slot) } else if slot.Type == "Lab" && slices.Contains(daySlots["Lab"], slot.Slot) { + index := slices.Index(daySlots["Lab"], slot.Slot) + slot.StartTime, err = time.ParseInLocation(STD_REF_TIME, LabTimings[index].StartTime, time.Local) + if err != nil { + log.Println("Error parsing time: ", err) + return nil + } + + slot.EndTime, err = time.ParseInLocation(STD_REF_TIME, LabTimings[index].EndTime, time.Local) + + if err != nil { + log.Println("Error parsing time: ", err) + return nil + } data = append(data, slot) } } @@ -48,25 +78,29 @@ func (t Timetable) GetDaywiseTimetable() map[string][]Slot { if slices.Contains(value["Theory"], slot.Slot) { index := slices.Index(value["Theory"], slot.Slot) var err error - slot.StartTime, err = time.Parse("15:04", TheoryTimings[index].StartTime) + slot.StartTime, err = time.ParseInLocation(STD_REF_TIME, TheoryTimings[index].StartTime, time.Local) if err != nil { log.Println("Error parsing time: ", err) + return nil } - slot.EndTime, err = time.Parse("15:04", TheoryTimings[index].EndTime) + slot.EndTime, err = time.ParseInLocation(STD_REF_TIME, TheoryTimings[index].EndTime, time.Local) if err != nil { log.Println("Error parsing time: ", err) + return nil } resp[day] = append(resp[day], slot) } else if slices.Contains(value["Lab"], slot.Slot) { index := slices.Index(value["Lab"], slot.Slot) var err error - slot.StartTime, err = time.Parse("15:04", LabTimings[index].StartTime) + slot.StartTime, err = time.ParseInLocation(STD_REF_TIME, LabTimings[index].StartTime, time.Local) if err != nil { log.Println("Error parsing time: ", err) + return nil } - slot.EndTime, err = time.Parse("15:04", LabTimings[index].EndTime) + slot.EndTime, err = time.ParseInLocation(STD_REF_TIME, LabTimings[index].EndTime, time.Local) if err != nil { log.Println("Error parsing time: ", err) + return nil } resp[day] = append(resp[day], slot) } @@ -80,22 +114,22 @@ func (s *Slot) AddSlotTime() error { if slices.Contains(value["Theory"], s.Slot) { index := slices.Index(value["Theory"], s.Slot) var err error - s.StartTime, err = time.Parse("15:04", TheoryTimings[index].StartTime) + s.StartTime, err = time.ParseInLocation(STD_REF_TIME, TheoryTimings[index].StartTime, time.Local) if err != nil { return err } - s.EndTime, err = time.Parse("15:04", TheoryTimings[index].EndTime) + s.EndTime, err = time.ParseInLocation(STD_REF_TIME, TheoryTimings[index].EndTime, time.Local) if err != nil { return err } } else if slices.Contains(value["Lab"], s.Slot) { index := slices.Index(value["Lab"], s.Slot) var err error - s.StartTime, err = time.Parse("15:04", LabTimings[index].StartTime) + s.StartTime, err = time.ParseInLocation(STD_REF_TIME, LabTimings[index].StartTime, time.Local) if err != nil { return err } - s.EndTime, err = time.Parse("15:04", LabTimings[index].EndTime) + s.EndTime, err = time.ParseInLocation(STD_REF_TIME, LabTimings[index].EndTime, time.Local) if err != nil { return err } diff --git a/vitty-backend-api/internal/models/users.go b/vitty-backend-api/internal/models/users.go index 7fc7fd5..65621dd 100644 --- a/vitty-backend-api/internal/models/users.go +++ b/vitty-backend-api/internal/models/users.go @@ -25,7 +25,7 @@ func (u *User) GetCurrentStatus() map[string]interface{} { // If no, status = free // Get current time(indian timezone) without `date` part now := time.Now() - currTime := time.Date(0, 1, 1, now.Hour(), now.Minute(), now.Second(), 0, time.UTC).Add(5*time.Hour + 30*time.Minute) + currTime := time.Date(0, 1, 1, now.Hour(), now.Minute(), now.Second(), 0, time.Local) // Remove date part fmt.Println("Current time: ", currTime) daySlots := u.GetTimeTable().GetDaySlots(time.Now().Weekday())