判断一个字符串 clock time 格式时间是否发生```穿越```(比当前时间晚)
```
func IsTimeThrough(clockTime string) bool {
tm, err := time.Parse("2006-01-02 15:04:05", clockTime)
if err != nil {
return false
}
if time.Now().Add(time.Minute * 10).Before(tm) {
return true
}
return false
}
```
单体测试代码:
```
func IsTimeThrough(clockTime string) bool {
tm, err := time.Parse("2006-01-02 15:04:05", clockTime)
if err != nil {
return false
}
t1 := time.Now().Add(time.Minute * 10)
fmt.Println(t1.Format("2006-01-02 15:04:05"))
fmt.Println(tm.Format("2006-01-02 15:04:05"))
var flag = false
if t1.Before(tm) {
fmt.Println("true")
flag = true
} else {
fmt.Println("false")
flag = false
}
return flag
}
```
测试结果:
```
2018-08-04 11:30:34
2018-08-04 11:21:10
true
```
查看 **time** 包源码注释找到:
```
go1.9.2 time/time.go L48
// If Times t and u both contain monotonic clock readings, the operations
// t.After(u), t.Before(u), t.Equal(u), and t.Sub(u) are carried out
// using the monotonic clock readings alone, ignoring the wall clock
// readings. If either t or u contains no monotonic clock reading, these
// operations fall back to using the wall clock readings.
```
如果两个时钟都是 monotonic 模式,会忽略 wall 的值,如果有一个不是 mononotic,会使用 wall 来作为补偿。
通过 debug 发现 **Now** 生成的时钟是 ```monitonic```,传入的字符串转化的时钟不是 ```monotonic```(wall = 0)。
虽然有这样的限制,但是不能违反结果啊!!!
这算 BUG 吗?
贴相应的源码片段:
```
// nsec returns the time's nanoseconds.
func (t *Time) nsec() int32 {
return int32(t.wall & nsecMask)
}
// sec returns the time's seconds since Jan 1 year 1.
func (t *Time) sec() int64 {
if t.wall&hasMonotonic != 0 {
return wallToInternal + int64(t.wall<<1>>(nsecShift+1))
}
return int64(t.ext)
}
```
```
// Before reports whether the time instant t is before u.
func (t Time) Before(u Time) bool {
if t.wall&u.wall&hasMonotonic != 0 {
return t.ext < u.ext
}
return t.sec() < u.sec() || t.sec() == u.sec() && t.nsec() < u.nsec()
}
```
如果这样的需求该自己作比较?
有疑问加站长微信联系(非本文作者)