Help with nextOccurrence()


(Jason) #1

I’m trying to do some time comparison within a SmartApp. I basically want to use conditional logic to compare the current time vs a time defined as a perference in the app. It seems like using the nextOccurrence method is the best way to do this. The problem I’m having is the method doesn’t seem to work according to the documentation. For example, I’d like to get the next occurrence of “10 PM” so i have “def next = nextOccurrence(‘20:00’)” as per the documentation. When I run this in the IDE I get a message indicating I need to include a TimeZone. If I try to use "def next = nextOccurrence(‘20:00’,‘America/Los_Angeles’) I get a different message in the IDE that I don’t quite understand (pasted below). Anyone have any ideas on how to get this working?

groovy.lang.MissingMethodException: No signature of method: script1410314202644455294186.nextOccurrence() is applicable for argument types: (java.lang.String, java.lang.String) values: [22:00:00.00, America/Los_Angeles]
Possible solutions: nextOccurrence(java.lang.Object) @ line 75


(Matt Nohr) #2

Sorry about that, the documentation is not very clear and nextOccurence is not used very much. However, it should work as you suggested. It should just take a String, but the string has to be of a specific format, something like:

new Date().format("yyyy-MM-dd'T'HH:mm:ss.SSSZ")

Here is an example:

nextOccurrence('2014-01-01T20:00:00.000-0800')

The month/day/year do not matter in this case, just the time and timezone offset.

Hope this helps!


(Jason) #3

@matthewnohr, Thanks, that appears to be working. One last question, If I’m using ‘-0800’ for “Pacific” time zone will this also account for daylight savings changes?


(Matt Nohr) #4

Ugh, time zones and daylight savings time are the worst!

No, this approach will not take into account daylight savings. However, as long as you have your location’s coordinates set (geofence), the location time zone should be set. Then you can use that to get the appropriate time with daylight savings.

Here’s one approach that should work. I get the time using the time zone, then check and add 1 day to it if it already happened today (to get the next occurrence)

def tenPm = timeToday('22:00', location.timeZone)
if(tenPm.before(new Date())) {
    tenPm = tenPm + 1
}

Hope this helps!