sprechstunden-go/models/date.go

65 lines
1.4 KiB
Go

package models
import (
"log"
)
type Date struct {
Week int // Set whether the date is all weeks (0), odd weeks (1) or even weeks (2).
Day int
Hour int
Minute int
}
// Return the name of the day for a given integer.
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:
log.Printf("No day name found for day %d", day)
return ""
}
}
// Compare whether first date is strictly before second date.
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
}
// Get the end date for some duration.
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}
}
endDate.Day = (endDate.Day + (endDate.Hour*60+endDate.Minute+duration)/(60*24)) % 7
endDate.Hour = (endDate.Hour + (endDate.Minute+duration)/60) % 24
endDate.Minute = (endDate.Minute + duration) % 60
return endDate
}