关于 Time.Before 陷阱

Justin19960208 · · 3215 次点击
Justin19960208
Insects awaken soon after the first day of spring.
根据楼上的反馈,测试了下,好像是这样子的。又**GET**到新知识。 再贴个实现。 ``` func IsTimeThrough(clockTime string) bool { tm, err := time.ParseInLocation("2006-01-02 15:04:05", clockTime, time.Now().Location()) if err != nil { return false } if time.Now().Add(time.Minute * 10).Before(tm) { return true } return false } ```
#5
更多评论
Justin19960208
Insects awaken soon after the first day of spring.
上一个实现: ``` func IsTimeThrough(clockTime string) bool { tm, err := time.Parse("2006-01-02 15:04:05", clockTime) if err != nil { return false } // as the restricted condition for time.Before() now := time.Now().Add(time.Minute * 10) nY, nM, nD := now.Date() tY, tM, tD := tm.Date() if tY > nY { return true } else if tY < nY { return false } // tY == nY if tM > nM { return true } else if tM < nM { return false } // tM == nM if tD > nD { return true } else if tD < nD { return false } // tD == nD nH, nMinute, nS := now.Clock() tH, tMinute, tS := tm.Clock() if tH > nH { return true } else if tH < nH { return false } // tH == nH if tMinute > nMinute { return true } else if tMinute < nMinute { return false } // tMinute == nMinute if tS > nS { return true } return false } ```
#1
建议楼主把时区信息也打印出来,就知道是怎么回事了
#2