Hi everybody,
I have hard coded a solution to my problem but I am posting this to see if there's a better way to achieve the same result.
The idea is to set a time window between, for example, 7 pm and 8am the next day. If I simply use timeOfDayIsBetween(), it doesn't work, unless I use groovy.time.TimeCategory as seen below.
Please, let me know if you know a simpler way to do this.
boolean restrictedTime(){
result = false
if(!restrictedTimeSlots) result = false
if(atomicState.timeSlots){
int s = atomicState.timeSlots
for(int i = 0; i < s; i++){
def start = settings.find{it.key == "restrictedTimeStart${i}"}.value
def end = settings.find{it.key == "restrictedTimeEnd${i}"}.value
def nextD = settings.find{it.key == "plusOneDay${i}"}?.value
if(nextD) {
def nEnd = Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSSZ", end) //time must be re-parsed into date format using proper pattern "2022-11-21T08:00:00.000-0500"
use(groovy.time.TimeCategory) {
logging "current time: ${new Date().format('yyyy-MM-dd h:mm:ss')}"
logging "mode: $location.currentMode"
nextday = nEnd + 1.day
end = nextday.format("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
}
}
logging "start time slot #$i: ${Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSSZ",start) }"
logging "end time slot #$i: ${Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSSZ", end)} ${nextD ? "(+1 day)" : ""}"
def test = timeOfDayIsBetween(toDateTime(start), toDateTime(end), new Date(), location.timeZone)
if(test) result = true
}
}
log.debug "restricted time returns $result"
return result
}