4 * Provides various methods needed for formatting dates and times. This
5 * implementation implments 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 $.each( config
.fullMonthNames
, function ( k
, v
) {
39 config
.shortMonthNames
[ k
] = v
.substr( 0, 3 );
42 if ( config
.shortDayNames
&& !config
.dayLetters
) {
43 config
.dayLetters
= [];
44 $.each( config
.shortDayNames
, function ( k
, v
) {
45 config
.dayLetters
[ k
] = v
.substr( 0, 1 );
48 if ( config
.fullDayNames
&& !config
.dayLetters
) {
49 config
.dayLetters
= [];
50 $.each( config
.fullDayNames
, function ( k
, v
) {
51 config
.dayLetters
[ k
] = v
.substr( 0, 1 );
54 if ( config
.fullDayNames
&& !config
.shortDayNames
) {
55 config
.shortDayNames
= {};
56 $.each( config
.fullDayNames
, function ( k
, v
) {
57 config
.shortDayNames
[ k
] = v
.substr( 0, 3 );
61 fullMonthNames
: this.constructor.static.fullMonthNames
,
62 shortMonthNames
: this.constructor.static.shortMonthNames
,
63 fullDayNames
: this.constructor.static.fullDayNames
,
64 shortDayNames
: this.constructor.static.shortDayNames
,
65 dayLetters
: this.constructor.static.dayLetters
69 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'super' ].call( this, config
);
72 this.weekStartsOn
= config
.weekStartsOn
% 7;
73 this.fullMonthNames
= config
.fullMonthNames
;
74 this.shortMonthNames
= config
.shortMonthNames
;
75 this.fullDayNames
= config
.fullDayNames
;
76 this.shortDayNames
= config
.shortDayNames
;
77 this.dayLetters
= config
.dayLetters
;
78 this.hour12Periods
= config
.hour12Periods
;
83 OO
.inheritClass( mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
, mw
.widgets
.datetime
.DateTimeFormatter
);
90 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.formats
= {
91 '@time': '${hour|0}:${minute|0}:${second|0}',
92 '@date': '$!{dow|short} ${day|#} ${month|short} ${year|#}',
93 '@datetime': '$!{dow|short} ${day|#} ${month|short} ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}',
94 '@default': '$!{dow|short} ${day|#} ${month|short} ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}'
98 * Default full month names.
104 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.fullMonthNames
= null;
107 * Default abbreviated month names.
113 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.shortMonthNames
= null;
116 * Default full day of week names.
122 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.fullDayNames
= null;
125 * Default abbreviated day of week names.
131 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.shortDayNames
= null;
134 * Default day letters.
138 * @property {string[]}
140 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.dayLetters
= null;
143 * Default AM/PM indicators
147 * @property {string[]}
149 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.hour12Periods
= null;
151 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.static.setupDefaults = function () {
152 mw
.widgets
.datetime
.DateTimeFormatter
.static.setupDefaults
.call( this );
154 if ( this.fullMonthNames
&& !this.shortMonthNames
) {
155 this.shortMonthNames
= {};
156 $.each( this.fullMonthNames
, function ( k
, v
) {
157 this.shortMonthNames
[ k
] = v
.substr( 0, 3 );
160 if ( this.shortDayNames
&& !this.dayLetters
) {
161 this.dayLetters
= [];
162 $.each( this.shortDayNames
, function ( k
, v
) {
163 this.dayLetters
[ k
] = v
.substr( 0, 1 );
166 if ( this.fullDayNames
&& !this.dayLetters
) {
167 this.dayLetters
= [];
168 $.each( this.fullDayNames
, function ( k
, v
) {
169 this.dayLetters
[ k
] = v
.substr( 0, 1 );
172 if ( this.fullDayNames
&& !this.shortDayNames
) {
173 this.shortDayNames
= {};
174 $.each( this.fullDayNames
, function ( k
, v
) {
175 this.shortDayNames
[ k
] = v
.substr( 0, 3 );
179 if ( !this.fullMonthNames
) {
180 this.fullMonthNames
= {
181 1: mw
.msg( 'january' ),
182 2: mw
.msg( 'february' ),
183 3: mw
.msg( 'march' ),
184 4: mw
.msg( 'april' ),
185 5: mw
.msg( 'may_long' ),
188 8: mw
.msg( 'august' ),
189 9: mw
.msg( 'september' ),
190 10: mw
.msg( 'october' ),
191 11: mw
.msg( 'november' ),
192 12: mw
.msg( 'december' )
195 if ( !this.shortMonthNames
) {
196 this.shortMonthNames
= {
212 if ( !this.fullDayNames
) {
213 this.fullDayNames
= {
214 0: mw
.msg( 'sunday' ),
215 1: mw
.msg( 'monday' ),
216 2: mw
.msg( 'tuesday' ),
217 3: mw
.msg( 'wednesday' ),
218 4: mw
.msg( 'thursday' ),
219 5: mw
.msg( 'friday' ),
220 6: mw
.msg( 'saturday' )
223 if ( !this.shortDayNames
) {
224 this.shortDayNames
= {
234 if ( !this.dayLetters
) {
235 this.dayLetters
= [];
236 $.each( this.shortDayNames
, function ( k
, v
) {
237 this.dayLetters
[ k
] = v
.substr( 0, 1 );
241 if ( !this.hour12Periods
) {
242 this.hour12Periods
= [
243 mw
.msg( 'period-am' ),
244 mw
.msg( 'period-pm' )
254 * Additional fields implemented here are:
255 * - ${year|#}: Year as a number
256 * - ${year|0}: Year as a number, zero-padded to 4 digits
257 * - ${month|#}: Month as a number
258 * - ${month|0}: Month as a number with leading 0
259 * - ${month|short}: Month from 'shortMonthNames' configuration setting
260 * - ${month|full}: Month from 'fullMonthNames' configuration setting
261 * - ${day|#}: Day of the month as a number
262 * - ${day|0}: Day of the month as a number with leading 0
263 * - ${dow|short}: Day of the week from 'shortDayNames' configuration setting
264 * - ${dow|full}: Day of the week from 'fullDayNames' configuration setting
265 * - ${hour|#}: Hour as a number
266 * - ${hour|0}: Hour as a number with leading 0
267 * - ${hour|12}: Hour in a 12-hour clock as a number
268 * - ${hour|012}: Hour in a 12-hour clock as a number, with leading 0
269 * - ${hour|period}: Value from 'hour12Periods' configuration setting
270 * - ${minute|#}: Minute as a number
271 * - ${minute|0}: Minute as a number with leading 0
272 * - ${second|#}: Second as a number
273 * - ${second|0}: Second as a number with leading 0
274 * - ${millisecond|#}: Millisecond as a number
275 * - ${millisecond|0}: Millisecond as a number, zero-padded to 3 digits
277 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getFieldForTag = function ( tag
, params
) {
280 switch ( tag
+ '|' + params
[ 0 ] ) {
287 zeropad
: params
[ 0 ] === '0'
296 values
: params
[ 0 ] === 'short' ? this.shortMonthNames
: this.fullMonthNames
306 values
: params
[ 0 ] === 'short' ? this.shortDayNames
: this.fullDayNames
324 zeropad
: params
[ 0 ] === '0'
334 zeropad
: params
[ 0 ] === '012'
340 component
: 'hour12period',
342 values
: this.hour12Periods
346 case 'millisecond|#':
347 case 'millisecond|0':
349 component
: 'millisecond',
352 zeropad
: params
[ 0 ] === '0'
357 return mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'super' ].prototype.getFieldForTag
.call( this, tag
, params
);
361 if ( spec
.editable
=== undefined ) {
362 spec
.editable
= true;
364 spec
.formatValue
= this.formatSpecValue
;
365 spec
.parseValue
= this.parseSpecValue
;
367 spec
.size
= Math
.max
.apply(
368 null, $.map( spec
.values
, function ( v
) { return v
.length
; } )
377 * Get components from a Date object
381 * - month {number} (1-12)
382 * - day {number} (1-31)
383 * - dow {number} (0-6, 0 is Sunday)
384 * - hour {number} (0-23)
385 * - hour12 {number} (1-12)
386 * - hour12period {boolean}
387 * - minute {number} (0-59)
388 * - second {number} (0-59)
389 * - millisecond {number} (0-999)
392 * @param {Date|null} date
393 * @return {Object} Components
395 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getComponentsFromDate = function ( date
) {
398 if ( !( date
instanceof Date
) ) {
399 date
= this.defaultDate
;
404 year
: date
.getFullYear(),
405 month
: date
.getMonth() + 1,
407 dow
: date
.getDay() % 7,
408 hour
: date
.getHours(),
409 minute
: date
.getMinutes(),
410 second
: date
.getSeconds(),
411 millisecond
: date
.getMilliseconds(),
412 zone
: date
.getTimezoneOffset()
416 year
: date
.getUTCFullYear(),
417 month
: date
.getUTCMonth() + 1,
418 day
: date
.getUTCDate(),
419 dow
: date
.getUTCDay() % 7,
420 hour
: date
.getUTCHours(),
421 minute
: date
.getUTCMinutes(),
422 second
: date
.getUTCSeconds(),
423 millisecond
: date
.getUTCMilliseconds(),
428 ret
.hour12period
= ret
.hour
>= 12 ? 1 : 0;
429 ret
.hour12
= ret
.hour
% 12;
430 if ( ret
.hour12
=== 0 ) {
440 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getDateFromComponents = function ( components
) {
441 var date
= new Date();
443 components
= $.extend( {}, components
);
444 if ( components
.hour
=== undefined && components
.hour12
!== undefined && components
.hour12period
!== undefined ) {
445 components
.hour
= ( components
.hour12
% 12 ) + ( components
.hour12period
? 12 : 0 );
447 components
= $.extend( {}, this.getComponentsFromDate( null ), components
);
449 if ( components
.zone
) {
450 // Can't just use the constructor because that's stupid about ancient years.
451 date
.setFullYear( components
.year
, components
.month
- 1, components
.day
);
452 date
.setHours( components
.hour
, components
.minute
, components
.second
, components
.millisecond
);
454 // Date.UTC() is stupid about ancient years too.
455 date
.setUTCFullYear( components
.year
, components
.month
- 1, components
.day
);
456 date
.setUTCHours( components
.hour
, components
.minute
, components
.second
, components
.millisecond
);
465 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.adjustComponent = function ( date
, component
, delta
, mode
) {
466 var min
, max
, range
, components
;
468 if ( !( date
instanceof Date
) ) {
469 date
= this.defaultDate
;
471 components
= this.getComponentsFromDate( date
);
473 switch ( component
) {
484 max
= this.getDaysInMonth( components
.month
, components
.year
);
507 min
= components
.hour12period
? 12 : 0;
508 max
= components
.hour12period
? 23 : 11;
511 return new Date( date
.getTime() );
514 components
[ component
] += delta
;
515 range
= max
- min
+ 1;
518 // Date() will mostly handle it automatically. But months need
519 // manual handling to prevent e.g. Jan 31 => Mar 3.
520 if ( component
=== 'month' || component
=== 'year' ) {
521 while ( components
.month
< 1 ) {
522 components
[ component
] += 12;
525 while ( components
.month
> 12 ) {
526 components
[ component
] -= 12;
532 while ( components
[ component
] < min
) {
533 components
[ component
] += range
;
535 while ( components
[ component
] > max
) {
536 components
[ component
] -= range
;
540 if ( components
[ component
] < min
) {
541 components
[ component
] = min
;
543 if ( components
[ component
] < max
) {
544 components
[ component
] = max
;
548 if ( component
=== 'month' || component
=== 'year' ) {
549 components
.day
= Math
.min( components
.day
, this.getDaysInMonth( components
.month
, components
.year
) );
552 return this.getDateFromComponents( components
);
556 * Get the number of days in a month
559 * @param {number} month
560 * @param {number} year
563 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getDaysInMonth = function ( month
, year
) {
573 } else if ( year
% 100 ) {
576 return ( year
% 400 ) ? 28 : 29;
585 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getCalendarHeadings = function () {
586 var a
= this.dayLetters
;
588 if ( this.weekStartsOn
) {
589 return a
.slice( this.weekStartsOn
).concat( a
.slice( 0, this.weekStartsOn
) );
591 return a
.slice( 0 ); // clone
598 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.sameCalendarGrid = function ( date1
, date2
) {
600 return date1
.getFullYear() === date2
.getFullYear() && date1
.getMonth() === date2
.getMonth();
602 return date1
.getUTCFullYear() === date2
.getUTCFullYear() && date1
.getUTCMonth() === date2
.getUTCMonth();
609 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getCalendarData = function ( date
) {
610 var dt
, t
, d
, e
, i
, row
,
611 getDate
= this.local
? 'getDate' : 'getUTCDate',
612 setDate
= this.local
? 'setDate' : 'setUTCDate',
615 monthComponent
: 'month'
618 if ( !( date
instanceof Date
) ) {
619 date
= this.defaultDate
;
622 dt
= new Date( date
.getTime() );
627 ret
.header
= this.fullMonthNames
[ dt
.getMonth() + 1 ] + ' ' + dt
.getFullYear();
629 e
= this.getDaysInMonth( dt
.getMonth() + 1, dt
.getFullYear() );
631 ret
.header
= this.fullMonthNames
[ dt
.getUTCMonth() + 1 ] + ' ' + dt
.getUTCFullYear();
632 d
= dt
.getUTCDay() % 7;
633 e
= this.getDaysInMonth( dt
.getUTCMonth() + 1, dt
.getUTCFullYear() );
636 if ( this.weekStartsOn
) {
637 d
= ( d
+ 7 - this.weekStartsOn
) % 7;
644 for ( i
= 0; i
< 7; i
++, d
++ ) {
648 display
: String( dt
[ getDate
]() ),
650 extra
: d
< 1 ? 'prev' : d
> e
? 'next' : null
653 ret
.rows
.push( row
);
659 }( jQuery
, mediaWiki
) );