Home > database >  Slice of array time.Time in Ascending order validation
Slice of array time.Time in Ascending order validation

Time:09-13

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
}

CodePudding user response:

To check if a time.Time is its zero value, use Time.IsZero().

Other than that, simply implement your rules one-by-one. It won't be the most efficient solution, but it will be clean and simple which you can improve once it works correctly:

func isValid(slarti [][2]time.Time) bool {
    now := time.Now()

    for i, v := range slarti {
        v1, v2 := v[0], v[1]

        // Rule #3
        if v1.IsZero() {
            return false
        }

        // Rule #1: times must be in the past
        if now.Before(v1) || now.Before(v2) {
            return false
        }

        // Rule #2: times can't be the same
        if v1.Equal(v2) {
            return false
        }
        if i > 0 && v1.Equal(slarti[i-1][1]) {
            return false
        }

        // Rule #3: invalid entry if no output before:
        if i > 0 && slarti[i-1][1].IsZero() {
            return false
        }

        // Rule #4: times must be in ascending order:
        if !v2.IsZero() && v2.Before(v1) {
            return false
        }
        if i > 0 && v1.Before(slarti[i-1][1]) {
            return false
        }
    }

    return true // Got this far: valid
}

Here's a test code that tests all rules, and also valid inputs (try it on the Go Playground):

cases := []struct {
    name  string
    input [][2]time.Time
    valid bool
}{
    {
        name:  "Valid",
        input: [][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)}},
        valid: true,
    },
    {
        name:  "Valid (2)",
        input: [][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, 10, 0, 0, 0, time.UTC), time.Date(2020, 11, 23, 11, 0, 0, 0, time.UTC)}},
        valid: true,
    },
    {
        name:  "Valid (3)",
        input: [][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, 10, 0, 0, 0, time.UTC), time.Time{}}},
        valid: true,
    },
    {
        name:  "Rule #1",
        input: [][2]time.Time{{time.Date(2023, 11, 23, 8, 0, 0, 0, time.UTC), time.Date(2023, 11, 23, 9, 0, 0, 0, time.UTC)}},
    },
    {
        name:  "Rule #2",
        input: [][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)}},
    },
    {
        name:  "Rule #2 (2)",
        input: [][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)}},
    },
    {
        name:  "Rule #3",
        input: [][2]time.Time{{time.Date(2020, 11, 23, 8, 0, 0, 0, time.UTC), time.Time{}}, {time.Date(2020, 11, 23, 10, 0, 0, 0, time.UTC), time.Date(2020, 11, 23, 11, 0, 0, 0, time.UTC)}},
    },
    {
        name:  "Rule #3 (2)",
        input: [][2]time.Time{{time.Time{}, time.Date(2020, 11, 23, 9, 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)}},
    },
    {
        name:  "Rule #4",
        input: [][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)}},
    },
}

for i, c := range cases {
    if valid := isValid(c.input); valid != c.valid {
        log.Printf("[%d] %q expected valid: %t, got: %t", i, c.name, c.valid, valid)
    }
}
  • Related