1 /* eslint-disable no-restricted-properties */
5 * Provides various methods needed for formatting dates and times. This
6 * implementation implements the proleptic Gregorian calendar over years
10 * @extends mw.widgets.datetime.DateTimeFormatter
13 * @param {Object} [config] Configuration options
14 * @cfg {Object} [fullMonthNames] Mapping 1–12 to full month names.
15 * @cfg {Object} [shortMonthNames] Mapping 1–12 to abbreviated month names.
16 * If {@link #fullMonthNames fullMonthNames} is given and this is not,
17 * defaults to the first three characters from that setting.
18 * @cfg {Object} [fullDayNames] Mapping 0–6 to full day of week names. 0 is Sunday, 6 is Saturday.
19 * @cfg {Object} [shortDayNames] Mapping 0–6 to abbreviated day of week names. 0 is Sunday, 6 is Saturday.
20 * If {@link #fullDayNames fullDayNames} is given and this is not, defaults to
21 * the first three characters from that setting.
22 * @cfg {string[]} [dayLetters] Weekday column headers for a calendar. Array of 7 strings.
23 * If {@link #fullDayNames fullDayNames} or {@link #shortDayNames shortDayNames}
24 * are given and this is not, defaults to the first character from
26 * @cfg {string[]} [hour12Periods] AM and PM texts. Array of 2 strings, AM and PM.
27 * @cfg {number} [weekStartsOn=0] What day the week starts on: 0 is Sunday, 1 is Monday, 6 is Saturday.
29 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
= function MwWidgetsDatetimeProlepticGregorianDateTimeFormatter( config
) {
30 this.constructor.static.setupDefaults();
34 hour12Periods
: this.constructor.static.hour12Periods
37 if ( config
.fullMonthNames
&& !config
.shortMonthNames
) {
38 config
.shortMonthNames
= {};
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 $.each( config
.shortDayNames
, function ( k
, v
) {
46 config
.dayLetters
[ k
] = v
.substr( 0, 1 );
49 if ( config
.fullDayNames
&& !config
.dayLetters
) {
50 config
.dayLetters
= [];
51 $.each( config
.fullDayNames
, function ( k
, v
) {
52 config
.dayLetters
[ k
] = v
.substr( 0, 1 );
55 if ( config
.fullDayNames
&& !config
.shortDayNames
) {
56 config
.shortDayNames
= {};
57 $.each( config
.fullDayNames
, function ( k
, v
) {
58 config
.shortDayNames
[ k
] = v
.substr( 0, 3 );
62 fullMonthNames
: this.constructor.static.fullMonthNames
,
63 shortMonthNames
: this.constructor.static.shortMonthNames
,
64 fullDayNames
: this.constructor.static.fullDayNames
,
65 shortDayNames
: this.constructor.static.shortDayNames
,
66 dayLetters
: this.constructor.static.dayLetters
70 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'super' ].call( this, config
);
73 this.weekStartsOn
= config
.weekStartsOn
% 7;
74 this.fullMonthNames
= config
.fullMonthNames
;
75 this.shortMonthNames
= config
.shortMonthNames
;
76 this.fullDayNames
= config
.fullDayNames
;
77 this.shortDayNames
= config
.shortDayNames
;
78 this.dayLetters
= config
.dayLetters
;
79 this.hour12Periods
= config
.hour12Periods
;
84 OO
.inheritClass( mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
, mw
.widgets
.datetime
.DateTimeFormatter
);
91 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.formats
= {
92 '@time': '${hour|0}:${minute|0}:${second|0}',
93 '@date': '$!{dow|short} ${day|#} ${month|short} ${year|#}',
94 '@datetime': '$!{dow|short} ${day|#} ${month|short} ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}',
95 '@default': '$!{dow|short} ${day|#} ${month|short} ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}'
99 * Default full month names.
105 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.fullMonthNames
= null;
108 * Default abbreviated month names.
114 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.shortMonthNames
= null;
117 * Default full day of week names.
123 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.fullDayNames
= null;
126 * Default abbreviated day of week names.
132 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.shortDayNames
= null;
135 * Default day letters.
139 * @property {string[]}
141 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.dayLetters
= null;
144 * Default AM/PM indicators
148 * @property {string[]}
150 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.hour12Periods
= null;
152 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.setupDefaults = function () {
153 mw
.widgets
.datetime
.DateTimeFormatter
.static.setupDefaults
.call( this );
155 if ( this.fullMonthNames
&& !this.shortMonthNames
) {
156 this.shortMonthNames
= {};
157 $.each( this.fullMonthNames
, function ( k
, v
) {
158 this.shortMonthNames
[ k
] = v
.substr( 0, 3 );
161 if ( this.shortDayNames
&& !this.dayLetters
) {
162 this.dayLetters
= [];
163 $.each( this.shortDayNames
, function ( k
, v
) {
164 this.dayLetters
[ k
] = v
.substr( 0, 1 );
167 if ( this.fullDayNames
&& !this.dayLetters
) {
168 this.dayLetters
= [];
169 $.each( this.fullDayNames
, function ( k
, v
) {
170 this.dayLetters
[ k
] = v
.substr( 0, 1 );
173 if ( this.fullDayNames
&& !this.shortDayNames
) {
174 this.shortDayNames
= {};
175 $.each( this.fullDayNames
, function ( k
, v
) {
176 this.shortDayNames
[ k
] = v
.substr( 0, 3 );
180 if ( !this.fullMonthNames
) {
181 this.fullMonthNames
= {
182 1: mw
.msg( 'january' ),
183 2: mw
.msg( 'february' ),
184 3: mw
.msg( 'march' ),
185 4: mw
.msg( 'april' ),
186 5: mw
.msg( 'may_long' ),
189 8: mw
.msg( 'august' ),
190 9: mw
.msg( 'september' ),
191 10: mw
.msg( 'october' ),
192 11: mw
.msg( 'november' ),
193 12: mw
.msg( 'december' )
196 if ( !this.shortMonthNames
) {
197 this.shortMonthNames
= {
213 if ( !this.fullDayNames
) {
214 this.fullDayNames
= {
215 0: mw
.msg( 'sunday' ),
216 1: mw
.msg( 'monday' ),
217 2: mw
.msg( 'tuesday' ),
218 3: mw
.msg( 'wednesday' ),
219 4: mw
.msg( 'thursday' ),
220 5: mw
.msg( 'friday' ),
221 6: mw
.msg( 'saturday' )
224 if ( !this.shortDayNames
) {
225 this.shortDayNames
= {
235 if ( !this.dayLetters
) {
236 this.dayLetters
= [];
237 $.each( this.shortDayNames
, function ( k
, v
) {
238 this.dayLetters
[ k
] = v
.substr( 0, 1 );
242 if ( !this.hour12Periods
) {
243 this.hour12Periods
= [
244 mw
.msg( 'period-am' ),
245 mw
.msg( 'period-pm' )
255 * Additional fields implemented here are:
256 * - ${year|#}: Year as a number
257 * - ${year|0}: Year as a number, zero-padded to 4 digits
258 * - ${month|#}: Month as a number
259 * - ${month|0}: Month as a number with leading 0
260 * - ${month|short}: Month from 'shortMonthNames' configuration setting
261 * - ${month|full}: Month from 'fullMonthNames' configuration setting
262 * - ${day|#}: Day of the month as a number
263 * - ${day|0}: Day of the month as a number with leading 0
264 * - ${dow|short}: Day of the week from 'shortDayNames' configuration setting
265 * - ${dow|full}: Day of the week from 'fullDayNames' configuration setting
266 * - ${hour|#}: Hour as a number
267 * - ${hour|0}: Hour as a number with leading 0
268 * - ${hour|12}: Hour in a 12-hour clock as a number
269 * - ${hour|012}: Hour in a 12-hour clock as a number, with leading 0
270 * - ${hour|period}: Value from 'hour12Periods' configuration setting
271 * - ${minute|#}: Minute as a number
272 * - ${minute|0}: Minute as a number with leading 0
273 * - ${second|#}: Second as a number
274 * - ${second|0}: Second as a number with leading 0
275 * - ${millisecond|#}: Millisecond as a number
276 * - ${millisecond|0}: Millisecond as a number, zero-padded to 3 digits
278 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getFieldForTag = function ( tag
, params
) {
281 switch ( tag
+ '|' + params
[ 0 ] ) {
286 calendarComponent
: true,
289 zeropad
: params
[ 0 ] === '0'
297 calendarComponent
: true,
299 values
: params
[ 0 ] === 'short' ? this.shortMonthNames
: this.fullMonthNames
307 calendarComponent
: true,
310 values
: params
[ 0 ] === 'short' ? this.shortDayNames
: this.fullDayNames
320 calendarComponent
: true,
323 zeropad
: params
[ 0 ] === '0'
335 calendarComponent
: false,
338 zeropad
: params
[ 0 ] === '0'
346 calendarComponent
: false,
349 zeropad
: params
[ 0 ] === '012'
355 component
: 'hour12period',
356 calendarComponent
: false,
358 values
: this.hour12Periods
362 case 'millisecond|#':
363 case 'millisecond|0':
365 component
: 'millisecond',
366 calendarComponent
: false,
369 zeropad
: params
[ 0 ] === '0'
374 return mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'super' ].prototype.getFieldForTag
.call( this, tag
, params
);
378 if ( spec
.editable
=== undefined ) {
379 spec
.editable
= true;
381 spec
.formatValue
= this.formatSpecValue
;
382 spec
.parseValue
= this.parseSpecValue
;
384 spec
.size
= Math
.max
.apply(
385 null, $.map( spec
.values
, function ( v
) { return v
.length
; } )
394 * Get components from a Date object
398 * - month {number} (1-12)
399 * - day {number} (1-31)
400 * - dow {number} (0-6, 0 is Sunday)
401 * - hour {number} (0-23)
402 * - hour12 {number} (1-12)
403 * - hour12period {boolean}
404 * - minute {number} (0-59)
405 * - second {number} (0-59)
406 * - millisecond {number} (0-999)
409 * @param {Date|null} date
410 * @return {Object} Components
412 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getComponentsFromDate = function ( date
) {
415 if ( !( date
instanceof Date
) ) {
416 date
= this.defaultDate
;
421 year
: date
.getFullYear(),
422 month
: date
.getMonth() + 1,
424 dow
: date
.getDay() % 7,
425 hour
: date
.getHours(),
426 minute
: date
.getMinutes(),
427 second
: date
.getSeconds(),
428 millisecond
: date
.getMilliseconds(),
429 zone
: date
.getTimezoneOffset()
433 year
: date
.getUTCFullYear(),
434 month
: date
.getUTCMonth() + 1,
435 day
: date
.getUTCDate(),
436 dow
: date
.getUTCDay() % 7,
437 hour
: date
.getUTCHours(),
438 minute
: date
.getUTCMinutes(),
439 second
: date
.getUTCSeconds(),
440 millisecond
: date
.getUTCMilliseconds(),
445 ret
.hour12period
= ret
.hour
>= 12 ? 1 : 0;
446 ret
.hour12
= ret
.hour
% 12;
447 if ( ret
.hour12
=== 0 ) {
457 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getDateFromComponents = function ( components
) {
458 var date
= new Date();
460 components
= $.extend( {}, components
);
461 if ( components
.hour
=== undefined && components
.hour12
!== undefined && components
.hour12period
!== undefined ) {
462 components
.hour
= ( components
.hour12
% 12 ) + ( components
.hour12period
? 12 : 0 );
464 components
= $.extend( {}, this.getComponentsFromDate( null ), components
);
466 if ( components
.zone
) {
467 // Can't just use the constructor because that's stupid about ancient years.
468 date
.setFullYear( components
.year
, components
.month
- 1, components
.day
);
469 date
.setHours( components
.hour
, components
.minute
, components
.second
, components
.millisecond
);
471 // Date.UTC() is stupid about ancient years too.
472 date
.setUTCFullYear( components
.year
, components
.month
- 1, components
.day
);
473 date
.setUTCHours( components
.hour
, components
.minute
, components
.second
, components
.millisecond
);
482 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.adjustComponent = function ( date
, component
, delta
, mode
) {
483 var min
, max
, range
, components
;
485 if ( !( date
instanceof Date
) ) {
486 date
= this.defaultDate
;
488 components
= this.getComponentsFromDate( date
);
490 switch ( component
) {
501 max
= this.getDaysInMonth( components
.month
, components
.year
);
524 min
= components
.hour12period
? 12 : 0;
525 max
= components
.hour12period
? 23 : 11;
528 return new Date( date
.getTime() );
531 components
[ component
] += delta
;
532 range
= max
- min
+ 1;
535 // Date() will mostly handle it automatically. But months need
536 // manual handling to prevent e.g. Jan 31 => Mar 3.
537 if ( component
=== 'month' || component
=== 'year' ) {
538 while ( components
.month
< 1 ) {
539 components
[ component
] += 12;
542 while ( components
.month
> 12 ) {
543 components
[ component
] -= 12;
549 while ( components
[ component
] < min
) {
550 components
[ component
] += range
;
552 while ( components
[ component
] > max
) {
553 components
[ component
] -= range
;
557 if ( components
[ component
] < min
) {
558 components
[ component
] = min
;
560 if ( components
[ component
] < max
) {
561 components
[ component
] = max
;
565 if ( component
=== 'month' || component
=== 'year' ) {
566 components
.day
= Math
.min( components
.day
, this.getDaysInMonth( components
.month
, components
.year
) );
569 return this.getDateFromComponents( components
);
573 * Get the number of days in a month
576 * @param {number} month
577 * @param {number} year
580 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getDaysInMonth = function ( month
, year
) {
590 } else if ( year
% 100 ) {
593 return ( year
% 400 ) ? 28 : 29;
602 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getCalendarHeadings = function () {
603 var a
= this.dayLetters
;
605 if ( this.weekStartsOn
) {
606 return a
.slice( this.weekStartsOn
).concat( a
.slice( 0, this.weekStartsOn
) );
608 return a
.slice( 0 ); // clone
615 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.sameCalendarGrid = function ( date1
, date2
) {
617 return date1
.getFullYear() === date2
.getFullYear() && date1
.getMonth() === date2
.getMonth();
619 return date1
.getUTCFullYear() === date2
.getUTCFullYear() && date1
.getUTCMonth() === date2
.getUTCMonth();
626 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getCalendarData = function ( date
) {
627 var dt
, t
, d
, e
, i
, row
,
628 getDate
= this.local
? 'getDate' : 'getUTCDate',
629 setDate
= this.local
? 'setDate' : 'setUTCDate',
632 monthComponent
: 'month'
635 if ( !( date
instanceof Date
) ) {
636 date
= this.defaultDate
;
639 dt
= new Date( date
.getTime() );
644 ret
.header
= this.fullMonthNames
[ dt
.getMonth() + 1 ] + ' ' + dt
.getFullYear();
646 e
= this.getDaysInMonth( dt
.getMonth() + 1, dt
.getFullYear() );
648 ret
.header
= this.fullMonthNames
[ dt
.getUTCMonth() + 1 ] + ' ' + dt
.getUTCFullYear();
649 d
= dt
.getUTCDay() % 7;
650 e
= this.getDaysInMonth( dt
.getUTCMonth() + 1, dt
.getUTCFullYear() );
653 if ( this.weekStartsOn
) {
654 d
= ( d
+ 7 - this.weekStartsOn
) % 7;
661 for ( i
= 0; i
< 7; i
++, d
++ ) {
665 display
: String( dt
[ getDate
]() ),
667 extra
: d
< 1 ? 'prev' : d
> e
? 'next' : null
670 ret
.rows
.push( row
);
676 }( jQuery
, mediaWiki
) );