/*! phoscon-app 2024-10-07 */

function setGatewayDateAndTime(){let lt=app.config.get("localtime")||"";if(!(lt=lt.split("T"))[0]||!lt[1])return;let date=lt[0].split("-"),time=lt[1].split(":");if(3!==date.length||3!==time.length)return;date[1]=parseInt(date[1])-1;var gatewayDate=new Date(date[0],date[1],date[2],time[0],time[1],time[2]),browserDate=new Date,h=gatewayDate.getHours(),m=gatewayDate.getMinutes(),s=gatewayDate.getSeconds();app.time.format=timeFormat(browserDate),app.time.offset=browserDate.getTimezoneOffset(),app.clock.hour=h,app.clock.min=m,app.clock.sec=s,app.time.format!==app.config.get("timeformat")&&(console.log("timeformat of gateway is different. Set browser timeformat: "+app.time.format),app.config.save({timeformat:app.time.format},{patch:!0}));let diff=Math.abs(gatewayDate.getTime()-browserDate.getTime());diff>3e4&&(console.log("time difference browser <-> gateway: "+diff+"s"),app.config.has("ntp")&&"synced"===app.config.get("ntp")||"deCONZ"===app.config.get("modelid")&&saveGatewayDateTime(browserDate)),"12h"===app.time.format&&(h<12?" AM":" PM",h=h%12||12),h<10&&(h="0"+h),m<10&&(m="0"+m)}function checkTimezone(h){(new Intl.DateTimeFormat).resolvedOptions();var timezone=app.config.attributes.timezone;"12h"===app.time.format&&(prefix=h<12?" AM":" PM",h=h%12||12);var tz_affix=Math.round(app.time.offset/60),set_timezone="Etc/GMT";0!==tz_affix&&(set_timezone+=(tz_affix<0?"-":"+")+Math.abs(tz_affix)),timezone!==set_timezone&&(console.log("timezone is different. Set new timezone: "+set_timezone),app.config.save({timezone:set_timezone},{patch:!0}))}function saveGatewayDateTime(dateTime){var month=dateTime.getMonth()+1<10?"0"+(dateTime.getMonth()+1):dateTime.getMonth()+1,day=dateTime.getDate()<10?"0"+dateTime.getDate():dateTime.getDate(),h=dateTime.getHours()<10?"0"+dateTime.getHours():dateTime.getHours(),min=dateTime.getMinutes()<10?"0"+dateTime.getMinutes():dateTime.getMinutes(),s=dateTime.getSeconds()<10?"0"+dateTime.getSeconds():dateTime.getSeconds();app.clock.hour=parseInt(h),app.clock.min=parseInt(min),app.clock.sec=parseInt(s);var timestring=dateTime.getFullYear()+"-"+month+"-"+day+"T"+h+":"+min+":"+s;console.log("save time: "+timestring),app.config.save({utc:timestring},{patch:!0})}function saveTimezone(){var tz=$("#select-country").val();console.log("save timezone: "+tz),app.config.save({timezone:tz},{patch:!0})}