2022-08-29 20:58:19 +00:00
|
|
|
package models
|
2022-08-24 06:16:27 +00:00
|
|
|
|
2022-11-04 20:15:38 +00:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2022-08-24 06:16:27 +00:00
|
|
|
type Date struct {
|
2022-11-04 20:15:38 +00:00
|
|
|
Week int // Set whether the date is all weeks (0), odd weeks (1) or even weeks (2).
|
2022-08-24 06:16:27 +00:00
|
|
|
Day int
|
|
|
|
Hour int
|
|
|
|
Minute int
|
|
|
|
}
|
2022-08-29 20:58:19 +00:00
|
|
|
|
2022-11-04 20:15:38 +00:00
|
|
|
// Return the name of the day for a given integer.
|
2022-09-05 15:55:08 +00:00
|
|
|
func DayName(day int) string {
|
|
|
|
switch day {
|
|
|
|
case 0:
|
|
|
|
return "Montag"
|
|
|
|
case 1:
|
|
|
|
return "Dienstag"
|
|
|
|
case 2:
|
|
|
|
return "Mittwoch"
|
|
|
|
case 3:
|
|
|
|
return "Donnerstag"
|
|
|
|
case 4:
|
|
|
|
return "Freitag"
|
|
|
|
default:
|
2022-11-04 20:15:38 +00:00
|
|
|
log.Printf("No day name found for day %d", day)
|
2022-09-05 15:55:08 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
2022-09-07 16:26:05 +00:00
|
|
|
|
2022-11-04 20:15:38 +00:00
|
|
|
// Compare whether first date is strictly before second date.
|
2022-09-07 16:26:05 +00:00
|
|
|
func DateLess(first Date, second Date) bool {
|
|
|
|
if first.Day < second.Day {
|
|
|
|
return true
|
|
|
|
} else if first.Day > second.Day {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if first.Hour < second.Hour {
|
|
|
|
return true
|
|
|
|
} else if first.Hour > second.Hour {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if first.Minute < second.Minute {
|
|
|
|
return true
|
|
|
|
} else if first.Minute > second.Minute {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-11-04 20:15:38 +00:00
|
|
|
// Get the end date for some duration.
|
2022-09-07 16:26:05 +00:00
|
|
|
func GetEndDate(date Date, duration int, ignoreWeek bool) Date {
|
|
|
|
var endDate Date
|
|
|
|
if ignoreWeek {
|
|
|
|
endDate = Date{0, date.Day, date.Hour, date.Minute} // deep copy
|
|
|
|
} else {
|
|
|
|
endDate = Date{date.Week, date.Day, date.Hour, date.Minute}
|
|
|
|
}
|
2022-11-04 20:15:38 +00:00
|
|
|
endDate.Day = (endDate.Day + (endDate.Hour*60+endDate.Minute+duration)/(60*24)) % 7
|
2022-11-06 14:48:37 +00:00
|
|
|
endDate.Hour = (endDate.Hour + (endDate.Minute+duration)/60) % 24
|
2022-09-07 16:26:05 +00:00
|
|
|
endDate.Minute = (endDate.Minute + duration) % 60
|
|
|
|
return endDate
|
|
|
|
}
|