3 回答
TA贡献1877条经验 获得超6个赞
有很多方法可以解决这个问题,选择正确的方法取决于“开始时间前 x 分钟”的含义:
“只是时钟部分”我假设您的意思是当天,或者,如果当前时间在“时钟部分”之后,明天。
夏令时的开始/结束可能会导致明显的答案可能不正确(但您需要指定在这些时间应该发生什么)。
是否需要考虑时区?(我暂时不会假设,但值得考虑一下!)。
我将通过计算目标时间和检查时间之间的分钟数来解决这个问题,如下所示:
func MinutesBefore(target time.Time, check time.Time) int {
t := time.Date(check.Year(), check.Month(), check.Day(), target.Hour(), target.Minute(), target.Second(), target.Nanosecond(), target.Location())
if t.Before(check) {
t = time.Date(check.Year(), check.Month(), check.Day()+1, target.Hour(), target.Minute(), target.Second(), target.Nanosecond(), target.Location())
}
return int(t.Sub(check).Minutes())
}
在操场上尝试一些测试用例。
TA贡献1789条经验 获得超8个赞
截断差异可能更容易,而不是时间,例如:
package main
import (
"log"
"time"
)
const (
day = time.Hour * 24
)
func within(t1, t2 time.Time, epsilon time.Duration) bool {
delta := t1.Sub(t2)
delta = (delta - delta.Truncate(day))
// Take absolute value of delta.
if delta < 0 {
delta = -delta
}
// Reduce if the times are closer across midnight.
if adjusted := day - delta; adjusted < delta {
delta = adjusted
}
return delta <= epsilon
}
func main() {
start := time.Date(2019, 7, 2, 01, 0, 0, 0, time.UTC)
for _, tc := range []struct {
t time.Time
e time.Duration
want bool
}{
{
t: time.Date(2020, 11, 20, 0, 45, 0, 0, time.UTC),
e: time.Minute * 30,
want: true,
},
{
t: time.Date(2020, 11, 20, 0, 20, 0, 0, time.UTC),
e: time.Minute * 30,
want: false,
},
{
t: time.Date(2020, 11, 20, 23, 0, 0, 0, time.UTC),
e: time.Minute * 120,
want: true,
},
{
t: time.Date(2020, 11, 20, 0, 0, 0, 0, time.UTC),
e: time.Minute * 120,
want: true,
},
{
t: time.Date(2020, 11, 20, 0, 20, 0, 0, time.UTC),
e: time.Minute * 120,
want: true,
},
{
t: time.Date(2020, 11, 20, 22, 30, 0, 0, time.UTC),
e: time.Minute * 120,
want: false,
},
{
t: time.Date(2020, 11, 20, 13, 0, 0, 0, time.UTC),
e: time.Hour * 24,
want: true,
},
} {
if within(start, tc.t, tc.e) != tc.want {
log.Fatalf("test failed for %v, %v, %v", start, tc.t, tc.e)
}
}
}
您说您想在 24 小时或更长时间内返回true,x但请注意,因为您想要案例 2 的第一个示例中给出的行为,结果证明true将在x12 小时或更长时间内返回,因为那是如果您接受 24 小时内或跨过 24 小时的最近距离,则为两次之间的最大距离。
TA贡献1851条经验 获得超5个赞
您可以使用模板解析日期的时间分数(时钟部分)15:04:05
var start time.Time
start, _ = time.Parse("15:04:05", "01:00:00")
我们想要减去start并now检查结果是否不大于x
if start.Sub(now).Minutes() <= x.Minutes() {
return true
}
这将适用于我们的第一个案例,但是当我们中间有一个午夜时它会失败。解决这个问题,我们可以从中减去一天now
if start.Before(now) {
now = now.Add(-time.Hour * 24)
}
那么在这里你有它
func rangeOf(x time.Duration, start time.Time, now time.Time) bool {
if start.Before(now) {
now = now.Add(-time.Hour * 24)
}
if start.Sub(now).Minutes() <= x.Minutes() {
return true
}
return false
}
这是案例二测试
func main() {
x := 120 * time.Minute
var now time.Time
now, _ = time.Parse("15:04:05", "23:00:00")
fmt.Printf("%t\n", rangeOf(x, start, now))
now, _ = time.Parse("15:04:05", "00:00:00")
fmt.Printf("%t\n", rangeOf(x, start, now))
now, _ = time.Parse("15:04:05", "00:20:00")
fmt.Printf("%t\n", rangeOf(x, start, now))
now, _ = time.Parse("15:04:05", "22:30:00")
fmt.Printf("%t\n", rangeOf(x, start, now))
}
这里的所有测试用例https://play.golang.com/p/Ijs-rlPCIIJ
- 3 回答
- 0 关注
- 253 浏览
添加回答
举报
