SmartThings Community

Returning local timezones

Hello!
I am losing my mind with some timezone issues… I am trying to find the code to set a date variable to midnight (00:00) on the current day. I am located in the EST timezone but the only thing that I can get is UTC! I ran the following code at 7:54 and it returned 00:00 for the next day because UTC was already in the net day. I hsve included a few lines of code below and what they return. I am hoping that they would return, Mon Nov 12 00:00:00 EST 2018.

def now = new Date()
7:54:43 PM: trace now = Tue Nov 13 00:54:43 UTC 2018

def midnight = new Date().clearTime()
7:54:43 PM: trace midnight = Tue Nov 13 00:00:00 UTC 2018

log.debug “The time zone for this location is: ${location.timeZone}”
7:54:43 PM: debug The time zone for this location is: sun.util.calendar.ZoneInfo[id=“America/New_York”,offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]

def startTime = timeToday(“00:00”, location.timeZone)
log.trace “startTime = $startTime”
7:54:43 PM: trace startTime = Mon Nov 12 05:00:00 UTC 2018

Any help would be VERY appreciated!

Thank you!
Matt

In the ide, go to My Locations and click on the name of your Location. Is your time zone correct? If not, you might need to set the geolocation.

I wish it was that simple!

Time Zone America/New_York

Any other thoughts?

Thanks in advance!

7:54:43 PM: debug The time zone for this location is: sun.util.calendar.ZoneInfo[id=“America/New_York”

Hi there,

Just wondering why you need to store that date?
is it to trigger something?

if so, runOnce or Schedule(24 00 * * * ?) after first runOnce should be able to trigger your event at 12am daily

Sorry for the delayed response, and thanks for your note! I have a timed execution. At that time, I want to check to see if an event (open/close) has fired since midnight. In other words, if it fires at 0800 then I want to count how many times a sensor has been opened between midnight and 0800.

Thanks again!
Matt

in that case, you need to schedule to 12am a reset event method you define in your DTH to zero a state variable state.nbEvent.
And in your DTH, state.nbEvent is incremented every time an event “open” is triggered.