package utils import ( "context" "fmt" "time" ) // true if current hour is between a and b // clock is 24 hours // overflow is ok, e.g. CurrentHourCheck(22,8) will return true at night // always refers to local time func CurrentHourCheck(a, b int) bool { t, err := LocalTime(context.Background()) if err != nil { fmt.Printf("failed to get localtime: %s\n", ErrorString(err)) return false } return currentHourCheckWithTime(a, b, t) } func currentHourCheckWithTime(a, b int, t time.Time) bool { h, _, _ := t.Clock() if a == b { // e.g. CurrentHourCheck(22,22) -> returns true only at 22 hours return h == a } if a > b { // e.g. CurrentHourCheck(22,8) -> returns true between 22 and 8 hours (as in: during the night) if h >= a { return true } if h < b { return true } return false } // e.g. CurrentHourCheck(8,22) -> returns true between 8 and 22 hours (as in: during the day) if h <= a { return false } if h > b { return false } return true }