1/* eslint-disable */ 2this.BX = this.BX || {}; 3(function (exports,main_core) { 4 'use strict'; 5 6 function convertBitrixFormat(format) { 7 if (!main_core.Type.isStringFilled(format)) { 8 return ''; 9 } 10 return format.replace('YYYY', 'Y') // 1999 11 .replace('MMMM', 'F') // January - December 12 .replace('MM', 'm') // 01 - 12 13 .replace('M', 'M') // Jan - Dec 14 .replace('DD', 'd') // 01 - 31 15 .replace('G', 'g') // 1 - 12 16 .replace(/GG/i, 'G') // 0 - 23 17 .replace('H', 'h') // 01 - 12 18 .replace(/HH/i, 'H') // 00 - 24 19 .replace('MI', 'i') // 00 - 59 20 .replace('SS', 's') // 00 - 59 21 .replace('TT', 'A') // AM - PM 22 .replace('T', 'a'); // am - pm 23 } 24 25 const formatsCache = new main_core.Cache.MemoryCache(); 26 27 /** 28 * Returns culture-specific datetime format by code. 29 * The full list with examples can be found in config.php of this extension in ['settings']['formats']. 30 * All formats are compatible with this.format() without any additional transformations. 31 * 32 * @param code 33 * @returns {string|null} 34 */ 35 function getFormat(code) { 36 return formatsCache.remember(`main.date.format.${code}`, () => { 37 let format = main_core.Extension.getSettings('main.date').get(`formats.${code}`); 38 if (main_core.Type.isStringFilled(format) && (code === 'FORMAT_DATE' || code === 'FORMAT_DATETIME')) { 39 format = convertBitrixFormat(format); 40 } 41 return format; 42 }); 43 } 44 45 /** 46 * @memberOf BX.Main 47 * @alias Date 48 */ 49 let DateTimeFormat = /*#__PURE__*/function () { 50 function DateTimeFormat() { 51 babelHelpers.classCallCheck(this, DateTimeFormat); 52 } 53 babelHelpers.createClass(DateTimeFormat, null, [{ 54 key: "isAmPmMode", 55 value: function isAmPmMode(returnConst) { 56 if (returnConst === true) { 57 return this._getMessage('AMPM_MODE'); 58 } 59 return this._getMessage('AMPM_MODE') !== false; 60 } 61 }, { 62 key: "convertToUTC", 63 value: function convertToUTC(date) { 64 if (!main_core.Type.isDate(date)) { 65 return null; 66 } 67 return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds())); 68 } 69 /** 70 * Function creates and returns Javascript Date() object from server timestamp regardless of local browser (system) timezone. 71 * For example can be used to convert timestamp from some exact date on server to the JS Date object with the same value. 72 * 73 * @param timestamp - timestamp in seconds 74 * @returns {Date} 75 */ 76 }, { 77 key: "getNewDate", 78 value: function getNewDate(timestamp) { 79 return new Date(this.getBrowserTimestamp(timestamp)); 80 } 81 /** 82 * Function transforms server timestamp (in sec) to javascript timestamp (calculated depend on local browser timezone offset). Returns timestamp in milliseconds. 83 * Also see BX.Main.Date.getNewDate description. 84 * 85 * @param timestamp - timestamp in seconds 86 * @returns {number} 87 */ 88 }, { 89 key: "getBrowserTimestamp", 90 value: function getBrowserTimestamp(timestamp) { 91 timestamp = parseInt(timestamp, 10); 92 const browserOffset = new Date(timestamp * 1000).getTimezoneOffset() * 60; 93 return (parseInt(timestamp, 10) + parseInt(this._getMessage('SERVER_TZ_OFFSET')) + browserOffset) * 1000; 94 } 95 /** 96 * Function transforms local browser timestamp (in ms) to server timestamp (calculated depend on local browser timezone offset). Returns timestamp in seconds. 97 * 98 * @param timestamp - timestamp in milliseconds 99 * @returns {number} 100 */ 101 }, { 102 key: "getServerTimestamp", 103 value: function getServerTimestamp(timestamp) { 104 timestamp = parseInt(timestamp, 10); 105 const browserOffset = new Date(timestamp).getTimezoneOffset() * 60; 106 return Math.round(timestamp / 1000 - (parseInt(this._getMessage('SERVER_TZ_OFFSET'), 10) + parseInt(browserOffset, 10))); 107 } 108 }, { 109 key: "formatLastActivityDate", 110 value: function formatLastActivityDate(timestamp, now, utc) { 111 const ampm = this.isAmPmMode(true); 112 const timeFormat = ampm === this.AM_PM_MODE.LOWER ? 'g:i a' : ampm === this.AM_PM_MODE.UPPER ? 'g:i A' : 'H:i'; 113 const format = [['tomorrow', '#01#' + timeFormat], ['now', '#02#'], ['todayFuture', '#03#' + timeFormat], ['yesterday', '#04#' + timeFormat], ['-', this.convertBitrixFormat(this._getMessage('FORMAT_DATETIME')).replace(/:s/g, '')], ['
113s60', 'sago'], ['i60', 'iago'], ['H5', 'Hago'], ['H24', '#03#' + timeFormat], ['d31', 'dago'], ['m12>1', 'mago'], ['m12>0', 'dago'], ['', '#05#']]; 114 let formattedDate = this.format(format, timestamp, now, utc); 115 let match = null; 116 if ((match = /^#(\d+)#(.*)/.exec(formattedDate)) != null) { 117 switch (match[1]) { 118 case '01': 119 formattedDate = this._getMessage('FD_LAST_SEEN_TOMORROW').replace('#TIME#', match[2]); 120 break; 121 case '02': 122 formattedDate = this._getMessage('FD_LAST_SEEN_NOW'); 123 break; 124 case '03': 125 formattedDate = this._getMessage('FD_LAST_SEEN_TODAY').replace('#TIME#', match[2]); 126 break; 127 case '04': 128 formattedDate = this._getMessage('FD_LAST_SEEN_YESTERDAY').replace('#TIME#', match[2]); 129 break; 130 case '05': 131 formattedDate = this._getMessage('FD_LAST_SEEN_MORE_YEAR'); 132 break; 133 default: 134 formattedDate = match[2]; 135 break; 136 } 137 } 138 return formattedDate; 139 } 140 /** 141 * The method is designed to replace the localization storage on sites without Bitrix Framework. 142 * It gets overloaded with custom implementation: 143 * 144 * const CustomDate = Object.create(BX.Main.Date); 145 * CustomDate._getMessage = () => ...new implementation...; 146 * 147 * This class should get messages only via this method. 148 * Otherwise, the class won't work on sites without Bitrix Framework. 149 * 150 * @param message 151 * @returns {*} 152 * @private 153 */ 154 }, { 155 key: "_getMessage", 156 value: function _getMessage(message) { 157 return main_core.Loc.getMessage(message); 158 } 159 /** 160 * The method used to parse date from string by given format. 161 * 162 * @param {string} str - date in given format 163 * @param {boolean} isUTC - is date in UTC 164 * @param {string} formatDate - format of the date without time 165 * @param {string} formatDatetime - format of the date with time 166 * @returns {Date|null} - returns Date object if string was parsed or null 167 */ 168 }, { 169 key: "parse", 170 value: function parse(str, isUTC, formatDate, formatDatetime) { 171 if (main_core.Type.isStringFilled(str)) { 172 if (!formatDate) { 173 formatDate = this._getMessage('FORMAT_DATE'); 174 } 175 if (!formatDatetime) { 176 formatDatetime = this._getMessage('FORMAT_DATETIME'); 177 } 178 let regMonths = ''; 179 for (let i = 1; i <= 12; i++) { 180 regMonths = regMonths + '|' + this._getMessage('MON_' + i); 181 }
182 const expr = new RegExp('([0-9]+|[a-z]+' + regMonths + ')', 'ig'); 183 const aDate = str.match(expr); 184 let aFormat = formatDate.match(/(DD|MI|MMMM|MM|M|YYYY)/ig); 185 const aDateArgs = []; 186 const aFormatArgs = []; 187 const aResult = {}; 188 if (!aDate) { 189 return null; 190 } 191 if (aDate.length > aFormat.length) { 192 aFormat = formatDatetime.match(/(DD|MI|MMMM|MM|M|YYYY|HH|H|SS|TT|T|GG|G)/ig); 193 } 194 for (let i = 0, cnt = aDate.length; i < cnt; i++) { 195 if (aDate[i].trim() !== '') { 196 aDateArgs[aDateArgs.length] = aDate[i]; 197 } 198 } 199 for (let i = 0, cnt = aFormat.length; i < cnt; i++) { 200 if (aFormat[i].trim() !== '') { 201 aFormatArgs[aFormatArgs.length] = aFormat[i]; 202 } 203 } 204 let m = aFormatArgs.findIndex(item => item === 'MMMM'); 205 if (m > 0) { 206 aDateArgs[m] = this.getMonthIndex(aDateArgs[m]); 207 aFormatArgs[m] = 'MM'; 208 } else { 209 m = aFormatArgs.findIndex(item => item === 'M'); 210 if (m > 0) { 211 aDateArgs[m] = this.getMonthIndex(aDateArgs[m]); 212 aFormatArgs[m] = 'MM'; 213 } 214 } 215 for (let i = 0, cnt = aFormatArgs.length; i < cnt; i++) { 216 const k = aFormatArgs[i].toUpperCase(); 217 aResult[k] = k === 'T' || k === 'TT' ? aDateArgs[i] : parseInt(aDateArgs[i], 10); 218 } 219 if (aResult['DD'] > 0 && aResult['MM'] > 0 && aResult['YYYY'] > 0) { 220 const d = new Date(); 221 if (isUTC) { 222 d.setUTCDate(1); 223 d.setUTCFullYear(aResult['YYYY']); 224 d.setUTCMonth(aResult['MM'] - 1); 225 d.setUTCDate(aResult['DD']); 226 d.setUTCHours(0, 0, 0, 0); 227 } else { 228 d.setDate(1); 229 d.setFullYear(aResult['YYYY']); 230 d.setMonth(aResult['MM'] - 1); 231 d.setDate(aResult['DD']); 232 d.setHours(0, 0, 0, 0); 233 } 234 if ((!isNaN(aResult['HH']) || !isNaN(aResult['GG']) || !isNaN(aResult['H']) || !isNaN(aResult['G'])) && !isNaN(aResult['MI'])) { 235 if (!isNaN(aResult['H']) || !isNaN(aResult['G'])) { 236 const bPM = (aResult['T'] || aResult['TT'] || 'am').toUpperCase() === 'PM', 237 h = parseInt(aResult['H'] || aResult['G'] || 0, 10); 238 if (bPM) { 239 aResult['HH'] = h + (h === 12 ? 0 : 12); 240 } else { 241 aResult['HH'] = h < 12 ? h : 0; 242 } 243 } else { 244 aResult['HH'] = parseInt(aResult['HH'] || aResult['GG'] || 0, 10); 245 } 246 if (isNaN(aResult['SS'])) { 247 aResult['SS'] = 0; 248 } 249 if (isUTC) { 250 d.setUTCHours(aResult['HH'], aResult['MI'], aResult['SS']); 251 } else { 252 d.setHours(aResult['HH'], aResult['MI'], aResult['SS']); 253 } 254 } 255 return d; 256 } 257 } 258 return null; 259 } 260 }, { 261 key: "getMonthIndex", 262 value: function getMonthIndex(month) { 263 const q = month.toUpperCase(); 264 const wordMonthCut = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']; 265 const wordMonth = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']; 266 for (let i = 1; i <= 12; i++) { 267 if (q === this._getMessage('MON_' + i).toUpperCase() || q === this._getMessage('MONTH_' + i).toUpperCase() || q === this._getMessage('MONTH_' + i + '_S').toUpperCase() || q === wordMonthCut[i - 1].toUpperCase() || q === wordMonth[i - 1].toUpperCase()) { 268 return i; 269 } 270 } 271 return month; 272 } 273 }, { 274 key: "format", 275 value: function format(_format, timestamp, now, utc) { 276 /* 277 PHP to Javascript: 278 time() = new Date() 279 mktime(...) = new Date(...) 280 gmmktime(...) = new Date(Date.UTC(...)) 281 mktime(0,0,0, 1, 1, 1970) != 0 new Date(1970,0,1).getTime() != 0 282 gmmktime(0,0,0, 1, 1, 1970) == 0 new Date(Date.UTC(1970,0,1)).getTime() == 0 283 date('d.m.Y H:i:s') = BX.Main.Date.format('d.m.Y H:i:s') 284 gmdate('d.m.Y H:i:s') = BX.Main.Date.format('d.m.Y H:i:s', null, null, true); 285 */ 286 const date = main_core.Type.isDate(timestamp) ? new Date(timestamp.getTime()) : main_core.Type.isNumber(timestamp) ? new Date(timestamp * 1000) : new Date(); 287 const nowDate = main_core.Type.isDate(now) ? new Date(now.getTime()) : main_core.Type.isNumber(now) ? new Date(now * 1000) : new Date(); 288 const isUTC = !!utc; 289 // used in hoisting inner functions, like _formatDateInterval 290 const thisDateTimeFormat = this; 291 if (main_core.Type.isArray(_format)) { 292 return _formatDateInterval(_format, date, nowDate, isUTC); 293 } else { 294 if (!main_core.Type.isStringFilled(_format)) { 295 return ''; 296 } 297 } 298 const replaceMap = (_format.match(/{{([^{}]*)}}/g) || []).map(x => { 299 return (x.match(/[^{}]+/) || [''])[0]; 300 }); 301 if (replaceMap.length > 0) {
302 replaceMap.forEach((element, index) => { 303 _format = _format.replace('{{' + element + '}}', '{{' + index + '}}'); 304 }); 305 } 306 const formatRegex = /\\?(sago|iago|isago|Hago|dago|mago|Yago|sdiff|idiff|Hdiff|ddiff|mdiff|Ydiff|sshort|ishort|Hshort|dshort|mhort|Yshort|yesterday|today|tommorow|tomorrow|.)/gi; 307 const dateFormats = { 308 d: () => { 309 // Day of the month 01 to 31 310 return getDate(date).toString().padStart(2, '0'); 311 }, 312 D: () => { 313 //Mon through Sun 314 return this._getMessage('DOW_' + getDay(date)); 315 }, 316 j: () => { 317 //Day of the month 1 to 31 318 return getDate(date); 319 }, 320 l: () => { 321 //Sunday through Saturday 322 return this._getMessage('DAY_OF_WEEK_' + getDay(date)); 323 }, 324 N: () => { 325 //1 (for Monday) through 7 (for Sunday) 326 return getDay(date) || 7; 327 }, 328 S: () => { 329 //st, nd, rd or th. Works well with j 330 if (getDate(date) % 10 == 1 && getDate(date) != 11) { 331 return 'st'; 332 } else if (getDate(date) % 10 == 2 && getDate(date) != 12) { 333 return 'nd'; 334 } else if (getDate(date) % 10 == 3 && getDate(date) != 13) { 335 return 'rd'; 336 } else { 337 return 'th'; 338 } 339 }, 340 w: () => { 341 //0 (for Sunday) through 6 (for Saturday) 342 return getDay(date); 343 }, 344 z: () => { 345 //0 through 365 346 const firstDay = new Date(getFullYear(date), 0, 1); 347 const currentDay = new Date(getFullYear(date), getMonth(date), getDate(date)); 348 return Math.ceil((currentDay - firstDay) / (24 * 3600 * 1000)); 349 }, 350 W: () => { 351 //ISO-8601 week number of year 352 const newDate = new Date(date.getTime()); 353 const dayNumber = (getDay(date) + 6) % 7; 354 setDate(newDate, getDate(newDate) - dayNumber + 3); 355 const firstThursday = newDate.getTime(); 356 setMonth(newDate, 0, 1); 357 if (getDay(newDate) != 4) { 358 setMonth(newDate, 0, 1 + (4 - getDay(newDate) + 7) % 7); 359 } 360 const weekNumber = 1 + Math.ceil((firstThursday - newDate) / (7 * 24 * 3600 * 1000)); 361 return weekNumber.toString().padStart(2, '0'); 362 }, 363 F: () => { 364 //January through December 365 return this._getMessage('MONTH_' + (getMonth(date) + 1) + '_S'); 366 }, 367 f: () => { 368 //January through December 369 return this._getMessage('MONTH_' + (getMonth(date) + 1)); 370 }, 371 m: () => { 372 //Numeric representation of a month 01 through 12 373 return (getMonth(date) + 1).toString().padStart(2, '0'); 374 }, 375 M: () => { 376 //A short textual representation of a month, three letters Jan through Dec 377 return this._getMessage('MON_' + (getMonth(date) + 1)); 378 }, 379 n: () => { 380 //Numeric representation of a month 1 through 12 381 return getMonth(date) + 1; 382 }, 383 t: () => { 384 //Number of days in the given month 28 through 31 385 const lastMonthDay = isUTC ? new Date(Date.UTC(getFullYear(date), getMonth(date) + 1, 0)) : new Date(getFullYear(date), getMonth(date) + 1, 0); 386 return getDate(lastMonthDay); 387 }, 388 L: () => { 389 //1 if it is a leap year, 0 otherwise. 390 const year = getFullYear(date); 391 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? 1 : 0; 392 }, 393 o: () => { 394 //ISO-8601 year number 395 const correctDate = new Date(date.getTime()); 396 setDate(correctDate, getDate(correctDate) - (getDay(date) + 6) % 7 + 3); 397 return getFullYear(correctDate); 398 }, 399 Y: () => { 400 //A full numeric representation of a year, 4 digits 401 return getFullYear(date); 402 }, 403 y: () => { 404 //A two digit representation of a year 405 return getFullYear(date).toString().slice(2); 406 }, 407 a: () => { 408 //am or pm 409 return getHours(date) > 11 ? 'pm' : 'am'; 410 }, 411 A: () => { 412 //AM or PM 413 return getHours(date) > 11 ? 'PM' : 'AM'; 414 }, 415 B: () => { 416 //000 through 999 417 const swatch = (date.getUTCHours() + 1) % 24 + date.getUTCMinutes() / 60 + date.getUTCSeconds() / 3600; 418 return Math.floor(swatch * 1000 / 24).toString().padStart(3, '0'); 419 }, 420 g: () => { 421 //12-hour format of an hour without leading zeros 1 through 12 422 return getHours(date) % 12 || 12; 423 }, 424 G: () => { 425 //24-hour format of an hour without leading zeros 0 through 23 426 return getHours(date); 427 }, 428 h: () => { 429 //12-hour format of an hour with leading zeros 01 through 12 430 return (getHours(date) % 12 || 12).toString().padStart(2, '0'); 431 }, 432 H: () => { 433 //24-hour format of an hour with leading zeros 00 through 23 434 return getHours(date).toString().padStart(2, '0'); 435 }, 436 i: () => { 437 //Minutes with leading zeros 00 to 59 438 return getMinutes(date).toString().padStart(2, '0'); 439 }, 440 s: () => { 441 //Seconds, with leading zeros 00 through 59 442 return getSeconds(date).toString().padStart(2, '0'); 443 }, 444 u: () => { 445 //Microseconds 446 return (getMilliseconds(date) * 1000).toString().padStart(6, '0'); 447 }, 448 e: () => { 449 if (isUTC) { 450 return 'UTC'; 451 } 452 return ''; 453 }, 454 I: () => { 455 if (isUTC) { 456 return 0; 457 } 458 459 //Whether or not the date is in daylight saving time 1 if Daylight Saving Time, 0 otherwise 460 const firstJanuary = new Date(getFullYear(date), 0, 1); 461 const firstJanuaryUTC = Date.UTC(getFullYear(date), 0, 1); 462 const firstJuly = new Date(getFullYear(date), 6, 0); 463 const firstJulyUTC = Date.UTC(getFullYear(date), 6, 0); 464 return 0 + (firstJanuary - firstJanuaryUTC !== firstJuly - firstJulyUTC); 465 }, 466 O: () => { 467 if (isUTC) { 468 return '+0000'; 469 } 470 471 //Difference to Greenwich time (GMT) in hours +0200 472 const timezoneOffset = date.getTimezoneOffset(); 473 const timezoneOffsetAbs = Math.abs(timezoneOffset); 474 return (timezoneOffset > 0 ? '-' : '+') + (Math.floor(timezoneOffsetAbs / 60) * 100 + timezoneOffsetAbs % 60).toString().padStart(4, '0'); 475 }, 476 //this method references 'O' method of the same object, arrow function is not suitable here 477 P: function () { 478 if (isUTC) { 479 return '+00:00'; 480 } 481 482 //Difference to Greenwich time (GMT) with colon between hours and minutes +02:00 483 const difference = this.O(); 484 return difference.substr(0, 3) + ':' + difference.substr(3); 485 }, 486 Z: () => { 487 if (isUTC) { 488 return 0; 489 } 490 //Timezone offset in seconds. The offset for timezones west of UTC is always negative, 491 //and for those east of UTC is always positive. 492 return -date.getTimezoneOffset() * 60; 493 }, 494 c: () => { 495 //ISO 8601 date 496 return 'Y-m-d\\TH:i:sP'.replace(formatRegex, _replaceDateFormat); 497 }, 498 r: () => { 499 //RFC 2822 formatted date 500 return 'D, d M Y H:i:s O'.replace(formatRegex, _replaceDateFormat); 501 }, 502 U: () => { 503 //Seconds since the Unix Epoch 504 return Math.floor(date.getTime() / 1000); 505 }, 506 sago: () => { 507 return _formatDateMessage(intval((nowDate - date) / 1000), { 508 '0': 'FD_SECOND_AGO_0', 509 '1': 'FD_SECOND_AGO_1', 510 '10_20': 'FD_SECOND_AGO_10_20', 511 'MOD_1': 'FD_SECOND_AGO_MOD_1', 512 'MOD_2_4': 'FD_SECOND_AGO_MOD_2_4', 513 'MOD_OTHER': 'FD_SECOND_AGO_MOD_OTHER' 514 }); 515 }, 516 sdiff: () => { 517 return _formatDateMessage(intval((nowDate - date) / 1000), { 518 '0': 'FD_SECOND_DIFF_0', 519 '1': 'FD_SECOND_DIFF_1', 520 '10_20': 'FD_SECOND_DIFF_10_20', 521 'MOD_1': 'FD_SECOND_DIFF_MOD_1', 522 'MOD_2_4': 'FD_SECOND_DIFF_MOD_2_4', 523 'MOD_OTHER': 'FD_SECOND_DIFF_MOD_OTHER' 524 }); 525 },
526 sshort: () => { 527 return this._getMessage('FD_SECOND_SHORT').replace(/#VALUE#/g, intval((nowDate - date) / 1000)); 528 }, 529 iago: () => { 530 return _formatDateMessage(intval((nowDate - date) / 60 / 1000), { 531 '0': 'FD_MINUTE_AGO_0', 532 '1': 'FD_MINUTE_AGO_1', 533 '10_20': 'FD_MINUTE_AGO_10_20', 534 'MOD_1': 'FD_MINUTE_AGO_MOD_1', 535 'MOD_2_4': 'FD_MINUTE_AGO_MOD_2_4', 536 'MOD_OTHER': 'FD_MINUTE_AGO_MOD_OTHER' 537 }); 538 }, 539 idiff: () => { 540 return _formatDateMessage(intval((nowDate - date) / 60 / 1000), { 541 '0': 'FD_MINUTE_DIFF_0', 542 '1': 'FD_MINUTE_DIFF_1', 543 '10_20': 'FD_MINUTE_DIFF_10_20', 544 'MOD_1': 'FD_MINUTE_DIFF_MOD_1', 545 'MOD_2_4': 'FD_MINUTE_DIFF_MOD_2_4', 546 'MOD_OTHER': 'FD_MINUTE_DIFF_MOD_OTHER' 547 }); 548 }, 549 isago: () => { 550 const minutesAgo = intval((nowDate - date) / 60 / 1000); 551 let result = _formatDateMessage(minutesAgo, { 552 '0': 'FD_MINUTE_0', 553 '1': 'FD_MINUTE_1', 554 '10_20': 'FD_MINUTE_10_20', 555 'MOD_1': 'FD_MINUTE_MOD_1', 556 'MOD_2_4': 'FD_MINUTE_MOD_2_4', 557 'MOD_OTHER': 'FD_MINUTE_MOD_OTHER' 558 }); 559 result += ' '; 560 const secondsAgo = intval((nowDate - date) / 1000) - minutesAgo * 60; 561 result += _formatDateMessage(secondsAgo, { 562 '0': 'FD_SECOND_AGO_0', 563 '1': 'FD_SECOND_AGO_1', 564 '10_20': 'FD_SECOND_AGO_10_20', 565 'MOD_1': 'FD_SECOND_AGO_MOD_1', 566 'MOD_2_4': 'FD_SECOND_AGO_MOD_2_4', 567 'MOD_OTHER': 'FD_SECOND_AGO_MOD_OTHER' 568 }); 569 return result; 570 }, 571 ishort: () => { 572 return this._getMessage('FD_MINUTE_SHORT').replace(/#VALUE#/g, intval((nowDate - date) / 60 / 1000)); 573 }, 574 Hago: () => { 575 return _formatDateMessage(intval((nowDate - date) / 60 / 60 / 1000), { 576 '0': 'FD_HOUR_AGO_0', 577 '1': 'FD_HOUR_AGO_1', 578 '10_20': 'FD_HOUR_AGO_10_20', 579 'MOD_1': 'FD_HOUR_AGO_MOD_1', 580 'MOD_2_4': 'FD_HOUR_AGO_MOD_2_4', 581 'MOD_OTHER': 'FD_HOUR_AGO_MOD_OTHER' 582 }); 583 }, 584 Hdiff: () => { 585 return _formatDateMessage(intval((nowDate - date) / 60 / 60 / 1000), { 586 '0': 'FD_HOUR_DIFF_0', 587 '1': 'FD_HOUR_DIFF_1', 588 '10_20': 'FD_HOUR_DIFF_10_20', 589 'MOD_1': 'FD_HOUR_DIFF_MOD_1', 590 'MOD_2_4': 'FD_HOUR_DIFF_MOD_2_4', 591 'MOD_OTHER': 'FD_HOUR_DIFF_MOD_OTHER' 592 }); 593 }, 594 Hshort: () => { 595 return this._getMessage('FD_HOUR_SHORT').replace(/#VALUE#/g, intval((nowDate - date) / 60 / 60 / 1000)); 596 }, 597 yesterday: () => { 598 return this._getMessage('FD_YESTERDAY'); 599 }, 600 today: () => { 601 return this._getMessage('FD_TODAY'); 602 }, 603 tommorow: () => { 604 return this._getMessage('FD_TOMORROW'); 605 }, 606 tomorrow: () => { 607 return this._getMessage('FD_TOMORROW'); 608 }, 609 dago: () => { 610 return _formatDateMessage(intval((nowDate - date) / 60 / 60 / 24 / 1000), { 611 '0': 'FD_DAY_AGO_0', 612 '1': 'FD_DAY_AGO_1', 613 '10_20': 'FD_DAY_AGO_10_20', 614 'MOD_1': 'FD_DAY_AGO_MOD_1', 615 'MOD_2_4': 'FD_DAY_AGO_MOD_2_4', 616 'MOD_OTHER': 'FD_DAY_AGO_MOD_OTHER' 617 }); 618 }, 619 ddiff: () => { 620 return _formatDateMessage(intval((nowDate - date) / 60 / 60 / 24 / 1000), { 621 '0': 'FD_DAY_DIFF_0', 622 '1': 'FD_DAY_DIFF_1', 623 '10_20': 'FD_DAY_DIFF_10_20', 624 'MOD_1': 'FD_DAY_DIFF_MOD_1', 625 'MOD_2_4': 'FD_DAY_DIFF_MOD_2_4', 626 'MOD_OTHER': 'FD_DAY_DIFF_MOD_OTHER' 627 }); 628 }, 629 dshort: () => { 630 return this._getMessage('FD_DAY_SHORT').replace(/#VALUE#/g, intval((nowDate - date) / 60 / 60 / 24 / 1000)); 631 }, 632 mago: () => { 633 return _formatDateMessage(intval((nowDate - date) / 60 / 60 / 24 / 31 / 1000), { 634 '0': 'FD_MONTH_AGO_0', 635 '1': 'FD_MONTH_AGO_1', 636 '10_20': 'FD_MONTH_AGO_10_20', 637 'MOD_1': 'FD_MONTH_AGO_MOD_1', 638 'MOD_2_4': 'FD_MONTH_AGO_MOD_2_4', 639 'MOD_OTHER': 'FD_MONTH_AGO_MOD_OTHER' 640 }); 641 }, 642 mdiff: () => { 643 return _formatDateMessage(intval((nowDate - date) / 60 / 60 / 24 / 31 / 1000), { 644 '0': 'FD_MONTH_DIFF_0', 645 '1': 'FD_MONTH_DIFF_1', 646 '10_20': 'FD_MONTH_DIFF_10_20', 647 'MOD_1': 'FD_MONTH_DIFF_MOD_1', 648 'MOD_2_4': 'FD_MONTH_DIFF_MOD_2_4', 649 'MOD_OTHER': 'FD_MONTH_DIFF_MOD_OTHER' 650 }); 651 }, 652 mshort: () => { 653 return this._getMessage('FD_MONTH_SHORT').replace(/#VALUE#/g, intval((nowDate - date) / 60 / 60 / 24 / 31 / 1000)); 654 }, 655 Yago: () => { 656 return _formatDateMessage(intval((nowDate - date) / 60 / 60 / 24 / 365 / 1000), { 657 '0': 'FD_YEARS_AGO_0', 658 '1': 'FD_YEARS_AGO_1', 659 '10_20': 'FD_YEARS_AGO_10_20', 660 'MOD_1': 'FD_YEARS_AGO_MOD_1', 661 'MOD_2_4': 'FD_YEARS_AGO_MOD_2_4', 662 'MOD_OTHER': 'FD_YEARS_AGO_MOD_OTHER' 663 }); 664 }, 665 Ydiff: () => { 666 return _formatDateMessage(intval((nowDate - date) / 60 / 60 / 24 / 365 / 1000), { 667 '0': 'FD_YEARS_DIFF_0', 668 '1': 'FD_YEARS_DIFF_1', 669 '10_20': 'FD_YEARS_DIFF_10_20', 670 'MOD_1': 'FD_YEARS_DIFF_MOD_1', 671 'MOD_2_4': 'FD_YEARS_DIFF_MOD_2_4', 672 'MOD_OTHER': 'FD_YEARS_DIFF_MOD_OTHER' 673 }); 674 }, 675 Yshort: () => { 676 return _formatDateMessage(intval((nowDate - date) / 60 / 60 / 24 / 365 / 1000), { 677 '0': 'FD_YEARS_SHORT_0', 678 '1': 'FD_YEARS_SHORT_1', 679 '10_20': 'FD_YEARS_SHORT_10_20', 680 'MOD_1': 'FD_YEARS_SHORT_MOD_1', 681 'MOD_2_4': 'FD_YEARS_SHORT_MOD_2_4', 682 'MOD_OTHER': 'FD_YEARS_SHORT_MOD_OTHER' 683 }); 684 }, 685 x: () => { 686 const ampm = this.isAmPmMode(true); 687 const timeFormat = ampm === this.AM_PM_MODE.LOWER ? 'g:i a' : ampm === this.AM_PM_MODE.UPPER ? 'g:i A' : 'H:i'; 688 return this.format([['tomorrow', 'tomorrow, ' + timeFormat], ['-', this.convertBitrixFormat(this._getMessage('FORMAT_DATETIME')).replace(/:s/g, '')], ['
688s', 'sago'], ['i', 'iago'], ['today', 'today, ' + timeFormat], ['yesterday', 'yesterday, ' + timeFormat], ['', this.convertBitrixFormat(this._getMessage('FORMAT_DATETIME')).replace(/:s/g, '')]], date, nowDate, isUTC); 689 }, 690 X: () => { 691 const ampm = this.isAmPmMode(true); 692 const timeFormat = ampm === this.AM_PM_MODE.LOWER ? 'g:i a' : ampm === this.AM_PM_MODE.UPPER ? 'g:i A' : 'H:i'; 693 const day = this.format([['tomorrow', 'tomorrow'], ['-', this.convertBitrixFormat(this._getMessage('FORMAT_DATE'))], ['today', 'today'], ['yesterday', 'yesterday'], ['', this.convertBitrixFormat(this._getMessage('FORMAT_DATE'))]], date, nowDate, isUTC); 694 const time = this.format([['tomorrow', timeFormat], ['today', timeFormat], ['yesterday', timeFormat], ['', '']], date, nowDate, isUTC); 695 if (time.length > 0) { 696 return this._getMessage('FD_DAY_AT_TIME').replace(/#DAY#/g, day).replace(/#TIME#/g, time); 697 } else { 698 return day; 699 } 700 }, 701 Q: () => { 702 const daysAgo = intval((nowDate - date) / 60 / 60 / 24 / 1000); 703 if (daysAgo == 0) { 704 return this._getMessage('FD_DAY_DIFF_1').replace(/#VALUE#/g, 1); 705 } else { 706 return this.format([['d', 'ddiff'], ['m', 'mdiff'], ['', 'Ydiff']], date, nowDate); 707 } 708 } 709 }; 710 let cutZeroTime = false; 711 if (_format[0] && _format[0] == '^') { 712 cutZeroTime = true; 713 _format = _format.substr(1); 714 } 715 let result = _format.replace(formatRegex, _replaceDateFormat); 716 if (cutZeroTime) { 717 /* 15.04.12 13:00:00 => 15.04.12 13:00 718 00:01:00 => 00:01 719 4 may 00:00:00 => 4 may 720 01-01-12 00:00 => 01-01-12 721 */ 722 723 result = result.replace(/\s*00:00:00\s*/g, '').replace(/(\d\d:\d\d)(:00)/g, '$1').replace(/(\s*00:00\s*)(?!:)/g, ''); 724 } 725 if (replaceMap.length > 0) { 726 replaceMap.forEach(function (element, index) { 727 result = result.replace('{{' + index + '}}', element); 728 }); 729 } 730 return result; 731 function _formatDateInterval(formats, date, nowDate, isUTC) { 732 const secondsAgo = intval((nowDate - date) / 1000); 733 for (let i = 0; i < formats.length; i++) { 734 const formatInterval = formats[i][0]; 735 const formatValue = formats[i][1]; 736 let match = null; 737 if (formatInterval == 's') { 738 if (secondsAgo < 60) { 739 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 740 } 741 } else if ((match = /^s(\d+)\>?(\d+)?/.exec(formatInterval)) != null) { 742 if (match[1] && match[2]) { 743 if (secondsAgo < match[1] && secondsAgo > match[2]) { 744 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 745 } 746 } else if (secondsAgo < match[1]) { 747 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 748 } 749 } else if (formatInterval == 'i') { 750 if (secondsAgo < 60 * 60) { 751 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 752 } 753 } else if ((match = /^i(\d+)\>?(\d+)?/.exec(formatInterval)) != null) { 754 if (match[1] && match[2]) { 755 if (secondsAgo < match[1] * 60 && secondsAgo > match[2] * 60) { 756 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 757 } 758 } else if (secondsAgo < match[1] * 60) { 759 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 760 } 761 } else if (formatInterval == 'H') { 762 if (secondsAgo < 24 * 60 * 60) { 763 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 764 } 765 } else if ((match = /^H(\d+)\>?(\d+)?/.exec(formatInterval)) != null) { 766 if (match[1] && match[2]) { 767 if (secondsAgo < match[1] * 60 * 60 && secondsAgo > match[2] * 60 * 60) { 768 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 769 } 770 } else if (secondsAgo < match[1] * 60 * 60) { 771 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 772 } 773 } else if (formatInterval == 'd') { 774 if (secondsAgo < 31 * 24 * 60 * 60) { 775 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 776 } 777 } else if ((match = /^d(\d+)\>?(\d+)?/.exec(formatInterval)) != null) { 778 if (match[1] && match[2]) { 779 if (secondsAgo < match[1] * 24 * 60 * 60 && secondsAgo > match[2] * 24 * 60 * 60) { 780 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 781 } 782 } else if (secondsAgo < match[1] * 24 * 60 * 60) { 783 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 784 } 785 } else if (formatInterval == 'm') { 786 if (secondsAgo < 365 * 24 * 60 * 60) { 787 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 788 } 789 } else if ((match = /^m(\d+)\>?(\d+)?/.exec(formatInterval)) != null) { 790 if (match[1] && match[2]) { 791 if (secondsAgo < match[1] * 31 * 24 * 60 * 60 && secondsAgo > match[2] * 31 * 24 * 60 * 60) { 792 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 793 } 794 } else if (secondsAgo < match[1] * 31 * 24 * 60 * 60) { 795 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 796 } 797 } else if (formatInterval == 'now') { 798 if (date.getTime() == nowDate.getTime()) { 799 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 800 } 801 } else if (formatInterval == 'today') { 802 const year = getFullYear(nowDate);
803 const month = getMonth(nowDate); 804 const day = getDate(nowDate); 805 const todayStart = isUTC ? new Date(Date.UTC(year, month, day, 0, 0, 0, 0)) : new Date(year, month, day, 0, 0, 0, 0); 806 const todayEnd = isUTC ? new Date(Date.UTC(year, month, day + 1, 0, 0, 0, 0)) : new Date(year, month, day + 1, 0, 0, 0, 0); 807 if (date >= todayStart && date < todayEnd) { 808 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 809 } 810 } else if (formatInterval == 'todayFuture') { 811 const year = getFullYear(nowDate); 812 const month = getMonth(nowDate); 813 const day = getDate(nowDate); 814 const todayStart = nowDate.getTime(); 815 const todayEnd = isUTC ? new Date(Date.UTC(year, month, day + 1, 0, 0, 0, 0)) : new Date(year, month, day + 1, 0, 0, 0, 0); 816 if (date >= todayStart && date < todayEnd) { 817 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 818 } 819 } else if (formatInterval == 'yesterday') { 820 const year = getFullYear(nowDate);
821 const month = getMonth(nowDate); 822 const day = getDate(nowDate); 823 const yesterdayStart = isUTC ? new Date(Date.UTC(year, month, day - 1, 0, 0, 0, 0)) : new Date(year, month, day - 1, 0, 0, 0, 0); 824 const yesterdayEnd = isUTC ? new Date(Date.UTC(year, month, day, 0, 0, 0, 0)) : new Date(year, month, day, 0, 0, 0, 0); 825 if (date >= yesterdayStart && date < yesterdayEnd) { 826 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 827 } 828 } else if (formatInterval == 'tommorow' || formatInterval == 'tomorrow') { 829 const year = getFullYear(nowDate); 830 const month = getMonth(nowDate); 831 const day = getDate(nowDate); 832 const tomorrowStart = isUTC ? new Date(Date.UTC(year, month, day + 1, 0, 0, 0, 0)) : new Date(year, month, day + 1, 0, 0, 0, 0); 833 const tomorrowEnd = isUTC ? new Date(Date.UTC(year, month, day + 2, 0, 0, 0, 0)) : new Date(year, month, day + 2, 0, 0, 0, 0); 834 if (date >= tomorrowStart && date < tomorrowEnd) { 835 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 836 } 837 } else if (formatInterval == '-') { 838 if (secondsAgo < 0) { 839 return thisDateTimeFormat.format(formatValue, date, nowDate, isUTC); 840 } 841 } 842 } 843 844 //return formats.length > 0 ? thisDateTimeFormat.format(formats.pop()[1], date, nowDate, isUTC) : ''; 845 return formats.length > 0 ? thisDateTimeFormat.format(formats[formats.length - 1][1], date, nowDate, isUTC) : ''; 846 } 847 function getFullYear(date) { 848 return isUTC ? date.getUTCFullYear() : date.getFullYear(); 849 } 850 function getDate(date) { 851 return isUTC ? date.getUTCDate() : date.getDate(); 852 } 853 function getMonth(date) { 854 return isUTC ? date.getUTCMonth() : date.getMonth(); 855 } 856 function getHours(date) { 857 return isUTC ? date.getUTCHours() : date.getHours(); 858 } 859 function getMinutes(date) { 860 return isUTC ? date.getUTCMinutes() : date.getMinutes(); 861 } 862 function getSeconds(date) { 863 return isUTC ? date.getUTCSeconds() : date.getSeconds(); 864 } 865 function getMilliseconds(date) { 866 return isUTC ? date.getUTCMilliseconds() : date.getMilliseconds(); 867 } 868 function getDay(date) { 869 return isUTC ? date.getUTCDay() : date.getDay(); 870 } 871 function setDate(date, dayValue) { 872 return isUTC ? date.setUTCDate(dayValue) : date.setDate(dayValue); 873 } 874 function setMonth(date, monthValue, dayValue) { 875 return isUTC ? date.setUTCMonth(monthValue, dayValue) : date.setMonth(monthValue, dayValue); 876 } 877 function _formatDateMessage(value, messages) { 878 const val = value < 100 ? Math.abs(value) : Math.abs(value % 100); 879 const dec = val % 10; 880 let message = ''; 881 if (val == 0) { 882 message = thisDateTimeFormat._getMessage(messages['0']); 883 } else if (val == 1) { 884 message = thisDateTimeFormat._getMessage(messages['1']); 885 } else if (val >= 10 && val <= 20) { 886 message = thisDateTimeFormat._getMessage(messages['10_20']); 887 } else if (dec == 1) { 888 message = thisDateTimeFormat._getMessage(messages['MOD_1']); 889 } else if (2 <= dec && dec <= 4) { 890 message = thisDateTimeFormat._getMessage(messages['MOD_2_4']); 891 } else { 892 message = thisDateTimeFormat._getMessage(messages['MOD_OTHER']); 893 } 894 return message.replace(/#VALUE#/g, value); 895 } 896 function _replaceDateFormat(match, matchFull) { 897 if (dateFormats[match]) { 898 return dateFormats[match](); 899 } else { 900 return matchFull; 901 } 902 } 903 function intval(number) { 904 return number >= 0 ? Math.floor(number) : Math.ceil(number); 905 } 906 } 907 }]); 908 return DateTimeFormat; 909 }(); 910 babelHelpers.defineProperty(DateTimeFormat, "AM_PM_MODE", { 911 UPPER: 1, 912 LOWER: 2, 913 NONE: false 914 }); 915 babelHelpers.defineProperty(DateTimeFormat, "convertBitrixFormat", convertBitrixFormat); 916 babelHelpers.defineProperty(DateTimeFormat, "getFormat", getFormat); 917 918 const cache = new main_core.Cache.MemoryCache(); 919 /** 920 * @memberOf BX.Main.Timezone 921 * 922 * WARNING! Don't use this class or any classes from Timezone namespace on sites without Bitrix Framework. 923 * It is not designed to handle this case and will definitely break. 924 */ 925 const Offset = { 926 get SERVER_TO_UTC() { 927 return cache.remember('SERVER_TO_UTC', () => { 928 return main_core.Text.toInteger(main_core.Loc.getMessage('SERVER_TZ_OFFSET')); 929 }); 930 }, 931 get USER_TO_SERVER() { 932 return cache.remember('USER_TO_SERVER', () => { 933 return main_core.Text.toInteger(main_core.Loc.getMessage('USER_TZ_OFFSET')); 934 }); 935 }, 936 // Date returns timezone offset in minutes by default, change it to seconds 937 // Also offset is negative in UTC+ timezones and positive in UTC- timezones. 938 // By convention Bitrix uses the opposite approach, so change offset sign. 939 get BROWSER_TO_UTC() { 940 return cache.remember('BROWSER_TO_UTC', () => { 941 const offset = main_core.Text.toInteger(new Date().getTimezoneOffset() * 60); 942 return -offset; 943 }); 944 } 945 }; 946 Object.freeze(Offset); 947 948 function normalizeTimeValue(timeValue) { 949 if (main_core.Type.isDate(timeValue)) { 950 return getTimestampFromDate(timeValue); 951 } 952 return main_core.Text.toInteger(timeValue); 953 } 954 function createDateFromTimestamp(timestampInSeconds) { 955 return new Date(timestampInSeconds * 1000); 956 } 957 function getTimestampFromDate(date) { 958 return Math.floor(date.getTime() / 1000); 959 } 960 961 let offset = Offset; 962 let now = null; 963 function getOffset() { 964 return offset; 965 } 966 function getNowTimestamp() { 967 var _now; 968 return (_now = now) !== null && _now !== void 0 ? _now : getTimestampFromDate(new Date()); 969 } 970 971 /** 972 * @memberOf BX.Main.Timezone 973 * 974 * WARNING! Don't use this class or any classes from Timezone namespace on sites without Bitrix Framework. 975 * It is not designed to handle this case and will definitely break. 976 */ 977 let BrowserTime = /*#__PURE__*/function () { 978 function BrowserTime() { 979 babelHelpers.classCallCheck(this, BrowserTime); 980 } 981 babelHelpers.createClass(BrowserTime, null, [{ 982 key: "getDate", 983 /** 984 * Returns a Date object with time and date that represent a specific moment in Browser (device) timezone. 985 * 986 * @param utcTimestamp - normal utc timestamp in seconds. 'now' by default 987 * @returns {Date} 988 */ 989 value: function getDate(utcTimestamp = null) { 990 const timestamp = main_core.Type.isNumber(utcTimestamp) ? utcTimestamp : this.getTimestamp(); 991 return createDateFromTimestamp(timestamp); 992 } 993 /** 994 * Transforms a moment in Browser (device) timezone to a moment in User timezone. 995 * 996 * ATTENTION! Date.getTime() and Date.getTimezoneOffset() will return inaccurate data. Since a native Date object 997 * doesn't support timezone other that device timezone, we have to manually change timestamp to shift time value in 998 * a Date object. 999 * 1000 * @param browserTime - a moment in Browser (device) timezone. Either a Date object (recommended way). Or timestamp
1001 * in seconds in Browser timezone (see this.getTimestamp for details). 1002 * @returns {Date} 1003 */ 1004 }, { 1005 key: "toUserDate", 1006 value: function toUserDate(browserTime) { 1007 return createDateFromTimestamp(this.toUser(browserTime)); 1008 } 1009 /** 1010 * Transforms a moment in Browser (device) timezone to a moment in Server timezone. 1011 * 1012 * ATTENTION! Date.getTime() and Date.getTimezoneOffset() will return inaccurate data. Since a native Date object 1013 * doesn't support timezone other that device timezone, we have to manually change timestamp to shift time value in 1014 * a Date object. 1015 * 1016 * @param browserTime - a moment in Browser (device) timezone. Either a Date object (recommended way). Or timestamp 1017 * in seconds in Browser timezone (see this.getTimestamp for details). 1018 * @returns {Date} 1019 */ 1020 }, { 1021 key: "toServerDate", 1022 value: function toServerDate(browserTime) { 1023 return createDateFromTimestamp(this.toServer(browserTime)); 1024 } 1025 /** 1026 * Transforms a moment in Browser (device) timezone to a timestamp in User timezone. 1027 * It's recommended to use this.toUserDate for more clear code. 1028 * 1029 * @param browserTime - a moment in Browser timezone. Either a Date object (recommended way). Or timestamp in seconds 1030 * in Browser timezone (see this.getTimestamp for details). 1031 * @returns {number} - timestamp that when passed to 'new Date' will create an object with absolute time matching 1032 * the time in User timezone 1033 */ 1034 }, { 1035 key: "toUser", 1036 value: function toUser(browserTime) { 1037 return this.toServer(browserTime) + getOffset().USER_TO_SERVER; 1038 } 1039 /** 1040 * Transforms a moment in Browser (device) timezone to a timestamp in Server timezone. 1041 * It's recommended to use this.toServerDate for more clear code. 1042 * 1043 * @param browserTime - a moment in Browser timezone. Either a Date object (recommended way). Or timestamp in seconds 1044 * in Browser timezone (see this.getTimestamp for details). 1045 * @returns {number} - timestamp that when passed to 'new Date' will create an object with absolute time matching 1046 * the time in Server timezone 1047 */ 1048 }, { 1049 key: "toServer", 1050 value: function toServer(browserTime) { 1051 return normalizeTimeValue(browserTime) - getOffset().BROWSER_TO_UTC + getOffset().SERVER_TO_UTC; 1052 } 1053 /** 1054 * Returns 'now' timestamp in Browser (device) timezone - when it's passed to a 'new Date', it will create an object 1055 * with absolute time matching the time as if it was in Browser (device) timezone. 1056 * 1057 * @returns {number} 1058 */ 1059 }, { 1060 key: "getTimestamp", 1061 value: function getTimestamp() { 1062 // since 'Date' class in JS is hardcoded to use device timezone, 'browser timestamp' is just normal UTC timestamp :) 1063 1064 return getNowTimestamp(); 1065 } 1066 }]); 1067 return BrowserTime; 1068 }(); 1069 1070 /** 1071 * @memberOf BX.Main.Timezone 1072 * 1073 * WARNING! Don't use this class or any classes from Timezone namespace on sites without Bitrix Framework. 1074 * It is not designed to handle this case and will definitely break. 1075 */ 1076 let ServerTime = /*#__PURE__*/function () { 1077 function ServerTime() { 1078 babelHelpers.classCallCheck(this, ServerTime); 1079 } 1080 babelHelpers.createClass(ServerTime, null, [{ 1081 key: "getDate", 1082 /** 1083 * Returns a Date object with time and date that represent a specific moment in Server timezone. 1084 * 1085 * ATTENTION! Date.getTime() and Date.getTimezoneOffset() will return inaccurate data. Since a native Date object 1086 * doesn't support timezone other that device timezone, we have to manually change timestamp to shift time value in 1087 * a Date object. 1088 * 1089 * @param utcTimestamp - normal utc timestamp in seconds. 'now' by default 1090 * @returns {Date} 1091 */ 1092 value: function getDate(utcTimestamp = null) { 1093 if (main_core.Type.isNumber(utcTimestamp)) { 1094 const browserToServerOffset = getOffset().SERVER_TO_UTC - getOffset().BROWSER_TO_UTC; 1095 return createDateFromTimestamp(utcTimestamp + browserToServerOffset); 1096 } 1097 return BrowserTime.toServerDate(BrowserTime.getDate()); 1098 } 1099 /** 1100 * Transforms a moment in Server timezone to a moment in User timezone. 1101 * 1102 * ATTENTION! Date.getTime() and Date.getTimezoneOffset() will return inaccurate data. Since a native Date object 1103 * doesn't support timezone other that device timezone, we have to manually change timestamp to shift time value in 1104 * a Date object. 1105 * 1106 * @param serverTime - a moment in Server timezone. Either a Date object (recommended way). Or timestamp in seconds
1106in 1107 * Server timezone (see this.getTimestamp for details). 1108 * @returns {Date} 1109 */ 1110 }, { 1111 key: "toUserDate", 1112 value: function toUserDate(serverTime) { 1113 return createDateFromTimestamp(this.toUser(serverTime)); 1114 } 1115 /** 1116 * Transforms a moment in Server timezone to a moment in Browser (device) timezone. 1117 * 1118 * @param serverTime - a moment in Server timezone. Either a Date object (recommended way). Or timestamp in seconds in 1119 * Server timezone (see this.getTimestamp for details). 1120 * @returns {Date} 1121 */ 1122 }, { 1123 key: "toBrowserDate", 1124 value: function toBrowserDate(serverTime) { 1125 return createDateFromTimestamp(this.toBrowser(serverTime)); 1126 } 1127 /** 1128 * Transforms a moment in Server timezone to a timestamp in User timezone. 1129 * It's recommended to use this.toServerDate for more clear code. 1130 * 1131 * @param serverTime - a moment in Server timezone. Either a Date object (recommended way). Or timestamp in seconds in 1132 * Server timezone (see this.getTimestamp for details). 1133 * @returns {number} - timestamp that when passed to 'new Date' will create an object with absolute time matching 1134 * the time in User timezone 1135 */ 1136 }, { 1137 key: "toUser", 1138 value: function toUser(serverTime) { 1139 return normalizeTimeValue(serverTime) + getOffset().USER_TO_SERVER; 1140 } 1141 /** 1142 * Transforms a moment in Server timezone to a timestamp in Browser (device) timezone. 1143 * It's recommended to use this.toBrowserDate for more clear code. 1144 * 1145 * @param serverTime - a moment in Server timezone. Either a Date object (recommended way). Or timestamp in seconds in 1146 * Server timezone (see this.getTimestamp for details). 1147 * @returns {number} - timestamp that when passed to 'new Date' will create an object with absolute time matching 1148 * the time in Browser (device) timezone 1149 */ 1150 }, { 1151 key: "toBrowser", 1152 value: function toBrowser(serverTime) { 1153 return normalizeTimeValue(serverTime) + getOffset().BROWSER_TO_UTC - getOffset().SERVER_TO_UTC; 1154 } 1155 /** 1156 * Returns 'now' timestamp in Server timezone - when it's passed to a 'new Date', it will create an object with 1157 * absolute time matching the time as if it was in Server timezone. 1158 * 1159 * @returns {number} 1160 */ 1161 }, { 1162 key: "getTimestamp", 1163 value: function getTimestamp() { 1164 return BrowserTime.toServer(BrowserTime.getTimestamp()); 1165 } 1166 }]); 1167 return ServerTime; 1168 }(); 1169 1170 function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { _classCheckPrivateStaticAccess(receiver, classConstructor); _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); return _classApplyDescriptorGet(receiver, descriptor); } 1171 function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { if (descriptor === undefined) { throw new TypeError("attempted to " + action + " private static field before its declaration"); } } 1172 function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } } 1173 function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; } 1174 1175 /** 1176 * @memberOf BX.Main.Timezone 1177 * 1178 * WARNING! Don't use this class or any classes from Timezone namespace on sites without Bitrix Framework. 1179 * It is not designed to handle this case and will definitely break. 1180 * 1181 * ATTENTION! In Bitrix user timezone !== browser timezone. Users can change their timezone from their profile settings 1182 * and the timezone will be different from browser timezone. 1183 */ 1184 let UserTime = /*#__PURE__*/function () { 1185 function UserTime() { 1186 babelHelpers.classCallCheck(this, UserTime); 1187 } 1188 babelHelpers.createClass(UserTime, null, [{ 1189 key: "getDate", 1190 /** 1191 * Returns a Date object with time and date that represent a specific moment in User timezone. 1192 * 1193 * ATTENTION! Date.getTime() and Date.getTimezoneOffset() will return inaccurate data. Since a native Date object 1194 * doesn't support timezone other that device timezone, we have to manually change timestamp to shift time value in 1195 * a Date object. 1196 * 1197 * @param utcTimestamp - normal utc timestamp in seconds. 'now' by default 1198 * @returns {Date} 1199 */ 1200 value: function getDate(utcTimestamp = null) { 1201 if (main_core.Type.isNumber(utcTimestamp)) { 1202 return createDateFromTimestamp(utcTimestamp + _classStaticPrivateFieldSpecGet(this, UserTime, _userToBrowserOffset)); 1203 } 1204 return createDateFromTimestamp(this.getTimestamp()); 1205 } 1206 }, { 1207 key: "toBrowserDate", 1208 /** 1209 * Transforms a moment in User timezone to a moment in Browser (device) timezone. 1210 * 1211 * @param userTime - a moment in User timezone. Either a Date object (recommended way). Or timestamp in seconds
1211in 1212 * User timezone (see this.getTimestamp for details). 1213 * @returns {Date} 1214 */ 1215 value: function toBrowserDate(userTime) { 1216 return createDateFromTimestamp(this.toBrowser(userTime)); 1217 } 1218 /** 1219 * Transforms a moment in User timezone to a moment in Server timezone. 1220 * 1221 * ATTENTION! Date.getTime() and Date.getTimezoneOffset() will return inaccurate data. Since a native Date object 1222 * doesn't support timezone other that device timezone, we have to manually change timestamp to shift time value in 1223 * a Date object. 1224 * 1225 * @param userTime - a moment in User timezone. Either a Date object (recommended way). Or timestamp in seconds in 1226 * User timezone (see this.getTimestamp for details). 1227 * @returns {Date} 1228 */ 1229 }, { 1230 key: "toServerDate", 1231 value: function toServerDate(userTime) { 1232 return createDateFromTimestamp(this.toServer(userTime)); 1233 } 1234 }, { 1235 key: "toUTCTimestamp", 1236 value: function toUTCTimestamp(userTime) { 1237 return normalizeTimeValue(userTime) - _classStaticPrivateFieldSpecGet(this, UserTime, _userToBrowserOffset); 1238 } 1239 /** 1240 * Transforms a moment in User timezone to a timestamp in Browser timezone. 1241 * It's recommended to use this.toBrowserDate for more clear code. 1242 * 1243 * @param userTime - a moment in User timezone. Either a Date object (recommended way). Or timestamp in seconds in 1244 * User timezone (see this.getTimestamp for details). 1245 * @returns {number} - timestamp that when passed to 'new Date' will create an object with absolute time matching 1246 * the time in Browser (device) timezone 1247 */ 1248 }, { 1249 key: "toBrowser", 1250 value: function toBrowser(userTime) { 1251 return normalizeTimeValue(userTime) + getOffset().BROWSER_TO_UTC - getOffset().SERVER_TO_UTC - getOffset().USER_TO_SERVER; 1252 } 1253 /** 1254 * Transforms a moment in User timezone to a timestamp in Server timezone. 1255 * It's recommended to use this.toServerDate for more clear code. 1256 * 1257 * @param userTime - a moment in User timezone. Either a Date object (recommended way). Or timestamp in seconds in 1258 * User timezone (see this.getTimestamp for details). 1259 * @returns {number} - timestamp that when passed to 'new Date' will create an object with absolute time matching 1260 * the time in Server timezone 1261 */ 1262 }, { 1263 key: "toServer", 1264 value: function toServer(userTime) { 1265 return normalizeTimeValue(userTime) - getOffset().USER_TO_SERVER; 1266 } 1267 /** 1268 * Returns 'now' timestamp in User timezone - when it's passed to a 'new Date', it will create an object with absolute 1269 * time matching the time as if it was in User timezone. 1270 * 1271 * @returns {number} 1272 */ 1273 }, { 1274 key: "getTimestamp", 1275 value: function getTimestamp() { 1276 return BrowserTime.toUser(BrowserTime.getTimestamp()); 1277 } 1278 }]); 1279 return UserTime; 1280 }(); 1281 function _get_userToBrowserOffset() { 1282 const userToUTCOffset = getOffset().SERVER_TO_UTC + getOffset().USER_TO_SERVER; 1283 return userToUTCOffset - getOffset().BROWSER_TO_UTC; 1284 } 1285 var _userToBrowserOffset = { 1286 get: _get_userToBrowserOffset, 1287 set: void 0 1288 }; 1289 1290 // compatibility alias 1291 const Date$1 = DateTimeFormat; 1292 const Timezone = Object.freeze({ 1293 Offset, 1294 BrowserTime, 1295 UserTime, 1296 ServerTime 1297 }); 1298 1299 exports.DateTimeFormat = DateTimeFormat; 1300 exports.Date = Date$1; 1301 exports.Timezone = Timezone; 1302 1303}((this.BX.Main = this.BX.Main || {}),BX)); 1304//# sourceMappingURL=main.date.js.map
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.