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 var statick
= this.constructor[ 'static' ];
31 statick
.setupDefaults();
35 hour12Periods
: statick
.hour12Periods
38 if ( config
.fullMonthNames
&& !config
.shortMonthNames
) {
39 config
.shortMonthNames
= {};
40 $.each( config
.fullMonthNames
, function ( k
, v
) {
41 config
.shortMonthNames
[ k
] = v
.substr( 0, 3 );
44 if ( config
.shortDayNames
&& !config
.dayLetters
) {
45 config
.dayLetters
= [];
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 $.each( config
.fullDayNames
, function ( k
, v
) {
53 config
.dayLetters
[ k
] = v
.substr( 0, 1 );
56 if ( config
.fullDayNames
&& !config
.shortDayNames
) {
57 config
.shortDayNames
= {};
58 $.each( config
.fullDayNames
, function ( k
, v
) {
59 config
.shortDayNames
[ k
] = v
.substr( 0, 3 );
63 fullMonthNames
: statick
.fullMonthNames
,
64 shortMonthNames
: statick
.shortMonthNames
,
65 fullDayNames
: statick
.fullDayNames
,
66 shortDayNames
: statick
.shortDayNames
,
67 dayLetters
: statick
.dayLetters
71 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'super' ].call( this, config
);
74 this.weekStartsOn
= config
.weekStartsOn
% 7;
75 this.fullMonthNames
= config
.fullMonthNames
;
76 this.shortMonthNames
= config
.shortMonthNames
;
77 this.fullDayNames
= config
.fullDayNames
;
78 this.shortDayNames
= config
.shortDayNames
;
79 this.dayLetters
= config
.dayLetters
;
80 this.hour12Periods
= config
.hour12Periods
;
85 OO
.inheritClass( mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
, mw
.widgets
.datetime
.DateTimeFormatter
);
92 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'static' ].formats
= {
93 '@time': '${hour|0}:${minute|0}:${second|0}',
94 '@date': '$!{dow|short} ${day|#} ${month|short} ${year|#}',
95 '@datetime': '$!{dow|short} ${day|#} ${month|short} ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}',
96 '@default': '$!{dow|short} ${day|#} ${month|short} ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}'
100 * Default full month names.
106 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'static' ].fullMonthNames
= null;
109 * Default abbreviated month names.
115 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'static' ].shortMonthNames
= null;
118 * Default full day of week names.
124 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'static' ].fullDayNames
= null;
127 * Default abbreviated day of week names.
133 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'static' ].shortDayNames
= null;
136 * Default day letters.
140 * @property {string[]}
142 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'static' ].dayLetters
= null;
145 * Default AM/PM indicators
149 * @property {string[]}
151 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'static' ].hour12Periods
= null;
153 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'static' ].setupDefaults = function () {
154 mw
.widgets
.datetime
.DateTimeFormatter
[ 'static' ].setupDefaults
.call( this );
156 if ( this.fullMonthNames
&& !this.shortMonthNames
) {
157 this.shortMonthNames
= {};
158 $.each( this.fullMonthNames
, function ( k
, v
) {
159 this.shortMonthNames
[ k
] = v
.substr( 0, 3 );
162 if ( this.shortDayNames
&& !this.dayLetters
) {
163 this.dayLetters
= [];
164 $.each( this.shortDayNames
, function ( k
, v
) {
165 this.dayLetters
[ k
] = v
.substr( 0, 1 );
168 if ( this.fullDayNames
&& !this.dayLetters
) {
169 this.dayLetters
= [];
170 $.each( this.fullDayNames
, function ( k
, v
) {
171 this.dayLetters
[ k
] = v
.substr( 0, 1 );
174 if ( this.fullDayNames
&& !this.shortDayNames
) {
175 this.shortDayNames
= {};
176 $.each( this.fullDayNames
, function ( k
, v
) {
177 this.shortDayNames
[ k
] = v
.substr( 0, 3 );
181 if ( !this.fullMonthNames
) {
182 this.fullMonthNames
= {
183 1: mw
.msg( 'january' ),
184 2: mw
.msg( 'february' ),
185 3: mw
.msg( 'march' ),
186 4: mw
.msg( 'april' ),
187 5: mw
.msg( 'may_long' ),
190 8: mw
.msg( 'august' ),
191 9: mw
.msg( 'september' ),
192 10: mw
.msg( 'october' ),
193 11: mw
.msg( 'november' ),
194 12: mw
.msg( 'december' )
197 if ( !this.shortMonthNames
) {
198 this.shortMonthNames
= {
214 if ( !this.fullDayNames
) {
215 this.fullDayNames
= {
216 0: mw
.msg( 'sunday' ),
217 1: mw
.msg( 'monday' ),
218 2: mw
.msg( 'tuesday' ),
219 3: mw
.msg( 'wednesday' ),
220 4: mw
.msg( 'thursday' ),
221 5: mw
.msg( 'friday' ),
222 6: mw
.msg( 'saturday' )
225 if ( !this.shortDayNames
) {
226 this.shortDayNames
= {
236 if ( !this.dayLetters
) {
237 this.dayLetters
= [];
238 $.each( this.shortDayNames
, function ( k
, v
) {
239 this.dayLetters
[ k
] = v
.substr( 0, 1 );
243 if ( !this.hour12Periods
) {
244 this.hour12Periods
= [
245 mw
.msg( 'period-am' ),
246 mw
.msg( 'period-pm' )
256 * Additional fields implemented here are:
257 * - ${year|#}: Year as a number
258 * - ${year|0}: Year as a number, zero-padded to 4 digits
259 * - ${month|#}: Month as a number
260 * - ${month|0}: Month as a number with leading 0
261 * - ${month|short}: Month from 'shortMonthNames' configuration setting
262 * - ${month|full}: Month from 'fullMonthNames' configuration setting
263 * - ${day|#}: Day of the month as a number
264 * - ${day|0}: Day of the month as a number with leading 0
265 * - ${dow|short}: Day of the week from 'shortDayNames' configuration setting
266 * - ${dow|full}: Day of the week from 'fullDayNames' configuration setting
267 * - ${hour|#}: Hour as a number
268 * - ${hour|0}: Hour as a number with leading 0
269 * - ${hour|12}: Hour in a 12-hour clock as a number
270 * - ${hour|012}: Hour in a 12-hour clock as a number, with leading 0
271 * - ${hour|period}: Value from 'hour12Periods' configuration setting
272 * - ${minute|#}: Minute as a number
273 * - ${minute|0}: Minute as a number with leading 0
274 * - ${second|#}: Second as a number
275 * - ${second|0}: Second as a number with leading 0
276 * - ${millisecond|#}: Millisecond as a number
277 * - ${millisecond|0}: Millisecond as a number, zero-padded to 3 digits
279 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getFieldForTag = function ( tag
, params
) {
282 switch ( tag
+ '|' + params
[ 0 ] ) {
289 zeropad
: params
[ 0 ] === '0'
298 values
: params
[ 0 ] === 'short' ? this.shortMonthNames
: this.fullMonthNames
308 values
: params
[ 0 ] === 'short' ? this.shortDayNames
: this.fullDayNames
326 zeropad
: params
[ 0 ] === '0'
336 zeropad
: params
[ 0 ] === '012'
342 component
: 'hour12period',
344 values
: this.hour12Periods
348 case 'millisecond|#':
349 case 'millisecond|0':
351 component
: 'millisecond',
354 zeropad
: params
[ 0 ] === '0'
359 return mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'super' ].prototype.getFieldForTag
.call( this, tag
, params
);
363 if ( spec
.editable
=== undefined ) {
364 spec
.editable
= true;
366 spec
.formatValue
= this.formatSpecValue
;
367 spec
.parseValue
= this.parseSpecValue
;
369 spec
.size
= Math
.max
.apply(
370 null, $.map( spec
.values
, function ( v
) { return v
.length
; } )
379 * Get components from a Date object
383 * - month {number} (1-12)
384 * - day {number} (1-31)
385 * - dow {number} (0-6, 0 is Sunday)
386 * - hour {number} (0-23)
387 * - hour12 {number} (1-12)
388 * - hour12period {boolean}
389 * - minute {number} (0-59)
390 * - second {number} (0-59)
391 * - millisecond {number} (0-999)
394 * @param {Date|null} date
395 * @return {Object} Components
397 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getComponentsFromDate = function ( date
) {
400 if ( !( date
instanceof Date
) ) {
401 date
= this.defaultDate
;
406 year
: date
.getFullYear(),
407 month
: date
.getMonth() + 1,
409 dow
: date
.getDay() % 7,
410 hour
: date
.getHours(),
411 minute
: date
.getMinutes(),
412 second
: date
.getSeconds(),
413 millisecond
: date
.getMilliseconds(),
414 zone
: date
.getTimezoneOffset()
418 year
: date
.getUTCFullYear(),
419 month
: date
.getUTCMonth() + 1,
420 day
: date
.getUTCDate(),
421 dow
: date
.getUTCDay() % 7,
422 hour
: date
.getUTCHours(),
423 minute
: date
.getUTCMinutes(),
424 second
: date
.getUTCSeconds(),
425 millisecond
: date
.getUTCMilliseconds(),
430 ret
.hour12period
= ret
.hour
>= 12 ? 1 : 0;
431 ret
.hour12
= ret
.hour
% 12;
432 if ( ret
.hour12
=== 0 ) {
442 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getDateFromComponents = function ( components
) {
443 var date
= new Date();
445 components
= $.extend( {}, components
);
446 if ( components
.hour
=== undefined && components
.hour12
!== undefined && components
.hour12period
!== undefined ) {
447 components
.hour
= ( components
.hour12
% 12 ) + ( components
.hour12period
? 12 : 0 );
449 components
= $.extend( {}, this.getComponentsFromDate( null ), components
);
451 if ( components
.zone
) {
452 // Can't just use the constructor because that's stupid about ancient years.
453 date
.setFullYear( components
.year
, components
.month
- 1, components
.day
);
454 date
.setHours( components
.hour
, components
.minute
, components
.second
, components
.millisecond
);
456 // Date.UTC() is stupid about ancient years too.
457 date
.setUTCFullYear( components
.year
, components
.month
- 1, components
.day
);
458 date
.setUTCHours( components
.hour
, components
.minute
, components
.second
, components
.millisecond
);
467 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.adjustComponent = function ( date
, component
, delta
, mode
) {
468 var min
, max
, range
, components
;
470 if ( !( date
instanceof Date
) ) {
471 date
= this.defaultDate
;
473 components
= this.getComponentsFromDate( date
);
475 switch ( component
) {
486 max
= this.getDaysInMonth( components
.month
, components
.year
);
509 min
= components
.hour12period
? 12 : 0;
510 max
= components
.hour12period
? 23 : 11;
513 return new Date( date
.getTime() );
516 components
[ component
] += delta
;
517 range
= max
- min
+ 1;
520 // Date() will mostly handle it automatically. But months need
521 // manual handling to prevent e.g. Jan 31 => Mar 3.
522 if ( component
=== 'month' || component
=== 'year' ) {
523 while ( components
.month
< 1 ) {
524 components
[ component
] += 12;
527 while ( components
.month
> 12 ) {
528 components
[ component
] -= 12;
534 while ( components
[ component
] < min
) {
535 components
[ component
] += range
;
537 while ( components
[ component
] > max
) {
538 components
[ component
] -= range
;
542 if ( components
[ component
] < min
) {
543 components
[ component
] = min
;
545 if ( components
[ component
] < max
) {
546 components
[ component
] = max
;
550 if ( component
=== 'month' || component
=== 'year' ) {
551 components
.day
= Math
.min( components
.day
, this.getDaysInMonth( components
.month
, components
.year
) );
554 return this.getDateFromComponents( components
);
558 * Get the number of days in a month
561 * @param {number} month
562 * @param {number} year
565 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getDaysInMonth = function ( month
, year
) {
575 } else if ( year
% 100 ) {
578 return ( year
% 400 ) ? 28 : 29;
587 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getCalendarHeadings = function () {
588 var a
= this.dayLetters
;
590 if ( this.weekStartsOn
) {
591 return a
.slice( this.weekStartsOn
).concat( a
.slice( 0, this.weekStartsOn
) );
593 return a
.slice( 0 ); // clone
600 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.sameCalendarGrid = function ( date1
, date2
) {
602 return date1
.getFullYear() === date2
.getFullYear() && date1
.getMonth() === date2
.getMonth();
604 return date1
.getUTCFullYear() === date2
.getUTCFullYear() && date1
.getUTCMonth() === date2
.getUTCMonth();
611 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getCalendarData = function ( date
) {
612 var dt
, t
, d
, e
, i
, row
,
613 getDate
= this.local
? 'getDate' : 'getUTCDate',
614 setDate
= this.local
? 'setDate' : 'setUTCDate',
617 monthComponent
: 'month'
620 if ( !( date
instanceof Date
) ) {
621 date
= this.defaultDate
;
624 dt
= new Date( date
.getTime() );
629 ret
.header
= this.fullMonthNames
[ dt
.getMonth() + 1 ] + ' ' + dt
.getFullYear();
631 e
= this.getDaysInMonth( dt
.getMonth() + 1, dt
.getFullYear() );
633 ret
.header
= this.fullMonthNames
[ dt
.getUTCMonth() + 1 ] + ' ' + dt
.getUTCFullYear();
634 d
= dt
.getUTCDay() % 7;
635 e
= this.getDaysInMonth( dt
.getUTCMonth() + 1, dt
.getUTCFullYear() );
638 if ( this.weekStartsOn
) {
639 d
= ( d
+ 7 - this.weekStartsOn
) % 7;
646 for ( i
= 0; i
< 7; i
++, d
++ ) {
650 display
: String( dt
[ getDate
]() ),
652 extra
: d
< 1 ? 'prev' : d
> e
? 'next' : null
655 ret
.rows
.push( row
);
661 }( jQuery
, mediaWiki
) );