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 $.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 ] ) {
285 calendarComponent
: true,
288 zeropad
: params
[ 0 ] === '0'
296 calendarComponent
: true,
298 values
: params
[ 0 ] === 'short' ? this.shortMonthNames
: this.fullMonthNames
306 calendarComponent
: true,
309 values
: params
[ 0 ] === 'short' ? this.shortDayNames
: this.fullDayNames
319 calendarComponent
: true,
322 zeropad
: params
[ 0 ] === '0'
334 calendarComponent
: false,
337 zeropad
: params
[ 0 ] === '0'
345 calendarComponent
: false,
348 zeropad
: params
[ 0 ] === '012'
354 component
: 'hour12period',
355 calendarComponent
: false,
357 values
: this.hour12Periods
361 case 'millisecond|#':
362 case 'millisecond|0':
364 component
: 'millisecond',
365 calendarComponent
: false,
368 zeropad
: params
[ 0 ] === '0'
373 return mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'super' ].prototype.getFieldForTag
.call( this, tag
, params
);
377 if ( spec
.editable
=== undefined ) {
378 spec
.editable
= true;
380 spec
.formatValue
= this.formatSpecValue
;
381 spec
.parseValue
= this.parseSpecValue
;
383 spec
.size
= Math
.max
.apply(
384 null, $.map( spec
.values
, function ( v
) { return v
.length
; } )
393 * Get components from a Date object
397 * - month {number} (1-12)
398 * - day {number} (1-31)
399 * - dow {number} (0-6, 0 is Sunday)
400 * - hour {number} (0-23)
401 * - hour12 {number} (1-12)
402 * - hour12period {boolean}
403 * - minute {number} (0-59)
404 * - second {number} (0-59)
405 * - millisecond {number} (0-999)
408 * @param {Date|null} date
409 * @return {Object} Components
411 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getComponentsFromDate = function ( date
) {
414 if ( !( date
instanceof Date
) ) {
415 date
= this.defaultDate
;
420 year
: date
.getFullYear(),
421 month
: date
.getMonth() + 1,
423 dow
: date
.getDay() % 7,
424 hour
: date
.getHours(),
425 minute
: date
.getMinutes(),
426 second
: date
.getSeconds(),
427 millisecond
: date
.getMilliseconds(),
428 zone
: date
.getTimezoneOffset()
432 year
: date
.getUTCFullYear(),
433 month
: date
.getUTCMonth() + 1,
434 day
: date
.getUTCDate(),
435 dow
: date
.getUTCDay() % 7,
436 hour
: date
.getUTCHours(),
437 minute
: date
.getUTCMinutes(),
438 second
: date
.getUTCSeconds(),
439 millisecond
: date
.getUTCMilliseconds(),
444 ret
.hour12period
= ret
.hour
>= 12 ? 1 : 0;
445 ret
.hour12
= ret
.hour
% 12;
446 if ( ret
.hour12
=== 0 ) {
456 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getDateFromComponents = function ( components
) {
457 var date
= new Date();
459 components
= $.extend( {}, components
);
460 if ( components
.hour
=== undefined && components
.hour12
!== undefined && components
.hour12period
!== undefined ) {
461 components
.hour
= ( components
.hour12
% 12 ) + ( components
.hour12period
? 12 : 0 );
463 components
= $.extend( {}, this.getComponentsFromDate( null ), components
);
465 if ( components
.zone
) {
466 // Can't just use the constructor because that's stupid about ancient years.
467 date
.setFullYear( components
.year
, components
.month
- 1, components
.day
);
468 date
.setHours( components
.hour
, components
.minute
, components
.second
, components
.millisecond
);
470 // Date.UTC() is stupid about ancient years too.
471 date
.setUTCFullYear( components
.year
, components
.month
- 1, components
.day
);
472 date
.setUTCHours( components
.hour
, components
.minute
, components
.second
, components
.millisecond
);
481 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.adjustComponent = function ( date
, component
, delta
, mode
) {
482 var min
, max
, range
, components
;
484 if ( !( date
instanceof Date
) ) {
485 date
= this.defaultDate
;
487 components
= this.getComponentsFromDate( date
);
489 switch ( component
) {
500 max
= this.getDaysInMonth( components
.month
, components
.year
);
523 min
= components
.hour12period
? 12 : 0;
524 max
= components
.hour12period
? 23 : 11;
527 return new Date( date
.getTime() );
530 components
[ component
] += delta
;
531 range
= max
- min
+ 1;
534 // Date() will mostly handle it automatically. But months need
535 // manual handling to prevent e.g. Jan 31 => Mar 3.
536 if ( component
=== 'month' || component
=== 'year' ) {
537 while ( components
.month
< 1 ) {
538 components
[ component
] += 12;
541 while ( components
.month
> 12 ) {
542 components
[ component
] -= 12;
548 while ( components
[ component
] < min
) {
549 components
[ component
] += range
;
551 while ( components
[ component
] > max
) {
552 components
[ component
] -= range
;
556 if ( components
[ component
] < min
) {
557 components
[ component
] = min
;
559 if ( components
[ component
] < max
) {
560 components
[ component
] = max
;
564 if ( component
=== 'month' || component
=== 'year' ) {
565 components
.day
= Math
.min( components
.day
, this.getDaysInMonth( components
.month
, components
.year
) );
568 return this.getDateFromComponents( components
);
572 * Get the number of days in a month
575 * @param {number} month
576 * @param {number} year
579 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getDaysInMonth = function ( month
, year
) {
589 } else if ( year
% 100 ) {
592 return ( year
% 400 ) ? 28 : 29;
601 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getCalendarHeadings = function () {
602 var a
= this.dayLetters
;
604 if ( this.weekStartsOn
) {
605 return a
.slice( this.weekStartsOn
).concat( a
.slice( 0, this.weekStartsOn
) );
607 return a
.slice( 0 ); // clone
614 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.sameCalendarGrid = function ( date1
, date2
) {
616 return date1
.getFullYear() === date2
.getFullYear() && date1
.getMonth() === date2
.getMonth();
618 return date1
.getUTCFullYear() === date2
.getUTCFullYear() && date1
.getUTCMonth() === date2
.getUTCMonth();
625 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getCalendarData = function ( date
) {
626 var dt
, t
, d
, e
, i
, row
,
627 getDate
= this.local
? 'getDate' : 'getUTCDate',
628 setDate
= this.local
? 'setDate' : 'setUTCDate',
631 monthComponent
: 'month'
634 if ( !( date
instanceof Date
) ) {
635 date
= this.defaultDate
;
638 dt
= new Date( date
.getTime() );
643 ret
.header
= this.fullMonthNames
[ dt
.getMonth() + 1 ] + ' ' + dt
.getFullYear();
645 e
= this.getDaysInMonth( dt
.getMonth() + 1, dt
.getFullYear() );
647 ret
.header
= this.fullMonthNames
[ dt
.getUTCMonth() + 1 ] + ' ' + dt
.getUTCFullYear();
648 d
= dt
.getUTCDay() % 7;
649 e
= this.getDaysInMonth( dt
.getUTCMonth() + 1, dt
.getUTCFullYear() );
652 if ( this.weekStartsOn
) {
653 d
= ( d
+ 7 - this.weekStartsOn
) % 7;
660 for ( i
= 0; i
< 7; i
++, d
++ ) {
664 display
: String( dt
[ getDate
]() ),
666 extra
: d
< 1 ? 'prev' : d
> e
? 'next' : null
669 ret
.rows
.push( row
);
675 }( jQuery
, mediaWiki
) );