Ad-hoc Trends error when selecting duration of a month or more

We are using the Ad-hoc trends template in our vision project. It’s working great except we get an error in the duration component when we select a time of 1 month or larger. The error is in the script at the cal.add syntax:

File “event:propertyChange”, line 13, in
TypeError: add(): 2nd arg can’t be coerced to int


from java.util import Calendar
from java.util import Date

if event.propertyName == “selectedValue”:
seconds = long(event.newValue)

if seconds > 0:
	cal = Calendar.getInstance()
	chart = event.source.parent.getComponent('Easy Chart')
	endDate = Date()
	cal.add(Calendar.MILLISECOND, seconds * -1000)
	startDate = cal.getTime()
	chart.endDate = endDate
	chart.startDate = startDate

The Calendar class’ add method only accepts a (32-bit) integer value, so 1 month in milliseconds (2,592,000,000) overflows INT_MAX (2,147,483,647).

If you don’t need millisecond precision, you can easily change the math (and use Calendar.SECOND) instead, and you’ll still be fine for time ranges up to ~68 years :slight_smile:

Thanks, we were on the right track. We tried this before, but realized that we needed to multiple the “seconds” variable in the 2nd argument by -1 to go back in time.