In my Go application, I am trying to check the occurrence of a date in a period.
if survey.StartPeriod.Before(time.Now()) && survey.EndPeriod.After(time.Now()) {
fmt.Println("1 BLOCK")
}
else if survey.EndPeriod.Before(time.Now()) {
fmt.Println("2 BLOCK")
}
If I have such incoming data (current time inside period):
Current TIME | 2019-03-28 21:02:47.0377305 +0600 +06 m=+60.748044601
START_PERIOD | 2019-03-28 21:00:00 +0000 +0000
END_PERIOD | 2019-03-28 21:05:00 +0000 +0000
I want to run code inside 1 block. My current code run that block only if I use Before
function to both periods. I am not sure that it's correct.
If I have such incoming data (current time outside the end_period):
Current TIME | 2019-03-28 21:10:47.0377305 +0600 +06 m=+60.748044601
START_PERIOD | 2019-03-28 21:00:00 +0000 +0000
END_PERIOD | 2019-03-28 21:05:00 +0000 +0000
I want to run code inside 2 block. Right know it works incorrect. How to correctly make this program logic?
EDIT:
As you can see CURRENT_TIME
inside range.
CURRENT_TIME | 2019-03-29 08:32:06.1375323 +0600 +06 m=+60.318452301
START_PERIOD | 2019-03-29 08:30:00 +0000 +0000
END_PERIOD | 2019-03-29 08:31:00 +0000 +0000
When I try to test function InTimeSpan
it return false
value which is not correct. I am really confused. Why it happens?!
log.Println(InTimeSpan(survey.StartPeriod, survey.EndPeriod, time.Now()))
// return false
What you are currently checking is if your current time is in the given range. You might want to swap time.Now
with another time to check the occurrence of the date.
However a couple things to consider:
func InTimeSpan(start, finish time.Time, timeToCheck time.Time) bool {
return start.Before(timeToCheck) && finish.After(timeToCheck)
}
func InTimeSpan(start, finish time.Time, timeToCheck time.Time) bool {
return start.UTC().Before(timeToCheck) && finish.UTC().After(timeToCheck)
}
Really lame demo: https://play.golang.org/p/rzv3coztN4Z