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|mshort|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 function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); } 919 function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } 920 function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; } 921 922 /** 923 * Available units: `Y` - years, `m` - months, `d` - days, `H` - hours, `i` - minutes, `s` - seconds. 924 */ 925 926 const defaultOptions = { 927 format: 'Y m d H i s', 928 style: 'long' 929 }; 930 var _getSeparator = /*#__PURE__*/new WeakSet(); 931 var _getMaxUnit = /*#__PURE__*/new WeakSet(); 932 var _formatUnit = /*#__PURE__*/new WeakSet(); 933 var _getUnitPropertyModByFormat = /*#__PURE__*/new WeakSet(); 934 var _getUnitPropertyByFormat = /*#__PURE__*/new WeakSet(); 935 var _getUnitDuration = /*#__PURE__*/new WeakSet(); 936 let DurationFormat = /*#__PURE__*/function () { 937 function DurationFormat(milliseconds) { 938 babelHelpers.classCallCheck(this, DurationFormat); 939 _classPrivateMethodInitSpec(this, _getUnitDuration); 940 _classPrivateMethodInitSpec(this, _getUnitPropertyByFormat); 941 _classPrivateMethodInitSpec(this, _getUnitPropertyModByFormat); 942 _classPrivateMethodInitSpec(this, _formatUnit); 943 _classPrivateMethodInitSpec(this, _getMaxUnit); 944 _classPrivateMethodInitSpec(this, _getSeparator); 945 this.milliseconds = Math.abs(milliseconds); 946 } 947 babelHelpers.createClass(DurationFormat, [{ 948 key: "format", 949 /** 950 * @example new DurationFormat(5070000).format() // 1 hour, 24 minutes, 30 seconds 951 * @example new DurationFormat(5070000).format({ style: 'short' }) // 1 h 24 m 30 s 952 * @example new DurationFormat(5070000).format({ format: 'd H i' }) // 1 hour, 24 minutes 953 * @example new DurationFormat(5070000).format({ format: 'i s' }) // 84 minutes, 30 seconds 954 */ 955 value: function format(formatOptions = defaultOptions) { 956 const options = { 957 ...defaultOptions, 958 ...formatOptions 959 }; 960 const orderedUnits = main_core.Loc.getMessage('FD_UNIT_ORDER').split(' '); 961 const separator = _classPrivateMethodGet(this, _getSeparator, _getSeparator2).
961call(this, options.style); 962 const formatUnits = new Set(options.format.split(' ')); 963 const maxUnit = _classPrivateMethodGet(this, _getMaxUnit, _getMaxUnit2).call(this, options.format); 964 return orderedUnits.filter(unit => formatUnits.has(unit)).map(unit => _classPrivateMethodGet(this, _formatUnit, _formatUnit2).call(this, unit, unit !== maxUnit, options.style)).filter(unit => unit !== '').join(separator); 965 } 966 /** 967 * @example new DurationFormat(5070000).formatClosest() // 1 hour 968 * @example new DurationFormat(5070000).formatClosest({ format: 'i s' }) // 84 minutes 969 */ 970 }, { 971 key: "formatClosest", 972 value: function formatClosest(formatOptions = defaultOptions) { 973 const options = { 974 ...defaultOptions, 975 ...formatOptions 976 }; 977 const maxUnit = _classPrivateMethodGet(this, _getMaxUnit, _getMaxUnit2).call(this, options.format); 978 return _classPrivateMethodGet(this, _formatUnit, _formatUnit2).call(this, maxUnit, false, options.style); 979 } 980 }, { 981 key: "seconds", 982 get: function () { 983 return Math.floor(this.milliseconds / DurationFormat.getUnitDurations().s); 984 } 985 }, { 986 key: "minutes", 987 get: function () { 988 return Math.floor(this.milliseconds / DurationFormat.getUnitDurations().i); 989 } 990 }, { 991 key: "hours", 992 get: function () { 993 return Math.floor(this.milliseconds / DurationFormat.getUnitDurations().H); 994 } 995 }, { 996 key: "days", 997 get: function () { 998 return Math.floor(this.milliseconds / DurationFormat.getUnitDurations().d); 999 } 1000 /** 1001 * Considering month is 31 days 1002 */ 1003 }, { 1004 key: "months", 1005 get: function () { 1006 return Math.floor(this.milliseconds / DurationFormat.getUnitDurations().m); 1007 } 1008 /** 1009 * Considering year is 365 days 1010 */ 1011 }, { 1012 key: "years", 1013 get: function () { 1014 return Math.floor(this.milliseconds / DurationFormat.getUnitDurations().Y); 1015 } 1016 }], [{ 1017 key: "createFromSeconds", 1018 value: function createFromSeconds(seconds) { 1019 return new DurationFormat(seconds * DurationFormat.getUnitDurations().s); 1020 } 1021 }, { 1022 key: "createFromMinutes", 1023 value: function createFromMinutes(minutes) { 1024 return new DurationFormat(minutes * DurationFormat.getUnitDurations().i); 1025 } 1026 }, { 1027 key: "getUnitDurations", 1028 value: function getUnitDurations() { 1029 return { 1030 s: 1000, 1031 i: 60000, 1032 H: 3600000, 1033 d: 86400000, 1034 m: 2678400000, 1035 Y: 31536000000 1036 }; 1037 } 1038 }]); 1039 return DurationFormat; 1040 }(); 1041 function _getSeparator2(style) { 1042 if (style === 'short') { 1043 return main_core.Loc.getMessage('FD_SEPARATOR_SHORT').replaceAll(' ', ' '); 1044 } 1045 return main_core.Loc.getMessage('FD_SEPARATOR').replaceAll(' ', ' '); 1046 } 1047 function _getMaxUnit2(format) { 1048 const formatUnits = new Set(format.split(' ')); 1049 const units = Object.entries(DurationFormat.getUnitDurations()).filter(([unit]) => formatUnits.has(unit)); 1050 return units.reduce((closestDuration, unitDuration) => { 1051 const whole = Math.floor(this.milliseconds / unitDuration[1]) >= 1; 1052 const max = unitDuration[1] > closestDuration[1]; 1053 return whole && max ? unitDuration : closestDuration; 1054 }, units[0])[0]; 1055 } 1056 function _formatUnit2(unitStr, mod, style) { 1057 const value = mod ? _classPrivateMethodGet(this, _getUnitPropertyModByFormat, _getUnitPropertyModByFormat2).call(this, unitStr) : _classPrivateMethodGet(this, _getUnitPropertyByFormat, _getUnitPropertyByFormat2).call(this, unitStr); 1058 if (mod && value === 0) { 1059 return ''; 1060 } 1061 const now = Date.now() / 1000; 1062 const unitDuration = value * _classPrivateMethodGet(this, _getUnitDuration, _getUnitDuration2).call(this, unitStr) / 1000; 1063 const format = style === 'short' ? `${unitStr}short` : `${unitStr}diff`; 1064 return DateTimeFormat.format(format, now - unitDuration, now); 1065 } 1066 function _getUnitPropertyModByFormat2(unitStr) { 1067 const propsMod = { 1068 s: this.seconds % 60, 1069 i: this.minutes % 60, 1070 H: this.hours % 24, 1071 d: this.days % 31, 1072 m: this.months % 12, 1073 Y: this.years 1074 }; 1075 return propsMod[unitStr]; 1076 } 1077 function _getUnitPropertyByFormat2(unitStr) { 1078 const props = { 1079 s: this.seconds, 1080 i: this.minutes, 1081 H: this.hours, 1082 d: this.days, 1083 m: this.months, 1084 Y: this.years 1085 }; 1086 return props[unitStr]; 1087 } 1088 function _getUnitDuration2(unitStr) { 1089 return DurationFormat.getUnitDurations()[unitStr]; 1090 } 1091 1092 const cache = new main_core.Cache.MemoryCache(); 1093 /** 1094 * @memberOf BX.Main.Timezone 1095 * 1096 * WARNING! Don't use this class or any classes from Timezone namespace on sites without Bitrix Framework. 1097 * It is not designed to handle this case and will definitely break. 1098 */ 1099 const Offset = { 1100 get SERVER_TO_UTC() { 1101 return cache.remember('SERVER_TO_UTC', () => { 1102 return main_core.Text.toInteger(main_core.Loc.getMessage('SERVER_TZ_OFFSET')); 1103 }); 1104 }, 1105 get USER_TO_SERVER() { 1106 return cache.remember('USER_TO_SERVER', () => { 1107 return main_core.Text.toInteger(main_core.Loc.getMessage('USER_TZ_OFFSET')); 1108 }); 1109 }, 1110 // Date returns timezone offset in minutes by default, change it to seconds 1111 // Also offset is negative in UTC+ timezones and positive in UTC- timezones. 1112 // By convention Bitrix uses the opposite approach, so change offset sign. 1113 get BROWSER_TO_UTC() { 1114 return cache.remember('BROWSER_TO_UTC', () => { 1115 const offset = main_core.Text.toInteger(new Date().getTimezoneOffset() * 60); 1116 return -offset; 1117 }); 1118 } 1119 }; 1120 Object.freeze(Offset); 1121 1122 function normalizeTimeValue(timeValue) { 1123 if (main_core.Type.isDate(timeValue)) { 1124 return getTimestampFromDate(timeValue); 1125 } 1126 return main_core.Text.toInteger(timeValue); 1127 } 1128 function createDateFromTimestamp(timestampInSeconds) { 1129 return new Date(timestampInSeconds * 1000); 1130 } 1131 function getTimestampFromDate(date) { 1132 return Math.floor(date.getTime() / 1000); 1133 } 1134 1135 let offset = Offset; 1136 let now = null; 1137 function getOffset() { 1138 return offset; 1139 } 1140 function getNowTimestamp() { 1141 var _now; 1142 return (_now = now) !== null && _now !== void 0 ? _now : getTimestampFromDate(new Date()); 1143 } 1144 1145 /** 1146 * @memberOf BX.Main.Timezone 1147 * 1148 * WARNING! Don't use this class or any classes from Timezone namespace on sites without Bitrix Framework. 1149 * It is not designed to handle this case and will definitely break. 1150 */ 1151 let BrowserTime = /*#__PURE__*/function () { 1152 function BrowserTime() { 1153 babelHelpers.classCallCheck(this, BrowserTime); 1154 } 1155 babelHelpers.createClass(BrowserTime, null, [{ 1156 key: "getDate", 1157 /** 1158 * Returns a Date object with time and date that represent a specific moment in Browser (device) timezone. 1159 * 1160 * @param utcTimestamp - normal utc timestamp in seconds. 'now' by default 1161 * @returns {Date} 1162 */ 1163 value: function getDate(utcTimestamp = null) { 1164 const timestamp = main_core.Type.isNumber(utcTimestamp) ? utcTimestamp : this.getTimestamp(); 1165 return createDateFromTimestamp(timestamp); 1166 } 1167 /** 1168 * Transforms a moment in Browser (device) timezone to a moment in User timezone. 1169 * 1170 * ATTENTION! Date.getTime() and Date.getTimezoneOffset() will return inaccurate data. Since a native Date object 1171 * doesn't support timezone other that device timezone, we have to manually change timestamp to shift time value in 1172 * a Date object. 1173 * 1174 * @param browserTime - a moment in Browser (device) timezone. Either a Date object (recommended way). Or timestamp
1175 * in seconds in Browser timezone (see this.getTimestamp for details). 1176 * @returns {Date} 1177 */ 1178 }, { 1179 key: "toUserDate", 1180 value: function toUserDate(browserTime) { 1181 return createDateFromTimestamp(this.toUser(browserTime)); 1182 } 1183 /** 1184 * Transforms a moment in Browser (device) timezone to a moment in Server timezone. 1185 * 1186 * ATTENTION! Date.getTime() and Date.getTimezoneOffset() will return inaccurate data. Since a native Date object 1187 * doesn't support timezone other that device timezone, we have to manually change timestamp to shift time value in 1188 * a Date object. 1189 * 1190 * @param browserTime - a moment in Browser (device) timezone. Either a Date object (recommended way). Or timestamp 1191 * in seconds in Browser timezone (see this.getTimestamp for details). 1192 * @returns {Date} 1193 */ 1194 }, { 1195 key: "toServerDate", 1196 value: function toServerDate(browserTime) { 1197 return createDateFromTimestamp(this.toServer(browserTime)); 1198 } 1199 /** 1200 * Transforms a moment in Browser (device) timezone to a timestamp in User timezone. 1201 * It's recommended to use this.toUserDate for more clear code. 1202 * 1203 * @param browserTime - a moment in Browser timezone. Either a Date object (recommended way). Or timestamp in seconds 1204 * in Browser timezone (see this.getTimestamp for details). 1205 * @returns {number} - timestamp that when passed to 'new Date' will create an object with absolute time matching 1206 * the time in User timezone 1207 */ 1208 }, { 1209 key: "toUser", 1210 value: function toUser(browserTime) { 1211 return this.toServer(browserTime) + getOffset().USER_TO_SERVER; 1212 } 1213 /** 1214 * Transforms a moment in Browser (device) timezone to a timestamp in Server timezone. 1215 * It's recommended to use this.toServerDate for more clear code. 1216 * 1217 * @param browserTime - a moment in Browser timezone. Either a Date object (recommended way). Or timestamp in seconds 1218 * in Browser timezone (see this.getTimestamp for details). 1219 * @returns {number} - timestamp that when passed to 'new Date' will create an object with absolute time matching 1220 * the time in Server timezone 1221 */ 1222 }, { 1223 key: "toServer", 1224 value: function toServer(browserTime) { 1225 return normalizeTimeValue(browserTime) - getOffset().BROWSER_TO_UTC + getOffset().SERVER_TO_UTC; 1226 } 1227 /** 1228 * Returns 'now' timestamp in Browser (device) timezone - when it's passed to a 'new Date', it will create an object 1229 * with absolute time matching the time as if it was in Browser (device) timezone. 1230 * 1231 * @returns {number} 1232 */ 1233 }, { 1234 key: "getTimestamp", 1235 value: function getTimestamp() { 1236 // since 'Date' class in JS is hardcoded to use device timezone, 'browser timestamp' is just normal UTC timestamp :) 1237 1238 return getNowTimestamp(); 1239 } 1240 }]); 1241 return BrowserTime; 1242 }(); 1243 1244 /** 1245 * @memberOf BX.Main.Timezone 1246 * 1247 * WARNING! Don't use this class or any classes from Timezone namespace on sites without Bitrix Framework. 1248 * It is not designed to handle this case and will definitely break. 1249 */ 1250 let ServerTime = /*#__PURE__*/function () { 1251 function ServerTime() { 1252 babelHelpers.classCallCheck(this, ServerTime); 1253 } 1254 babelHelpers.createClass(ServerTime, null, [{ 1255 key: "getDate", 1256 /** 1257 * Returns a Date object with time and date that represent a specific moment in Server timezone. 1258 * 1259 * ATTENTION! Date.getTime() and Date.getTimezoneOffset() will return inaccurate data. Since a native Date object 1260 * doesn't support timezone other that device timezone, we have to manually change timestamp to shift time value in 1261 * a Date object. 1262 * 1263 * @param utcTimestamp - normal utc timestamp in seconds. 'now' by default 1264 * @returns {Date} 1265 */ 1266 value: function getDate(utcTimestamp = null) { 1267 if (main_core.Type.isNumber(utcTimestamp)) { 1268 const browserToServerOffset = getOffset().SERVER_TO_UTC - getOffset().BROWSER_TO_UTC; 1269 return createDateFromTimestamp(utcTimestamp + browserToServerOffset); 1270 } 1271 return BrowserTime.toServerDate(BrowserTime.getDate()); 1272 } 1273 /** 1274 * Transforms a moment in Server timezone to a moment in User timezone. 1275 * 1276 * ATTENTION! Date.getTime() and Date.getTimezoneOffset() will return inaccurate data. Since a native Date object 1277 * doesn't support timezone other that device timezone, we have to manually change timestamp to shift time value in 1278 * a Date object. 1279 * 1280 * @param serverTime - a moment in Server timezone. Either a Date object (recommended way). Or timestamp in seconds
1280in 1281 * Server timezone (see this.getTimestamp for details). 1282 * @returns {Date} 1283 */ 1284 }, { 1285 key: "toUserDate", 1286 value: function toUserDate(serverTime) { 1287 return createDateFromTimestamp(this.toUser(serverTime)); 1288 } 1289 /** 1290 * Transforms a moment in Server timezone to a moment in Browser (device) timezone. 1291 * 1292 * @param serverTime - a moment in Server timezone. Either a Date object (recommended way). Or timestamp in seconds in 1293 * Server timezone (see this.getTimestamp for details). 1294 * @returns {Date} 1295 */ 1296 }, { 1297 key: "toBrowserDate", 1298 value: function toBrowserDate(serverTime) { 1299 return createDateFromTimestamp(this.toBrowser(serverTime)); 1300 } 1301 /** 1302 * Transforms a moment in Server timezone to a timestamp in User timezone. 1303 * It's recommended to use this.toServerDate for more clear code. 1304 * 1305 * @param serverTime - a moment in Server timezone. Either a Date object (recommended way). Or timestamp in seconds in 1306 * Server timezone (see this.getTimestamp for details). 1307 * @returns {number} - timestamp that when passed to 'new Date' will create an object with absolute time matching 1308 * the time in User timezone 1309 */ 1310 }, { 1311 key: "toUser", 1312 value: function toUser(serverTime) { 1313 return normalizeTimeValue(serverTime) + getOffset().USER_TO_SERVER; 1314 } 1315 /** 1316 * Transforms a moment in Server timezone to a timestamp in Browser (device) timezone. 1317 * It's recommended to use this.toBrowserDate for more clear code. 1318 * 1319 * @param serverTime - a moment in Server timezone. Either a Date object (recommended way). Or timestamp in seconds in 1320 * Server timezone (see this.getTimestamp for details). 1321 * @returns {number} - timestamp that when passed to 'new Date' will create an object with absolute time matching 1322 * the time in Browser (device) timezone 1323 */ 1324 }, { 1325 key: "toBrowser", 1326 value: function toBrowser(serverTime) { 1327 return normalizeTimeValue(serverTime) + getOffset().BROWSER_TO_UTC - getOffset().SERVER_TO_UTC; 1328 } 1329 /** 1330 * Returns 'now' timestamp in Server timezone - when it's passed to a 'new Date', it will create an object with 1331 * absolute time matching the time as if it was in Server timezone. 1332 * 1333 * @returns {number} 1334 */ 1335 }, { 1336 key: "getTimestamp", 1337 value: function getTimestamp() { 1338 return BrowserTime.toServer(BrowserTime.getTimestamp()); 1339 } 1340 }]); 1341 return ServerTime; 1342 }(); 1343 1344 function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { _classCheckPrivateStaticAccess(receiver, classConstructor); _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); return _classApplyDescriptorGet(receiver, descriptor); } 1345 function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { if (descriptor === undefined) { throw new TypeError("attempted to " + action + " private static field before its declaration"); } } 1346 function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } } 1347 function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; } 1348 1349 /** 1350 * @memberOf BX.Main.Timezone 1351 * 1352 * WARNING! Don't use this class or any classes from Timezone namespace on sites without Bitrix Framework. 1353 * It is not designed to handle this case and will definitely break. 1354 * 1355 * ATTENTION! In Bitrix user timezone !== browser timezone. Users can change their timezone from their profile settings 1356 * and the timezone will be different from browser timezone. 1357 */ 1358 let UserTime = /*#__PURE__*/function () { 1359 function UserTime() { 1360 babelHelpers.classCallCheck(this, UserTime); 1361 } 1362 babelHelpers.createClass(UserTime, null, [{ 1363 key: "getDate", 1364 /** 1365 * Returns a Date object with time and date that represent a specific moment in User timezone. 1366 * 1367 * ATTENTION! Date.getTime() and Date.getTimezoneOffset() will return inaccurate data. Since a native Date object 1368 * doesn't support timezone other that device timezone, we have to manually change timestamp to shift time value in 1369 * a Date object. 1370 * 1371 * @param utcTimestamp - normal utc timestamp in seconds. 'now' by default 1372 * @returns {Date} 1373 */ 1374 value: function getDate(utcTimestamp = null) { 1375 if (main_core.Type.isNumber(utcTimestamp)) { 1376 return createDateFromTimestamp(utcTimestamp + _classStaticPrivateFieldSpecGet(this, UserTime, _userToBrowserOffset)); 1377 } 1378 return createDateFromTimestamp(this.getTimestamp()); 1379 } 1380 }, { 1381 key: "toBrowserDate", 1382 /** 1383 * Transforms a moment in User timezone to a moment in Browser (device) timezone. 1384 * 1385 * @param userTime - a moment in User timezone. Either a Date object (recommended way). Or timestamp in seconds
1385in 1386 * User timezone (see this.getTimestamp for details). 1387 * @returns {Date} 1388 */ 1389 value: function toBrowserDate(userTime) { 1390 return createDateFromTimestamp(this.toBrowser(userTime)); 1391 } 1392 /** 1393 * Transforms a moment in User timezone to a moment in Server timezone. 1394 * 1395 * ATTENTION! Date.getTime() and Date.getTimezoneOffset() will return inaccurate data. Since a native Date object 1396 * doesn't support timezone other that device timezone, we have to manually change timestamp to shift time value in 1397 * a Date object. 1398 * 1399 * @param userTime - a moment in User timezone. Either a Date object (recommended way). Or timestamp in seconds in 1400 * User timezone (see this.getTimestamp for details). 1401 * @returns {Date} 1402 */ 1403 }, { 1404 key: "toServerDate", 1405 value: function toServerDate(userTime) { 1406 return createDateFromTimestamp(this.toServer(userTime)); 1407 } 1408 }, { 1409 key: "toUTCTimestamp", 1410 value: function toUTCTimestamp(userTime) { 1411 return normalizeTimeValue(userTime) - _classStaticPrivateFieldSpecGet(this, UserTime, _userToBrowserOffset); 1412 } 1413 /** 1414 * Transforms a moment in User timezone to a timestamp in Browser timezone. 1415 * It's recommended to use this.toBrowserDate for more clear code. 1416 * 1417 * @param userTime - a moment in User timezone. Either a Date object (recommended way). Or timestamp in seconds in 1418 * User timezone (see this.getTimestamp for details). 1419 * @returns {number} - timestamp that when passed to 'new Date' will create an object with absolute time matching 1420 * the time in Browser (device) timezone 1421 */ 1422 }, { 1423 key: "toBrowser", 1424 value: function toBrowser(userTime) { 1425 return normalizeTimeValue(userTime) + getOffset().BROWSER_TO_UTC - getOffset().SERVER_TO_UTC - getOffset().USER_TO_SERVER; 1426 } 1427 /** 1428 * Transforms a moment in User timezone to a timestamp in Server timezone. 1429 * It's recommended to use this.toServerDate for more clear code. 1430 * 1431 * @param userTime - a moment in User timezone. Either a Date object (recommended way). Or timestamp in seconds in 1432 * User timezone (see this.getTimestamp for details). 1433 * @returns {number} - timestamp that when passed to 'new Date' will create an object with absolute time matching 1434 * the time in Server timezone 1435 */ 1436 }, { 1437 key: "toServer", 1438 value: function toServer(userTime) { 1439 return normalizeTimeValue(userTime) - getOffset().USER_TO_SERVER; 1440 } 1441 /** 1442 * Returns 'now' timestamp in User timezone - when it's passed to a 'new Date', it will create an object with absolute 1443 * time matching the time as if it was in User timezone. 1444 * 1445 * @returns {number} 1446 */ 1447 }, { 1448 key: "getTimestamp", 1449 value: function getTimestamp() { 1450 return BrowserTime.toUser(BrowserTime.getTimestamp()); 1451 } 1452 }]); 1453 return UserTime; 1454 }(); 1455 function _get_userToBrowserOffset() { 1456 const userToUTCOffset = getOffset().SERVER_TO_UTC + getOffset().USER_TO_SERVER; 1457 return userToUTCOffset - getOffset().BROWSER_TO_UTC; 1458 } 1459 var _userToBrowserOffset = { 1460 get: _get_userToBrowserOffset, 1461 set: void 0 1462 }; 1463 1464 const Timezone = Object.freeze({ 1465 BrowserTime, 1466 Offset, 1467 ServerTime, 1468 UserTime 1469 }); 1470 1471 exports.Timezone = Timezone; 1472 exports.Date = DateTimeFormat; 1473 exports.DateTimeFormat = DateTimeFormat; 1474 exports.DurationFormat = DurationFormat; 1475 1476}((this.BX.Main = this.BX.Main || {}),BX)); 1477//# 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.