为了账号安全,请及时绑定邮箱和手机立即绑定

数组时间片。升序验证中的时间

数组时间片。升序验证中的时间

Go
largeQ 2023-02-14 15:04:19
一段时间以来,我一直在尝试创建一个函数来验证 type 的变量[][2]time.Time。数组的列time.Time分别代表一对输入日期和出站日期。但我无法生成解决所有可能无效组合的代码,同时不会使实际有效的组合无效。无效的规则是:日期不能长于当前日期和时间。时代不可能一样。例如:[][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)}}。或者[][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)}}如果之前没有输出,就不可能有新的条目。例如: [][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)} }日期必须按升序排列,即在这种情况下表示条目的第一个切片日期必须早于第二个,第二个早于第三个,依此类推。因此,下面是无效组合的示例: [][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)} }。给我带来最大问题的是默认值,因为它们是有效的 Time 对象,但它们应该被视为 null,即未报告输出日期。想象一下,好像是一个工人的积分表,如果有进入日期,但输出是默认的,意味着工人进入但还没有离开,即是有效的情况。但是,如果工作人员之前的条目还没有输出,那么注册新条目将是无效的。这是我到目前为止能够生成的代码。是的,它还没有完成,因为我已经修改了很多次,我不知道如何继续前进了。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}
查看完整描述

1 回答

?
蛊毒传说

TA贡献1895条经验 获得超3个赞

要检查 a 是否time.Time为零值,请使用Time.IsZero()

除此之外,只需一个接一个地实施您的规则。它不会是最有效的解决方案,但它会很干净和简单,一旦它正常工作,您就可以对其进行改进:

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

}

这是测试所有规则以及有效输入的测试代码(在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)

    }

}



查看完整回答
反对 回复 2023-02-14
  • 1 回答
  • 0 关注
  • 100 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信