For some time now I've been trying to create a function that validates a variable of the type [][2]time.Time. The columns of the array represet a pair of time.Time which are an input date and an outbound date respectively.but I can't produce code that addresses all possible invalid combinations and at the same time does not invalidate combinations that are actually valid.
The rules to invalidate are:
Dates cannot be longer than the current date and time.
Times can't be the same. E.g:
[][2]time.Time{{time.Date(2020, 11, 23, 8, 0, 0, 0, time.UTC), time.Date(2020, 11, 23, 8, 0, 0, 0, time.UTC)}}. Or[][2]time.Time{ {time.Date(2020, 11, 23, 8, 0, 0, 0, time.UTC), time.Date(2020, 11, 23, 9, 0, 0, 0, time.UTC)}, {time.Date(2020, 11, 23, 9, 0, 0, 0, time.UTC), time.Date(2020, 11, 23, 11, 0, 0, 0, time.UTC)}}There cannot be a new entry if there is no output before. E.g:
[][2]time.Time{ {time.Date(2020, 11, 23, 8, 0, 0, 0, time.UTC), time.Time{}//Default}, {time.Date(2020, 11, 23, 10, 0, 0, 0, time.UTC), time.Date(2020, 11, 23, 11, 0, 0, 0, time.UTC)} }The dates must be in ascending order, that is, the first slice date that in this case represents an entry must be older than the second, the second older than the third, and so on. Therefore, below would be examples of invalid combinations:
[][2]time.Time{ {time.Date(2020, 11, 23, 8, 0, 0, 0, time.UTC), time.Date(2020, 11, 23, 7, 0, 0, 0, time.UTC)}, {time.Date(2020, 11, 23, 10, 0, 0, 0, time.UTC), time.Date(2020, 11, 23, 11, 0, 0, 0, time.UTC)} }.
What has caused me the biggest problem are the default values, because they are valid Time objects but they should be considered as null, that is, that the output date was not reported. Imagine as if it were a point sheet of a worker, if there is an entry date but the output is default means that the worker entered but has not yet left, that is, it is a valid situation. But it would not be valid for a worker to register a new entry if there is not yet an output for their previous entry. That's the code I've been able to produce so far. Yes, it's not complete because I've modified it so many times I don't know how to move forward anymore.
func validSliceArrayTime(slarti [][2]time.Time) bool {
now_time := time.Now()
var rtime, ltime time.Time
var rt_is_def bool
for _, v := slarti {
rtime = v[1]
rt_is_def = rtime.Year() <= 1
switch {
case v[0].Year() <= 1:
return false
case v[0].After(now_time):
return false
case (!v[0].Before(rtime) && !rt_is_def):
return false
case !v[0].After(ltime):
return false
// case !rtime.After(ltime):
// return false
// case rtime.After(now_time):
// return false
default:
ltime = v[1]
}
}
return true
}