4 * Provides various methods needed for formatting dates and times.
8 * @mixins OO.EventEmitter
11 * @param {Object} [config] Configuration options
12 * @cfg {string} [format='@default'] May be a key from the {@link #static-formats static formats},
13 * or a format specification as defined by {@link #method-parseFieldSpec parseFieldSpec}
14 * and {@link #method-getFieldForTag getFieldForTag}.
15 * @cfg {boolean} [local=false] Whether dates are local time or UTC
16 * @cfg {string[]} [fullZones] Time zone indicators. Array of 2 strings, for
18 * @cfg {string[]} [shortZones] Abbreviated time zone indicators. Array of 2
19 * strings, for UTC and local time.
20 * @cfg {Date} [defaultDate] Default date, for filling unspecified components.
21 * Defaults to the current date and time (with 0 milliseconds).
23 mw
.widgets
.datetime
.DateTimeFormatter
= function MwWidgetsDatetimeDateTimeFormatter( config
) {
24 this.constructor.static.setupDefaults();
29 fullZones
: this.constructor.static.fullZones
,
30 shortZones
: this.constructor.static.shortZones
34 OO
.EventEmitter
.call( this );
37 if ( this.constructor.static.formats
[ config
.format
] ) {
38 this.format
= this.constructor.static.formats
[ config
.format
];
40 this.format
= config
.format
;
42 this.local
= !!config
.local
;
43 this.fullZones
= config
.fullZones
;
44 this.shortZones
= config
.shortZones
;
45 if ( config
.defaultDate
instanceof Date
) {
46 this.defaultDate
= config
.defaultDate
;
48 this.defaultDate
= new Date();
50 this.defaultDate
.setMilliseconds( 0 );
52 this.defaultDate
.setUTCMilliseconds( 0 );
59 OO
.initClass( mw
.widgets
.datetime
.DateTimeFormatter
);
60 OO
.mixinClass( mw
.widgets
.datetime
.DateTimeFormatter
, OO
.EventEmitter
);
65 * Default format specifications. See the {@link #format format} parameter.
71 mw
.widgets
.datetime
.DateTimeFormatter
.static.formats
= {};
74 * Default time zone indicators
78 * @property {string[]}
80 mw
.widgets
.datetime
.DateTimeFormatter
.static.fullZones
= null;
83 * Default abbreviated time zone indicators
87 * @property {string[]}
89 mw
.widgets
.datetime
.DateTimeFormatter
.static.shortZones
= null;
91 mw
.widgets
.datetime
.DateTimeFormatter
.static.setupDefaults = function () {
92 if ( !this.fullZones
) {
94 mw
.msg( 'timezone-utc' ),
95 mw
.msg( 'timezone-local' )
98 if ( !this.shortZones
) {
101 this.fullZones
[ 1 ].substr( 0, 1 ).toUpperCase()
103 if ( this.shortZones
[ 1 ] === 'Z' ) {
104 this.shortZones
[ 1 ] = 'L';
112 * A `local` event is emitted when the 'local' flag is changed.
120 * Whether dates are in local time or UTC
122 * @return {boolean} True if local time
124 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.getLocal = function () {
128 // eslint-disable-next-line valid-jsdoc
130 * Toggle whether dates are in local time or UTC
132 * @param {boolean} [flag] Set the flag instead of toggling it
136 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.toggleLocal = function ( flag
) {
137 if ( flag
=== undefined ) {
142 if ( this.local
!== flag
) {
144 this.emit( 'local', this.local
);
150 * Get the default date
154 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.getDefaultDate = function () {
155 return new Date( this.defaultDate
.getTime() );
159 * Fetch the field specification array for this object.
161 * See {@link #parseFieldSpec parseFieldSpec} for details on the return value structure.
165 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.getFieldSpec = function () {
166 return this.parseFieldSpec( this.format
);
170 * Parse a format string into a field specification
172 * The input is a string containing tags formatted as ${tag|param|param...}
173 * (for editable fields) and $!{tag|param|param...} (for non-editable fields).
174 * Most tags are defined by {@link #getFieldForTag getFieldForTag}, but a few
176 * - ${intercalary|X|text}: Text that is only displayed when the 'intercalary'
178 * - ${not-intercalary|X|text}: Text that is displayed unless the 'intercalary'
181 * Elements of the returned array are strings or objects. Strings are meant to
182 * be displayed as-is. Objects are as returned by {@link #getFieldForTag getFieldForTag}.
185 * @param {string} format
188 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.parseFieldSpec = function ( format
) {
189 var m
, last
, tag
, params
, spec
,
191 re
= /(.*?)(\$(!?)\{([^}]+)\})/g;
194 while ( ( m
= re
.exec( format
) ) !== null ) {
197 if ( m
[ 1 ] !== '' ) {
201 params
= m
[ 4 ].split( '|' );
202 tag
= params
.shift();
203 spec
= this.getFieldForTag( tag
, params
);
205 if ( m
[ 3 ] === '!' ) {
206 spec
.editable
= false;
213 if ( last
< format
.length
) {
214 ret
.push( format
.substr( last
) );
221 * Turn a tag into a field specification object
223 * Fields implemented here are:
224 * - ${intercalary|X|text}: Text that is only displayed when the 'intercalary'
226 * - ${not-intercalary|X|text}: Text that is displayed unless the 'intercalary'
228 * - ${zone|#}: Timezone offset, "+0000" format.
229 * - ${zone|:}: Timezone offset, "+00:00" format.
230 * - ${zone|short}: Timezone from 'shortZones' configuration setting.
231 * - ${zone|full}: Timezone from 'fullZones' configuration setting.
235 * @param {string} tag
236 * @param {string[]} params
237 * @return {Object|null} Field specification object, or null if the tag+params are unrecognized.
238 * @return {string|null} return.component Date component corresponding to this field, if any.
239 * @return {boolean} return.editable Whether this field is editable.
240 * @return {string} return.type What kind of field this is:
241 * - 'static': The field is a static string; component will be null.
242 * - 'number': The field is generally numeric.
243 * - 'string': The field is generally textual.
244 * - 'boolean': The field is a boolean.
245 * - 'toggleLocal': The field represents {@link #getLocal this.getLocal()}.
246 * Editing should directly call {@link #toggleLocal this.toggleLocal()}.
247 * @return {boolean} return.calendarComponent Whether this field is part of a calendar, e.g.
248 * part of the date instead of the time.
249 * @return {number} return.size Maximum number of characters in the field (when
250 * the 'intercalary' component is falsey). If 0, the field should be hidden entirely.
251 * @return {Object.<string,number>} return.intercalarySize Map from
252 * 'intercalary' component values to overridden sizes.
253 * @return {string} return.value For type='static', the string to display.
254 * @return {function(Mixed): string} return.formatValue A function to format a
255 * component value as a display string.
256 * @return {function(string): Mixed} return.parseValue A function to parse a
257 * display string into a component value. If parsing fails, returns undefined.
259 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.getFieldForTag = function ( tag
, params
) {
264 case 'not-intercalary':
265 if ( params
.length
< 2 || !params
[ 0 ] ) {
270 calendarComponent
: false,
273 value
: params
.slice( 1 ).join( '|' ),
277 if ( tag
=== 'intercalary' ) {
278 spec
.intercalarySize
[ params
[ 0 ] ] = spec
.value
.length
;
280 spec
.size
= spec
.value
.length
;
281 spec
.intercalarySize
[ params
[ 0 ] ] = 0;
286 switch ( params
[ 0 ] ) {
289 c
= params
[ 0 ] === '#' ? '' : ':';
292 calendarComponent
: false,
296 formatValue: function ( v
) {
299 o
= new Date().getTimezoneOffset();
300 r
= String( Math
.abs( o
) % 60 );
301 while ( r
.length
< 2 ) {
304 r
= String( Math
.floor( Math
.abs( o
) / 60 ) ) + c
+ r
;
305 while ( r
.length
< 4 + c
.length
) {
308 return ( o
<= 0 ? '+' : '−' ) + r
;
310 return '+00' + c
+ '00';
313 parseValue: function ( v
) {
315 v
= String( v
).trim();
316 if ( ( m
= /^([+-−])([0-9]{1,2}):?([0-9]{2})$/.test( v
) ) ) {
317 return ( m
[ 2 ] * 60 + m
[ 3 ] ) * ( m
[ 1 ] === '+' ? -1 : 1 );
328 calendarComponent
: false,
331 values
: params
[ 0 ] === 'short' ? this.shortZones
: this.fullZones
,
332 formatValue
: this.formatSpecValue
,
333 parseValue
: this.parseSpecValue
335 spec
.size
= Math
.max
.apply(
336 null, $.map( spec
.values
, function ( v
) { return v
.length
; } )
348 * Format a value for a field specification
350 * 'this' must be the field specification object. The intention is that you
351 * could just assign this function as the 'formatValue' for each field spec.
353 * Besides the publicly-documented fields, uses the following:
354 * - values: Enumerated values for the field
355 * - zeropad: Whether to pad the number with zeros.
361 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.formatSpecValue = function ( v
) {
362 if ( v
=== undefined || v
=== null ) {
366 if ( typeof v
=== 'boolean' || this.type
=== 'toggleLocal' ) {
371 return this.values
[ v
];
375 if ( this.zeropad
) {
376 while ( v
.length
< this.size
) {
384 * Parse a value for a field specification
386 * 'this' must be the field specification object. The intention is that you
387 * could just assign this function as the 'parseValue' for each field spec.
389 * Besides the publicly-documented fields, uses the following:
390 * - values: Enumerated values for the field
394 * @return {number|string|null}
396 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.parseSpecValue = function ( v
) {
403 if ( !this.values
) {
405 if ( this.type
=== 'boolean' || this.type
=== 'toggleLocal' ) {
406 return isNaN( v
) ? undefined : !!v
;
408 return isNaN( v
) ? undefined : v
;
415 re
= new RegExp( '^\\s*' + v
.replace( /([\\{}()|.?*+\-\^$\[\]])/g, '\\$1' ), 'i' );
416 for ( k
in this.values
) {
418 if ( !isNaN( k
) && re
.test( this.values
[ k
] ) ) {
419 if ( this.type
=== 'boolean' || this.type
=== 'toggleLocal' ) {
430 * Get components from a Date object
432 * Most specific components are defined by the subclass. "Global" components
434 * - intercalary: {string} Non-falsey values are used to indicate intercalary days.
435 * - zone: {number} Timezone offset in minutes.
438 * @param {Date|null} date
439 * @return {Object} Components
441 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.getComponentsFromDate = function ( date
) {
442 // Should be overridden by subclass
444 zone
: this.local
? date
.getTimezoneOffset() : 0
449 * Get a Date object from components
451 * @param {Object} components Date components
454 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.getDateFromComponents = function ( /* components */ ) {
455 // Should be overridden by subclass
462 * @param {Date|null} date To be adjusted
463 * @param {string} component To adjust
464 * @param {number} delta Adjustment amount
465 * @param {string} mode Adjustment mode:
466 * - 'overflow': "Jan 32" => "Feb 1", "Jan 33" => "Feb 2", "Feb 0" => "Jan 31", etc.
467 * - 'wrap': "Jan 32" => "Jan 1", "Jan 33" => "Jan 2", "Jan 0" => "Jan 31", etc.
468 * - 'clip': "Jan 32" => "Jan 31", "Feb 32" => "Feb 28" (or 29), "Feb 0" => "Feb 1", etc.
469 * @return {Date} Adjusted date
471 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.adjustComponent = function ( date
/* , component, delta, mode */ ) {
472 // Should be overridden by subclass
477 * Get the column headings (weekday abbreviations) for a calendar grid
479 * Null-valued columns are hidden if getCalendarData() returns no "day" object
480 * for all days in that column.
483 * @return {Array} string or null
485 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.getCalendarHeadings = function () {
486 // Should be overridden by subclass
491 * Test whether two dates are in the same calendar grid
494 * @param {Date} date1
495 * @param {Date} date2
498 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.sameCalendarGrid = function ( date1
, date2
) {
499 // Should be overridden by subclass
500 return date1
.getTime() === date2
.getTime();
504 * Test whether the date parts of two Dates are equal
506 * @param {Date} date1
507 * @param {Date} date2
510 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.datePartIsEqual = function ( date1
, date2
) {
513 date1
.getFullYear() === date2
.getFullYear() &&
514 date1
.getMonth() === date2
.getMonth() &&
515 date1
.getDate() === date2
.getDate()
519 date1
.getUTCFullYear() === date2
.getUTCFullYear() &&
520 date1
.getUTCMonth() === date2
.getUTCMonth() &&
521 date1
.getUTCDate() === date2
.getUTCDate()
527 * Test whether the time parts of two Dates are equal
529 * @param {Date} date1
530 * @param {Date} date2
533 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.timePartIsEqual = function ( date1
, date2
) {
536 date1
.getHours() === date2
.getHours() &&
537 date1
.getMinutes() === date2
.getMinutes() &&
538 date1
.getSeconds() === date2
.getSeconds() &&
539 date1
.getMilliseconds() === date2
.getMilliseconds()
543 date1
.getUTCHours() === date2
.getUTCHours() &&
544 date1
.getUTCMinutes() === date2
.getUTCMinutes() &&
545 date1
.getUTCSeconds() === date2
.getUTCSeconds() &&
546 date1
.getUTCMilliseconds() === date2
.getUTCMilliseconds()
552 * Test whether toggleLocal() changes the date part
557 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.localChangesDatePart = function ( date
) {
559 date
.getUTCFullYear() !== date
.getFullYear() ||
560 date
.getUTCMonth() !== date
.getMonth() ||
561 date
.getUTCDate() !== date
.getDate()
566 * Create a new Date by merging the date part from one with the time part from
569 * @param {Date} datepart
570 * @param {Date} timepart
573 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.mergeDateAndTime = function ( datepart
, timepart
) {
574 var ret
= new Date( datepart
.getTime() );
579 timepart
.getMinutes(),
580 timepart
.getSeconds(),
581 timepart
.getMilliseconds()
585 timepart
.getUTCHours(),
586 timepart
.getUTCMinutes(),
587 timepart
.getUTCSeconds(),
588 timepart
.getUTCMilliseconds()
596 * Get data for a calendar grid
599 * - display: {string} Display text for the day.
600 * - date: {Date} Date to use when the day is selected.
601 * - extra: {string|null} 'prev' or 'next' on days used to fill out the weeks
602 * at the start and end of the month.
604 * In any one result object, 'extra' + 'display' will always be unique.
607 * @param {Date|null} current Current date
608 * @return {Object} Data
609 * @return {string} return.header String to display as the calendar header
610 * @return {string} return.monthComponent Component to adjust by ±1 to change months.
611 * @return {string} return.dayComponent Component to adjust by ±1 to change days.
612 * @return {string} [return.weekComponent] Component to adjust by ±1 to change
613 * weeks. If omitted, the dayComponent should be adjusted by ±the number of
614 * non-nullable columns returned by this.getCalendarHeadings() to change weeks.
615 * @return {Array} return.rows Array of arrays of "day" objects or null/undefined.
617 mw
.widgets
.datetime
.DateTimeFormatter
.prototype.getCalendarData = function ( /* components */ ) {
618 // Should be overridden by subclass
621 monthComponent
: 'month',
627 }( jQuery
, mediaWiki
) );