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 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 ] ) {
287 calendarComponent
: true,
290 zeropad
: params
[ 0 ] === '0'
298 calendarComponent
: true,
300 values
: params
[ 0 ] === 'short' ? this.shortMonthNames
: this.fullMonthNames
308 calendarComponent
: true,
311 values
: params
[ 0 ] === 'short' ? this.shortDayNames
: this.fullDayNames
321 calendarComponent
: true,
324 zeropad
: params
[ 0 ] === '0'
336 calendarComponent
: false,
339 zeropad
: params
[ 0 ] === '0'
347 calendarComponent
: false,
350 zeropad
: params
[ 0 ] === '012'
356 component
: 'hour12period',
357 calendarComponent
: false,
359 values
: this.hour12Periods
363 case 'millisecond|#':
364 case 'millisecond|0':
366 component
: 'millisecond',
367 calendarComponent
: false,
370 zeropad
: params
[ 0 ] === '0'
375 return mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
[ 'super' ].prototype.getFieldForTag
.call( this, tag
, params
);
379 if ( spec
.editable
=== undefined ) {
380 spec
.editable
= true;
382 spec
.formatValue
= this.formatSpecValue
;
383 spec
.parseValue
= this.parseSpecValue
;
385 spec
.size
= Math
.max
.apply(
386 null, $.map( spec
.values
, function ( v
) { return v
.length
; } )
395 * Get components from a Date object
399 * - month {number} (1-12)
400 * - day {number} (1-31)
401 * - dow {number} (0-6, 0 is Sunday)
402 * - hour {number} (0-23)
403 * - hour12 {number} (1-12)
404 * - hour12period {boolean}
405 * - minute {number} (0-59)
406 * - second {number} (0-59)
407 * - millisecond {number} (0-999)
410 * @param {Date|null} date
411 * @return {Object} Components
413 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getComponentsFromDate = function ( date
) {
416 if ( !( date
instanceof Date
) ) {
417 date
= this.defaultDate
;
422 year
: date
.getFullYear(),
423 month
: date
.getMonth() + 1,
425 dow
: date
.getDay() % 7,
426 hour
: date
.getHours(),
427 minute
: date
.getMinutes(),
428 second
: date
.getSeconds(),
429 millisecond
: date
.getMilliseconds(),
430 zone
: date
.getTimezoneOffset()
434 year
: date
.getUTCFullYear(),
435 month
: date
.getUTCMonth() + 1,
436 day
: date
.getUTCDate(),
437 dow
: date
.getUTCDay() % 7,
438 hour
: date
.getUTCHours(),
439 minute
: date
.getUTCMinutes(),
440 second
: date
.getUTCSeconds(),
441 millisecond
: date
.getUTCMilliseconds(),
446 ret
.hour12period
= ret
.hour
>= 12 ? 1 : 0;
447 ret
.hour12
= ret
.hour
% 12;
448 if ( ret
.hour12
=== 0 ) {
458 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getDateFromComponents = function ( components
) {
459 var date
= new Date();
461 components
= $.extend( {}, components
);
462 if ( components
.hour
=== undefined && components
.hour12
!== undefined && components
.hour12period
!== undefined ) {
463 components
.hour
= ( components
.hour12
% 12 ) + ( components
.hour12period
? 12 : 0 );
465 components
= $.extend( {}, this.getComponentsFromDate( null ), components
);
467 if ( components
.zone
) {
468 // Can't just use the constructor because that's stupid about ancient years.
469 date
.setFullYear( components
.year
, components
.month
- 1, components
.day
);
470 date
.setHours( components
.hour
, components
.minute
, components
.second
, components
.millisecond
);
472 // Date.UTC() is stupid about ancient years too.
473 date
.setUTCFullYear( components
.year
, components
.month
- 1, components
.day
);
474 date
.setUTCHours( components
.hour
, components
.minute
, components
.second
, components
.millisecond
);
483 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.adjustComponent = function ( date
, component
, delta
, mode
) {
484 var min
, max
, range
, components
;
486 if ( !( date
instanceof Date
) ) {
487 date
= this.defaultDate
;
489 components
= this.getComponentsFromDate( date
);
491 switch ( component
) {
502 max
= this.getDaysInMonth( components
.month
, components
.year
);
525 min
= components
.hour12period
? 12 : 0;
526 max
= components
.hour12period
? 23 : 11;
529 return new Date( date
.getTime() );
532 components
[ component
] += delta
;
533 range
= max
- min
+ 1;
536 // Date() will mostly handle it automatically. But months need
537 // manual handling to prevent e.g. Jan 31 => Mar 3.
538 if ( component
=== 'month' || component
=== 'year' ) {
539 while ( components
.month
< 1 ) {
540 components
[ component
] += 12;
543 while ( components
.month
> 12 ) {
544 components
[ component
] -= 12;
550 while ( components
[ component
] < min
) {
551 components
[ component
] += range
;
553 while ( components
[ component
] > max
) {
554 components
[ component
] -= range
;
558 if ( components
[ component
] < min
) {
559 components
[ component
] = min
;
561 if ( components
[ component
] < max
) {
562 components
[ component
] = max
;
566 if ( component
=== 'month' || component
=== 'year' ) {
567 components
.day
= Math
.min( components
.day
, this.getDaysInMonth( components
.month
, components
.year
) );
570 return this.getDateFromComponents( components
);
574 * Get the number of days in a month
577 * @param {number} month
578 * @param {number} year
581 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getDaysInMonth = function ( month
, year
) {
591 } else if ( year
% 100 ) {
594 return ( year
% 400 ) ? 28 : 29;
603 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getCalendarHeadings = function () {
604 var a
= this.dayLetters
;
606 if ( this.weekStartsOn
) {
607 return a
.slice( this.weekStartsOn
).concat( a
.slice( 0, this.weekStartsOn
) );
609 return a
.slice( 0 ); // clone
616 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.sameCalendarGrid = function ( date1
, date2
) {
618 return date1
.getFullYear() === date2
.getFullYear() && date1
.getMonth() === date2
.getMonth();
620 return date1
.getUTCFullYear() === date2
.getUTCFullYear() && date1
.getUTCMonth() === date2
.getUTCMonth();
627 mw
.widgets
.datetime
.ProlepticGregorianDateTimeFormatter
.prototype.getCalendarData = function ( date
) {
628 var dt
, t
, d
, e
, i
, row
,
629 getDate
= this.local
? 'getDate' : 'getUTCDate',
630 setDate
= this.local
? 'setDate' : 'setUTCDate',
633 monthComponent
: 'month'
636 if ( !( date
instanceof Date
) ) {
637 date
= this.defaultDate
;
640 dt
= new Date( date
.getTime() );
645 ret
.header
= this.fullMonthNames
[ dt
.getMonth() + 1 ] + ' ' + dt
.getFullYear();
647 e
= this.getDaysInMonth( dt
.getMonth() + 1, dt
.getFullYear() );
649 ret
.header
= this.fullMonthNames
[ dt
.getUTCMonth() + 1 ] + ' ' + dt
.getUTCFullYear();
650 d
= dt
.getUTCDay() % 7;
651 e
= this.getDaysInMonth( dt
.getUTCMonth() + 1, dt
.getUTCFullYear() );
654 if ( this.weekStartsOn
) {
655 d
= ( d
+ 7 - this.weekStartsOn
) % 7;
662 for ( i
= 0; i
< 7; i
++, d
++ ) {
666 display
: String( dt
[ getDate
]() ),
668 extra
: d
< 1 ? 'prev' : d
> e
? 'next' : null
671 ret
.rows
.push( row
);
677 }( jQuery
, mediaWiki
) );