Код расчета времени установки триггера с учетом временной зоны пользователя:
Fri Mar 26 2021 20:53:32 GMT+0000 (Coordinated Universal Time)
Saved by @Artem #javascript #metabot
var dd = lead.getAttr('alarmDD')*1; // + дней var hh = lead.getAttr('alarmHH')*1; // + часов var mm = lead.getAttr('alarmMM')*1; // + минут var utc = lead.getAttr('utc'); var summerTimeOffset = lead.getAttr('summerTimeOffset'); var serverTimeOffset = 3; // Часовой пояс сервера (Москва) UTC(3) var ms = 60 * 60 * 1000; // количеством миллисекунд в 1 часе ///////////////////////////////////////////////////////////////////////////// // 1. Текущая дата и время относительно сервера (Москва), в миллисекундах ///////////////////////////////////////////////////////////////////////////// var serverDatetime = new Date(Date.now()); var dateString = serverDatetime.getFullYear() + "-" + ("0" + (serverDatetime.getMonth()+1)).slice(-2) + "-" + ("0" + serverDatetime.getDate()).slice(-2) + " " + ("0" + serverDatetime.getHours()).slice(-2) + ":" + ("0" + serverDatetime.getMinutes()).slice(-2); lead.setAttr('serverDatetime', serverDatetime); lead.setAttr('serverDatetimeAsText', dateString); ///////////////////////////////////////////////////////////////////////////// // 2. Текущая дата и время пользователя по его местному часовому поясу ///////////////////////////////////////////////////////////////////////////// // Отнимаем сдвиг на московское время, чтобы привести все к UTC(0) // И потом добавляем сдвиг на часовой пояс пользователя и летнее время var currentLocalDatetime = new Date( serverDatetime - (serverTimeOffset * ms) + utc * ms + summerTimeOffset * ms ); dateString = currentLocalDatetime.getFullYear() + "-" + ("0" + (currentLocalDatetime.getMonth()+1)).slice(-2) + "-" + ("0" + currentLocalDatetime.getDate()).slice(-2) + " " + ("0" + currentLocalDatetime.getHours()).slice(-2) + ":" + ("0" + currentLocalDatetime.getMinutes()).slice(-2); lead.setAttr('currentLocalDatetime', currentLocalDatetime); lead.setAttr('currentLocalDatetimeAsText', dateString); ///////////////////////////////////////////////////////////////////////////// // 3. Расчитываем время будильника в часовом поясе пользователя ///////////////////////////////////////////////////////////////////////////// var localAlarmDatetime = new Date ( currentLocalDatetime // текущее местное время - currentLocalDatetime.getHours() * 60 * 1000 // отнимаем часы, - currentLocalDatetime.getMinutes() // минуты - currentLocalDatetime.getSeconds() // и cекунды, т.е. теперь это 00:00 + 24 * dd * ms // добавляем дни, если надо ); // Устанавливем время будильника localAlarmDatetime.setHours(hh); localAlarmDatetime.setMinutes(mm); localAlarmDatetime.setSeconds(0); dateString = localAlarmDatetime.getFullYear() + "-" + ("0" + (localAlarmDatetime.getMonth()+1)).slice(-2) + "-" + ("0" + localAlarmDatetime.getDate()).slice(-2) + " " + ("0" + localAlarmDatetime.getHours()).slice(-2) + ":" + ("0" + localAlarmDatetime.getMinutes()).slice(-2); lead.setAttr('localAlarmDatetime', localAlarmDatetime); lead.setAttr('localAlarmDatetimeAsText', dateString); /////////////////////////////////////////////////////////////////////////////////// // 4. Расчитываем время будильника в часовом поясе сервера для установки триггера /////////////////////////////////////////////////////////////////////////////////// var d = new Date( localAlarmDatetime - utc * ms - summerTimeOffset * ms + (serverTimeOffset * ms) ); var runTriggerAt = d.getFullYear() + "-" + ("0" + (d.getMonth()+1)).slice(-2) + "-" + ("0" + d.getDate()).slice(-2) + " " + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2) + ":00"; lead.setAttr('runTriggerAt', runTriggerAt);
Comments