2023-08-21 23:56:17 +00:00
|
|
|
package taf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.elara.ws/taf/internal/parser"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TimeFormat = "021504"
|
|
|
|
ValidFormat = "0215"
|
|
|
|
)
|
|
|
|
|
2023-08-23 02:11:58 +00:00
|
|
|
func parseTime(s string, m time.Month, year int) (time.Time, error) {
|
2023-08-21 23:56:17 +00:00
|
|
|
t, err := time.Parse(TimeFormat, s)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
2023-08-23 02:11:58 +00:00
|
|
|
return t.AddDate(year, int(m)-1, 0), nil
|
2023-08-21 23:56:17 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:11:58 +00:00
|
|
|
func parseValid(v *parser.ValidPair, m time.Month, year int) (ValidPair, error) {
|
|
|
|
start, err := parseValidTime(v.Start, m, year)
|
2023-08-21 23:56:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return ValidPair{}, err
|
|
|
|
}
|
|
|
|
|
2023-08-23 02:11:58 +00:00
|
|
|
end, err := parseValidTime(v.End, m, year)
|
2023-08-21 23:56:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return ValidPair{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValidPair{
|
|
|
|
From: start,
|
|
|
|
To: end,
|
|
|
|
Duration: end.Sub(start),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-08-23 02:11:58 +00:00
|
|
|
func parseValidTime(s string, m time.Month, year int) (time.Time, error) {
|
2023-08-21 23:56:17 +00:00
|
|
|
addDays := 0
|
|
|
|
// Go doesn't know what to do with hour 24,
|
|
|
|
// so we set it to 00 the next day
|
|
|
|
if strings.HasSuffix(s, "24") {
|
|
|
|
s = s[:2] + "00"
|
|
|
|
addDays = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := time.Parse(ValidFormat, s)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
|
2023-08-23 02:11:58 +00:00
|
|
|
return t.AddDate(year, int(m)-1, addDays), nil
|
2023-08-21 23:56:17 +00:00
|
|
|
}
|