4 * Provides various methods needed for formatting dates and times. This
5 * implementation implements the proleptic Gregorian calendar over years
9 * @extends mw.widgets.datetime.DateTimeFormatter
12 * @param {Object} [config] Configuration options
13 * @cfg {Object} [fullMonthNames] Mapping 1–12 to full month names.
14 * @cfg {Object} [shortMonthNames] Mapping 1–12 to abbreviated month names.
15 * If {@link #fullMonthNames fullMonthNames} is given and this is not,
16 * defaults to the first three characters from that setting.
17 * @cfg {Object} [fullDayNames] Mapping 0–6 to full day of week names. 0 is Sunday, 6 is Saturday.
18 * @cfg {Object} [shortDayNames] Mapping 0–6 to abbreviated day of week names. 0 is Sunday, 6 is Saturday.
19 * If {@link #fullDayNames fullDayNames} is given and this is not, defaults to
20 * the first three characters from that setting.
21 * @cfg {string[]} [dayLetters] Weekday column headers for a calendar. Array of 7 strings.
22 * If {@link #fullDayNames fullDayNames} or {@link #shortDayNames shortDayNames}
23 * are given and this is not, defaults to the first character from
25 * @cfg {string[]} [hour12Periods] AM and PM texts. Array of 2 strings, AM and PM.
26 * @cfg {number} [weekStartsOn=0] What day the week starts on: 0 is Sunday, 1 is Monday, 6 is Saturday.
28 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
= function MwWidgetsDatetimeProlepticGregorianDateTimeFormatter( config
) {
29 this.constructor.static.setupDefaults();
33 hour12Periods
: this.constructor.static.hour12Periods
36 if ( config
.fullMonthNames
&& !config
.shortMonthNames
) {
37 config
.shortMonthNames
= {};
38 // eslint-disable-next-line no-jquery/no-each-util
39 $.each( config
.fullMonthNames
, function ( k
, v
) {
40 config
.shortMonthNames
[ k
] = v
.substr( 0, 3 );
43 if ( config
.shortDayNames
&& !config
.dayLetters
) {
44 config
.dayLetters
= [];
45 // eslint-disable-next-line no-jquery/no-each-util
46 $.each( config
.shortDayNames
, function ( k
, v
) {
47 config
.dayLetters
[ k
] = v
.substr( 0, 1 );
50 if ( config
.fullDayNames
&& !config
.dayLetters
) {
51 config
.dayLetters
= [];
52 // eslint-disable-next-line no-jquery/no-each-util
53 $.each( config
.fullDayNames
, function ( k
, v
) {
54 config
.dayLetters
[ k
] = v
.substr( 0, 1 );
57 if ( config
.fullDayNames
&& !config
.shortDayNames
) {
58 config
.shortDayNames
= {};
59 // eslint-disable-next-line no-jquery/no-each-util
60 $.each( config
.fullDayNames
, function ( k
, v
) {
61 config
.shortDayNames
[ k
] = v
.substr( 0, 3 );
65 fullMonthNames
: this.constructor.static.fullMonthNames
,
66 shortMonthNames
: this.constructor.static.shortMonthNames
,
67 fullDayNames
: this.constructor.static.fullDayNames
,
68 shortDayNames
: this.constructor.static.shortDayNames
,
69 dayLetters
: this.constructor.static.dayLetters
73 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.super.call( this, config
);
76 this.weekStartsOn
= config
.weekStartsOn
% 7;
77 this.fullMonthNames
= config
.fullMonthNames
;
78 this.shortMonthNames
= config
.shortMonthNames
;
79 this.fullDayNames
= config
.fullDayNames
;
80 this.shortDayNames
= config
.shortDayNames
;
81 this.dayLetters
= config
.dayLetters
;
82 this.hour12Periods
= config
.hour12Periods
;
87 OO
.inheritClass( mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
, mw
.widgets
.datetime
.DateTimeFormatter
);
94 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.formats
= {
95 '@time': '${hour|0}:${minute|0}:${second|0}',
96 '@date': '$!{dow|short} ${day|#} ${month|short} ${year|#}',
97 '@datetime': '$!{dow|short} ${day|#} ${month|short} ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}',
98 '@default': '$!{dow|short} ${day|#} ${month|short} ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}'
102 * Default full month names.
108 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.fullMonthNames
= null;
111 * Default abbreviated month names.
117 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.shortMonthNames
= null;
120 * Default full day of week names.
126 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.fullDayNames
= null;
129 * Default abbreviated day of week names.
135 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.shortDayNames
= null;
138 * Default day letters.
142 * @property {string[]}
144 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.dayLetters
= null;
147 * Default AM/PM indicators
151 * @property {string[]}
153 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.hour12Periods
= null;
155 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.setupDefaults = function () {
156 mw
.widgets
.datetime
.DateTimeFormatter
.static.setupDefaults
.call( this );
158 if ( this.fullMonthNames
&& !this.shortMonthNames
) {
159 this.shortMonthNames
= {};
160 // eslint-disable-next-line no-jquery/no-each-util
161 $.each( this.fullMonthNames
, function ( k
, v
) {
162 this.shortMonthNames
[ k
] = v
.substr( 0, 3 );
165 if ( this.shortDayNames
&& !this.dayLetters
) {
166 this.dayLetters
= [];
167 // eslint-disable-next-line no-jquery/no-each-util
168 $.each( this.shortDayNames
, function ( k
, v
) {
169 this.dayLetters
[ k
] = v
.substr( 0, 1 );
172 if ( this.fullDayNames
&& !this.dayLetters
) {
173 this.dayLetters
= [];
174 // eslint-disable-next-line no-jquery/no-each-util
175 $.each( this.fullDayNames
, function ( k
, v
) {
176 this.dayLetters
[ k
] = v
.substr( 0, 1 );
179 if ( this.fullDayNames
&& !this.shortDayNames
) {
180 this.shortDayNames
= {};
181 // eslint-disable-next-line no-jquery/no-each-util
182 $.each( this.fullDayNames
, function ( k
, v
) {
183 this.shortDayNames
[ k
] = v
.substr( 0, 3 );
187 if ( !this.fullMonthNames
) {
188 this.fullMonthNames
= {
189 1: mw
.msg( 'january' ),
190 2: mw
.msg( 'february' ),
191 3: mw
.msg( 'march' ),
192 4: mw
.msg( 'april' ),
193 5: mw
.msg( 'may_long' ),
196 8: mw
.msg( 'august' ),
197 9: mw
.msg( 'september' ),
198 10: mw
.msg( 'october' ),
199 11: mw
.msg( 'november' ),
200 12: mw
.msg( 'december' )
203 if ( !this.shortMonthNames
) {
204 this.shortMonthNames
= {
220 if ( !this.fullDayNames
) {
221 this.fullDayNames
= {
222 0: mw
.msg( 'sunday' ),
223 1: mw
.msg( 'monday' ),
224 2: mw
.msg( 'tuesday' ),
225 3: mw
.msg( 'wednesday' ),
226 4: mw
.msg( 'thursday' ),
227 5: mw
.msg( 'friday' ),
228 6: mw
.msg( 'saturday' )
231 if ( !this.shortDayNames
) {
232 this.shortDayNames
= {
242 if ( !this.dayLetters
) {
243 this.dayLetters
= [];
244 // eslint-disable-next-line no-jquery/no-each-util
245 $.each( this.shortDayNames
, function ( k
, v
) {
246 this.dayLetters
[ k
] = v
.substr( 0, 1 );
250 if ( !this.hour12Periods
) {
251 this.hour12Periods
= [
252 mw
.msg( 'period-am' ),
253 mw
.msg( 'period-pm' )
263 * Additional fields implemented here are:
264 * - ${year|#}: Year as a number
265 * - ${year|0}: Year as a number, zero-padded to 4 digits
266 * - ${month|#}: Month as a number
267 * - ${month|0}: Month as a number with leading 0
268 * - ${month|short}: Month from 'shortMonthNames' configuration setting
269 * - ${month|full}: Month from 'fullMonthNames' configuration setting
270 * - ${day|#}: Day of the month as a number
271 * - ${day|0}: Day of the month as a number with leading 0
272 * - ${dow|short}: Day of the week from 'shortDayNames' configuration setting
273 * - ${dow|full}: Day of the week from 'fullDayNames' configuration setting
274 * - ${hour|#}: Hour as a number
275 * - ${hour|0}: Hour as a number with leading 0
276 * - ${hour|12}: Hour in a 12-hour clock as a number
277 * - ${hour|012}: Hour in a 12-hour clock as a number, with leading 0
278 * - ${hour|period}: Value from 'hour12Periods' configuration setting
279 * - ${minute|#}: Minute as a number
280 * - ${minute|0}: Minute as a number with leading 0
281 * - ${second|#}: Second as a number
282 * - ${second|0}: Second as a number with leading 0
283 * - ${millisecond|#}: Millisecond as a number
284 * - ${millisecond|0}: Millisecond as a number, zero-padded to 3 digits
286 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getFieldForTag = function ( tag
, params
) {
289 switch ( tag
+ '|' + params
[ 0 ] ) {
294 calendarComponent
: true,
297 zeropad
: params
[ 0 ] === '0'
305 calendarComponent
: true,
307 values
: params
[ 0 ] === 'short' ? this.shortMonthNames
: this.fullMonthNames
315 calendarComponent
: true,
318 values
: params
[ 0 ] === 'short' ? this.shortDayNames
: this.fullDayNames
328 calendarComponent
: true,
331 zeropad
: params
[ 0 ] === '0'
343 calendarComponent
: false,
346 zeropad
: params
[ 0 ] === '0'
354 calendarComponent
: false,
357 zeropad
: params
[ 0 ] === '012'
363 component
: 'hour12period',
364 calendarComponent
: false,
366 values
: this.hour12Periods
370 case 'millisecond|#':
371 case 'millisecond|0':
373 component
: 'millisecond',
374 calendarComponent
: false,
377 zeropad
: params
[ 0 ] === '0'
382 return mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.super.prototype.getFieldForTag
.call( this, tag
, params
);
386 if ( spec
.editable
=== undefined ) {
387 spec
.editable
= true;
389 spec
.formatValue
= this.formatSpecValue
;
390 spec
.parseValue
= this.parseSpecValue
;
392 spec
.size
= Math
.max
.apply(
393 // eslint-disable-next-line no-jquery/no-map-util
394 null, $.map( spec
.values
, function ( v
) { return v
.length
; } )
403 * Get components from a Date object
407 * - month {number} (1-12)
408 * - day {number} (1-31)
409 * - dow {number} (0-6, 0 is Sunday)
410 * - hour {number} (0-23)
411 * - hour12 {number} (1-12)
412 * - hour12period {boolean}
413 * - minute {number} (0-59)
414 * - second {number} (0-59)
415 * - millisecond {number} (0-999)
418 * @param {Date|null} date
419 * @return {Object} Components
421 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getComponentsFromDate = function ( date
) {
424 if ( !( date
instanceof Date
) ) {
425 date
= this.defaultDate
;
430 year
: date
.getFullYear(),
431 month
: date
.getMonth() + 1,
433 dow
: date
.getDay() % 7,
434 hour
: date
.getHours(),
435 minute
: date
.getMinutes(),
436 second
: date
.getSeconds(),
437 millisecond
: date
.getMilliseconds(),
438 zone
: date
.getTimezoneOffset()
442 year
: date
.getUTCFullYear(),
443 month
: date
.getUTCMonth() + 1,
444 day
: date
.getUTCDate(),
445 dow
: date
.getUTCDay() % 7,
446 hour
: date
.getUTCHours(),
447 minute
: date
.getUTCMinutes(),
448 second
: date
.getUTCSeconds(),
449 millisecond
: date
.getUTCMilliseconds(),
454 ret
.hour12period
= ret
.hour
>= 12 ? 1 : 0;
455 ret
.hour12
= ret
.hour
% 12;
456 if ( ret
.hour12
=== 0 ) {
466 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getDateFromComponents = function ( components
) {
467 var date
= new Date();
469 components
= $.extend( {}, components
);
470 if ( components
.hour
=== undefined && components
.hour12
!== undefined && components
.hour12period
!== undefined ) {
471 components
.hour
= ( components
.hour12
% 12 ) + ( components
.hour12period
? 12 : 0 );
473 components
= $.extend( {}, this.getComponentsFromDate( null ), components
);
475 if ( components
.zone
) {
476 // Can't just use the constructor because that's stupid about ancient years.
477 date
.setFullYear( components
.year
, components
.month
- 1, components
.day
);
478 date
.setHours( components
.hour
, components
.minute
, components
.second
, components
.millisecond
);
480 // Date.UTC() is stupid about ancient years too.
481 date
.setUTCFullYear( components
.year
, components
.month
- 1, components
.day
);
482 date
.setUTCHours( components
.hour
, components
.minute
, components
.second
, components
.millisecond
);
491 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.adjustComponent = function ( date
, component
, delta
, mode
) {
492 var min
, max
, range
, components
;
494 if ( !( date
instanceof Date
) ) {
495 date
= this.defaultDate
;
497 components
= this.getComponentsFromDate( date
);
499 switch ( component
) {
510 max
= this.getDaysInMonth( components
.month
, components
.year
);
533 min
= components
.hour12period
? 12 : 0;
534 max
= components
.hour12period
? 23 : 11;
537 return new Date( date
.getTime() );
540 components
[ component
] += delta
;
541 range
= max
- min
+ 1;
544 // Date() will mostly handle it automatically. But months need
545 // manual handling to prevent e.g. Jan 31 => Mar 3.
546 if ( component
=== 'month' || component
=== 'year' ) {
547 while ( components
.month
< 1 ) {
548 components
[ component
] += 12;
551 while ( components
.month
> 12 ) {
552 components
[ component
] -= 12;
558 while ( components
[ component
] < min
) {
559 components
[ component
] += range
;
561 while ( components
[ component
] > max
) {
562 components
[ component
] -= range
;
566 if ( components
[ component
] < min
) {
567 components
[ component
] = min
;
569 if ( components
[ component
] < max
) {
570 components
[ component
] = max
;
574 if ( component
=== 'month' || component
=== 'year' ) {
575 components
.day
= Math
.min( components
.day
, this.getDaysInMonth( components
.month
, components
.year
) );
578 return this.getDateFromComponents( components
);
582 * Get the number of days in a month
585 * @param {number} month
586 * @param {number} year
589 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getDaysInMonth = function ( month
, year
) {
599 } else if ( year
% 100 ) {
602 return ( year
% 400 ) ? 28 : 29;
611 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getCalendarHeadings = function () {
612 var a
= this.dayLetters
;
614 if ( this.weekStartsOn
) {
615 return a
.slice( this.weekStartsOn
).concat( a
.slice( 0, this.weekStartsOn
) );
617 return a
.slice( 0 ); // clone
624 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.sameCalendarGrid = function ( date1
, date2
) {
626 return date1
.getFullYear() === date2
.getFullYear() && date1
.getMonth() === date2
.getMonth();
628 return date1
.getUTCFullYear() === date2
.getUTCFullYear() && date1
.getUTCMonth() === date2
.getUTCMonth();
635 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getCalendarData = function ( date
) {
636 var dt
, t
, d
, e
, i
, row
,
637 getDate
= this.local
? 'getDate' : 'getUTCDate',
638 setDate
= this.local
? 'setDate' : 'setUTCDate',
641 monthComponent
: 'month'
644 if ( !( date
instanceof Date
) ) {
645 date
= this.defaultDate
;
648 dt
= new Date( date
.getTime() );
653 ret
.header
= this.fullMonthNames
[ dt
.getMonth() + 1 ] + ' ' + dt
.getFullYear();
655 e
= this.getDaysInMonth( dt
.getMonth() + 1, dt
.getFullYear() );
657 ret
.header
= this.fullMonthNames
[ dt
.getUTCMonth() + 1 ] + ' ' + dt
.getUTCFullYear();
658 d
= dt
.getUTCDay() % 7;
659 e
= this.getDaysInMonth( dt
.getUTCMonth() + 1, dt
.getUTCFullYear() );
662 if ( this.weekStartsOn
) {
663 d
= ( d
+ 7 - this.weekStartsOn
) % 7;
670 for ( i
= 0; i
< 7; i
++, d
++ ) {
674 display
: String( dt
[ getDate
]() ),
676 extra
: d
< 1 ? 'prev' : d
> e
? 'next' : null
679 ret
.rows
.push( row
);