在 Go 中进行日期比较有什么选择吗?我必须根据日期和时间对数据进行排序 - 独立。所以我可能允许一个对象出现在一个日期范围内,只要它也出现在一个时间范围内。在这个模型中,我不能简单地选择最旧的日期、最年轻的时间/最新日期、最晚的时间和 Unix() 秒来比较它们。我真的很感激任何建议。
最后,我写了一个时间解析字符串比较模块来检查时间是否在一个范围内。然而,情况并不好。我有一些悬而未决的问题。我会在这里发布只是为了好玩,但我希望有更好的时间比较方法。
package main import ( "strconv" "strings" ) func tryIndex(arr []string, index int, def string) string { if index <= len(arr)-1 { return arr[index] } return def } /* * Takes two strings of format "hh:mm:ss" and compares them. * Takes a function to compare individual sections (split by ":"). * Note: strings can actually be formatted like "h", "hh", "hh:m", * "hh:mm", etc. Any missing parts will be added lazily. */ func timeCompare(a, b string, compare func(int, int) (bool, bool)) bool { aArr := strings.Split(a, ":") bArr := strings.Split(b, ":") // Catches margins. if (b == a) { return true } for i := range aArr { aI, _ := strconv.Atoi(tryIndex(aArr, i, "00")) bI, _ := strconv.Atoi(tryIndex(bArr, i, "00")) res, flag := compare(aI, bI) if res { return true } else if flag { // Needed to catch case where a > b and a is the lower limit return false } } return false } func timeGreaterEqual(a, b int) (bool, bool) {return a > b, a < b} func timeLesserEqual(a, b int) (bool, bool) {return a < b, a > b} /* * Returns true for two strings formmated "hh:mm:ss". * Note: strings can actually be formatted like "h", "hh", "hh:m", * "hh:mm", etc. Any missing parts will be added lazily. */ func withinTime(timeRange, time string) bool { rArr := strings.Split(timeRange, "-") if timeCompare(rArr[0], rArr[1], timeLesserEqual) { afterStart := timeCompare(rArr[0], time, timeLesserEqual) beforeEnd := timeCompare(rArr[1], time, timeGreaterEqual) return afterStart && beforeEnd } // Catch things like `timeRange := "22:00:00-04:59:59"` which will happen // with UTC conversions from local time. // THIS IS THE BROKEN PART I BELIEVE afterStart := timeCompare(rArr[0], time, timeLesserEqual) beforeEnd := timeCompare(rArr[1], time, timeGreaterEqual) return afterStart || beforeEnd }
所以 TLDR,我写了一个 insideTimeRange(range, time) 函数,但它没有完全正常工作。(实际上,主要是第二种情况,即时间范围跨越数天的情况被破坏。原始部分有效,我刚刚意识到在从本地转换为 UTC 时需要考虑到这一点。)
如果有更好的(最好是内置的)方式,我很想听听!
注意:举个例子,我用这个函数在 Javascript 中解决了这个问题:
function withinTime(start, end, time) { var s = Date.parse("01/01/2011 "+start); var e = Date.parse("01/0"+(end=="24:00:00"?"2":"1")+"/2011 "+(end=="24:00:00"?"00:00:00":end)); var t = Date.parse("01/01/2011 "+time); return s <= t && e >= t; }
但是我真的很想做这个过滤器服务器端。
使用time包在 Go 中处理时间信息。
可以使用 Before、After 和 Equal 方法比较时间瞬间。Sub 方法减去两个时刻,产生一个 Duration。Add 方法添加一个时间和一个持续时间,产生一个时间。
播放示例:
package main import ( "fmt" "time" ) func inTimeSpan(start, end, check time.Time) bool { return check.After(start) && check.Before(end) } func main() { start, _ := time.Parse(time.RFC822, "01 Jan 15 10:00 UTC") end, _ := time.Parse(time.RFC822, "01 Jan 16 10:00 UTC") in, _ := time.Parse(time.RFC822, "01 Jan 15 20:00 UTC") out, _ := time.Parse(time.RFC822, "01 Jan 17 10:00 UTC") if inTimeSpan(start, end, in) { fmt.Println(in, "is between", start, "and", end, ".") } if !inTimeSpan(start, end, out) { fmt.Println(out, "is not between", start, "and", end, ".") } }