| 1 |
'use strict'; |
| 2 |
|
| 3 |
const config = require( './config.json' ); |
| 4 |
|
| 5 |
const instanceCache = { |
| 6 |
user: null, |
| 7 |
utc: null, |
| 8 |
site: null |
| 9 |
}; |
| 10 |
|
| 11 |
let supportsOffsetCache = null; |
| 12 |
|
| 13 |
|
| 14 |
const includes = ( arr, x, from ) => arr.indexOf( x, from ) !== -1; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
const priv = { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
supportsOffset() { |
| 27 |
if ( supportsOffsetCache === null ) { |
| 28 |
try { |
| 29 |
|
| 30 |
new Intl.DateTimeFormat( undefined, { timeZone: '+01:00' } ); |
| 31 |
supportsOffsetCache = true; |
| 32 |
} catch ( e ) { |
| 33 |
supportsOffsetCache = false; |
| 34 |
} |
| 35 |
} |
| 36 |
return supportsOffsetCache; |
| 37 |
}, |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
offsetToZone( offset ) { |
| 48 |
const hour = Math.floor( Math.abs( offset ) / 60 ); |
| 49 |
const minute = Math.abs( offset ) % 60; |
| 50 |
if ( priv.supportsOffset() ) { |
| 51 |
const pad = ( x ) => `${ x < 10 ? '0' : '' }${ x }`; |
| 52 |
return `${ offset < 0 ? '-' : '+' }${ pad( hour ) }:${ pad( minute ) }`; |
| 53 |
} |
| 54 |
if ( minute !== 0 ) { |
| 55 |
|
| 56 |
return undefined; |
| 57 |
} |
| 58 |
return `Etc/GMT${ offset < 0 ? '+' : '-' }${ hour }`; |
| 59 |
}, |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
isKnownZoneName( zoneName ) { |
| 69 |
|
| 70 |
|
| 71 |
return Intl.supportedValuesOf && includes( Intl.supportedValuesOf( 'timeZone' ), zoneName ); |
| 72 |
}, |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
normalizeZone( optionValue, localZone, localOffset ) { |
| 85 |
if ( optionValue ) { |
| 86 |
const parts = optionValue.split( '|', 3 ); |
| 87 |
if ( parts[ 0 ] === 'ZoneInfo' ) { |
| 88 |
if ( priv.isKnownZoneName( parts[ 2 ] ) ) { |
| 89 |
return parts[ 2 ]; |
| 90 |
} else { |
| 91 |
return priv.offsetToZone( parseInt( parts[ 1 ], 10 ) ); |
| 92 |
} |
| 93 |
} else if ( parts[ 0 ] === 'Offset' ) { |
| 94 |
return priv.offsetToZone( parts[ 1 ] ); |
| 95 |
} |
| 96 |
} |
| 97 |
return priv.isKnownZoneName( localZone ) ? localZone : priv.offsetToZone( localOffset ); |
| 98 |
}, |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
getUserZone() { |
| 106 |
return priv.normalizeZone( |
| 107 |
mw.user.options.get( 'timecorrection' ), |
| 108 |
config.localZone, |
| 109 |
config.localOffset |
| 110 |
); |
| 111 |
}, |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
getSiteZone() { |
| 119 |
return priv.normalizeZone( null, config.localZone, config.localOffset ); |
| 120 |
}, |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
getUserDateStyle() { |
| 128 |
const def = config.defaultStyle; |
| 129 |
const style = mw.user.options.get( 'date', def ); |
| 130 |
return ( !style || style === 'default' ) ? def : style; |
| 131 |
}, |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
partsToInfo( parts ) { |
| 141 |
const info = {}; |
| 142 |
for ( const part of parts ) { |
| 143 |
info[ part.type ] = part.value; |
| 144 |
} |
| 145 |
return info; |
| 146 |
}, |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
formatPattern( info, pattern ) { |
| 156 |
return pattern.replace( |
| 157 |
/\{(\w+)}/g, |
| 158 |
( _, name ) => name in info ? info[ name ] : '?' |
| 159 |
); |
| 160 |
}, |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
longOffsetToIsoOffset( offset ) { |
| 169 |
const m = /(GMT|UTC)?([+-][0-9]{2}:[0-9]{2})/.exec( offset ); |
| 170 |
return m ? m[ 2 ] : '+00:00'; |
| 171 |
} |
| 172 |
}; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
class DateFormatter { |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
static forUser() { |
| 212 |
if ( !instanceCache.user ) { |
| 213 |
instanceCache.user = new DateFormatter( |
| 214 |
config.locales, |
| 215 |
config.formats, |
| 216 |
priv.getUserZone(), |
| 217 |
priv.getUserDateStyle() |
| 218 |
); |
| 219 |
} |
| 220 |
return instanceCache.user; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
static forUtc() { |
| 230 |
if ( !instanceCache.utc ) { |
| 231 |
instanceCache.utc = new DateFormatter( |
| 232 |
config.locales, |
| 233 |
config.formats, |
| 234 |
'Etc/GMT', |
| 235 |
priv.getUserDateStyle() |
| 236 |
); |
| 237 |
} |
| 238 |
return instanceCache.utc; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
static forSiteZone() { |
| 248 |
if ( !instanceCache.site ) { |
| 249 |
instanceCache.site = new DateFormatter( |
| 250 |
config.locales, |
| 251 |
config.formats, |
| 252 |
priv.getSiteZone(), |
| 253 |
priv.getUserDateStyle() |
| 254 |
); |
| 255 |
} |
| 256 |
return instanceCache.site; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
static formatTimeAndDate( date ) { |
| 266 |
return DateFormatter.forUser().formatTimeAndDate( date ); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
static formatDate( date ) { |
| 276 |
return DateFormatter.forUser().formatDate( date ); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
static formatTime( date ) { |
| 286 |
return DateFormatter.forUser().formatTime( date ); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
static formatPrettyDate( date ) { |
| 296 |
return DateFormatter.forUser().formatPrettyDate( date ); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
static formatIso( date ) { |
| 309 |
return DateFormatter.forUser().formatIso( date ); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
static formatForDateTimeInput( date ) { |
| 321 |
return DateFormatter.forUser().formatForDateTimeInput( date ); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
static formatTimeAndDateRange( date1, date2 ) { |
| 337 |
return DateFormatter.forUser().formatTimeAndDateRange( date1, date2 ); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
static formatTimeRange( date1, date2 ) { |
| 350 |
return DateFormatter.forUser().formatTimeRange( date1, date2 ); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
static formatDateRange( date1, date2 ) { |
| 363 |
return DateFormatter.forUser().formatDateRange( date1, date2 ); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
static getShortZoneName( date ) { |
| 373 |
return DateFormatter.forUser().getShortZoneName( date ); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
static getZoneOffsetMinutes( date ) { |
| 384 |
return DateFormatter.forUser().getZoneOffsetMinutes( date ); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
static clearInstanceCache() { |
| 394 |
instanceCache.user = null; |
| 395 |
instanceCache.utc = null; |
| 396 |
instanceCache.site = null; |
| 397 |
supportsOffsetCache = null; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
constructor( locales, formats, zone, style ) { |
| 417 |
this.locales = locales; |
| 418 |
this.formats = Object.assign( {}, formats ); |
| 419 |
const machineTemplate = { |
| 420 |
locale: 'en', |
| 421 |
options: { |
| 422 |
year: 'numeric', month: '2-digit', day: '2-digit', |
| 423 |
hour: '2-digit', minute: '2-digit', second: '2-digit', |
| 424 |
hour12: false, timeZoneName: 'longOffset' |
| 425 |
} |
| 426 |
}; |
| 427 |
this.formats.datetime = Object.assign( |
| 428 |
{ pattern: '{year}-{month}-{day}T{hour}:{minute}' }, |
| 429 |
machineTemplate |
| 430 |
); |
| 431 |
this.formats.iso = Object.assign( |
| 432 |
{ pattern: '{year}-{month}-{day}T{hour}:{minute}:{second}{mwZoneOffset}' }, |
| 433 |
machineTemplate |
| 434 |
); |
| 435 |
this.formats.mw = Object.assign( |
| 436 |
{ pattern: '{year}{month}{day}{hour}{minute}{second}' }, |
| 437 |
machineTemplate |
| 438 |
); |
| 439 |
this.formats.monthNumber = { locale: 'en', options: { month: 'numeric' } }; |
| 440 |
this.zone = zone; |
| 441 |
this.intlFormatters = {}; |
| 442 |
this.style = style; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
formatTimeAndDate( date ) { |
| 452 |
return this.formatInternal( this.style, 'both', date ); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
formatDate( date ) { |
| 462 |
return this.formatInternal( this.style, 'date', date ); |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
formatTime( date ) { |
| 472 |
return this.formatInternal( this.style, 'time', date ); |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
formatPrettyDate( date ) { |
| 482 |
return this.formatInternal( this.style, 'pretty', date ); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
formatMw( date ) { |
| 494 |
return this.formatInternal( null, 'mw', date ); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
formatIso( date ) { |
| 505 |
return this.formatInternal( null, 'iso', date ); |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
formatForDateTimeInput( date ) { |
| 517 |
return this.formatInternal( null, 'datetime', date ); |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
formatTimeAndDateRange( date1, date2 ) { |
| 533 |
return this.formatRangeInternal( this.style, 'both', date1, date2 ); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
formatTimeRange( date1, date2 ) { |
| 546 |
return this.formatRangeInternal( this.style, 'time', date1, date2 ); |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
formatDateRange( date1, date2 ) { |
| 559 |
return this.formatRangeInternal( this.style, 'date', date1, date2 ); |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
getShortZoneName( date ) { |
| 569 |
return this.getLocalInfoInternal( 'timeZoneName', 'short', date ); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
getZoneOffsetMinutes( date ) { |
| 580 |
const [ h, m ] = |
| 581 |
priv.longOffsetToIsoOffset( |
| 582 |
this.getLocalInfoInternal( 'timeZoneName', 'longOffset', date ) |
| 583 |
).split( ':' ); |
| 584 |
return h * 60 + Math.sign( +h ) * m; |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
formatInternal( style, type, date ) { |
| 602 |
const formatName = style ? `${ style } ${ type }` : type; |
| 603 |
const formatter = this.getIntlFormatInternal( formatName ); |
| 604 |
const pattern = this.formats[ formatName ].pattern; |
| 605 |
if ( pattern ) { |
| 606 |
const info = priv.partsToInfo( formatter.formatToParts( date ) ); |
| 607 |
if ( includes( pattern, '{mwMonth' ) ) { |
| 608 |
const i = +this.getIntlFormatInternal( 'monthNumber' ).format( date ); |
| 609 |
[ info.mwMonth, info.mwMonthGen, info.mwMonthAbbrev ] = config.months[ i ] || []; |
| 610 |
} |
| 611 |
if ( includes( pattern, '{mwZoneOffset}' ) ) { |
| 612 |
info.mwZoneOffset = priv.longOffsetToIsoOffset( info.timeZoneName ); |
| 613 |
} |
| 614 |
return priv.formatPattern( info, pattern ); |
| 615 |
} else { |
| 616 |
return formatter.format( date ); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
formatRangeInternal( style, type, date1, date2 ) { |
| 633 |
const formatName = style ? `${ style } ${ type }` : type; |
| 634 |
const formatter = this.getIntlFormatInternal( formatName ); |
| 635 |
const pattern = this.formats[ formatName ].rangePattern; |
| 636 |
if ( pattern ) { |
| 637 |
return priv.formatPattern( |
| 638 |
priv.partsToInfo( formatter.formatRangeToParts( date1, date2 ) ), |
| 639 |
pattern |
| 640 |
); |
| 641 |
} else { |
| 642 |
return formatter.formatRange( date1, date2 ); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
getIntlFormatInternal( formatName ) { |
| 657 |
if ( !( formatName in this.intlFormatters ) ) { |
| 658 |
if ( !( formatName in this.formats ) ) { |
| 659 |
throw new Error( `Unknown date format "${ formatName }"` ); |
| 660 |
} |
| 661 |
const format = this.formats[ formatName ]; |
| 662 |
const locale = format.locale || this.locales; |
| 663 |
const options = Object.assign( {}, format.options ); |
| 664 |
options.timeZone = this.zone; |
| 665 |
this.intlFormatters[ formatName ] = new Intl.DateTimeFormat( locale, options ); |
| 666 |
} |
| 667 |
return this.intlFormatters[ formatName ]; |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
getLocalInfoInternal( fieldName, configValue, date ) { |
| 683 |
const key = `info ${ fieldName }.${ configValue }`; |
| 684 |
if ( !this.formats[ key ] ) { |
| 685 |
this.formats[ key ] = { options: { [ fieldName ]: configValue } }; |
| 686 |
} |
| 687 |
const formatter = this.getIntlFormatInternal( key ); |
| 688 |
const info = priv.partsToInfo( formatter.formatToParts( date ) ); |
| 689 |
return info[ fieldName ] || ''; |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
if ( window.QUnit ) { |
| 694 |
DateFormatter.priv = priv; |
| 695 |
DateFormatter.config = config; |
| 696 |
} |
| 697 |
|
| 698 |
module.exports = DateFormatter; |