Trust me, You still need the Date, not just the Clock.
For example : what if the StartTime = 23:00:00 and FinishTime = 06:00:00, if you didn't capture the date, the information inside of the Shift struct is FinishTime < StartTime, and that goes against the words of start and finish itself.
That's where the date works, so if the FinishTime is more than the StartTime, the StartTime needs to be reduced by 1 day OR FinishTime plus 1 day.
Here's the example solution code that I make to check if the currentTime is between 2 time(clock)
First of all since I just need the clock information (HH:mm:ss), I prefer to use a string type for the StartTime and FinishTime.
type Shift struct {
StartTime string
FinishTime string
}
and here's what it goes.
type Shift struct {
StartTime string
FinishTime string
}
func main() {
myShift := Shift{
StartTime: "23:00",
FinishTime: "06:00",
}
actualStartTime, actualEndTIme := convertShiftClockToShiftTime(myShift.StartTime, myShift.FinishTime)
fmt.Println(actualStartTime, actualEndTIme)
}
func convertShiftClockToShiftTime(StartTime string, FinishTime string) (startTime time.Time, endTime time.Time) {
// I need to convert the time to a string so I can easily add the clock
timeLayout := "2006-01-02T15:04"
dateOnlyLayout := "2006-01-02"
currentTime := time.Now()
currentDate := currentTime.Format(dateOnlyLayout)
// currentDate = Y-m-d
startDateStr := currentDate + "T" + StartTime
startTime, _ = time.Parse(timeLayout, startDateStr)
endDateStr := currentDate + "T" + FinishTime
endTime, _ = time.Parse(timeLayout, endDateStr)
if startTime.After(endTime) {
// plus 1 day to the endTime if the endTime < startTime
endTime = endTime.AddDate(0, 0, 1)
}
return startTime, endTime
}
I created a function called convertShiftClockToShiftTime to change the clock into actual time as I mentioned before, what if the StartTime = 23:00 and EndTime = 06:00.
Basically the program should know if the StartTime is more than FinishTime, FinishTime should be tomorrow.
Hope this helps, I hope my explanation can explain to you what time itself means.