4 * Provides various methods needed for formatting dates and times. This
5 * implementation implments the [Discordian calendar][1], mainly for testing with
6 * something very different from the usual Gregorian calendar.
8 * Being intended mainly for testing, niceties like i18n and better
9 * configurability have been omitted.
11 * [1]: https://en.wikipedia.org/wiki/Discordian_calendar
14 * @extends mw.widgets.datetime.DateTimeFormatter
17 * @param {Object} [config] Configuration options
19 mw.widgets.datetime.DiscordianDateTimeFormatter = function MwWidgetsDatetimeDiscordianDateTimeFormatter( config ) {
20 config = $.extend( {}, config );
23 mw.widgets.datetime.DiscordianDateTimeFormatter[ 'super' ].call( this, config );
28 OO.inheritClass( mw.widgets.datetime.DiscordianDateTimeFormatter, mw.widgets.datetime.DateTimeFormatter );
35 mw.widgets.datetime.DiscordianDateTimeFormatter.static.formats = {
36 '@time': '${hour|0}:${minute|0}:${second|0}',
37 '@date': '$!{dow|full}${not-intercalary|1|, }${season|full}${not-intercalary|1| }${day|#}, ${year|#}',
38 '@datetime': '$!{dow|full}${not-intercalary|1|, }${season|full}${not-intercalary|1| }${day|#}, ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}',
39 '@default': '$!{dow|full}${not-intercalary|1|, }${season|full}${not-intercalary|1| }${day|#}, ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}'
47 * Additional fields implemented here are:
48 * - ${year|#}: Year as a number
49 * - ${season|#}: Season as a number
50 * - ${season|full}: Season as a string
51 * - ${day|#}: Day of the month as a number
52 * - ${day|0}: Day of the month as a number with leading 0
53 * - ${dow|full}: Day of the week as a string
54 * - ${hour|#}: Hour as a number
55 * - ${hour|0}: Hour as a number with leading 0
56 * - ${minute|#}: Minute as a number
57 * - ${minute|0}: Minute as a number with leading 0
58 * - ${second|#}: Second as a number
59 * - ${second|0}: Second as a number with leading 0
60 * - ${millisecond|#}: Millisecond as a number
61 * - ${millisecond|0}: Millisecond as a number, zero-padded to 3 digits
63 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.getFieldForTag = function ( tag, params ) {
66 switch ( tag + '|' + params[ 0 ] ) {
81 intercalarySize: { 1: 0 },
90 intercalarySize: { 1: 0 },
106 intercalarySize: { 1: 0 },
112 3: 'Prickle-Prickle',
124 intercalarySize: { 1: 13 },
125 zeropad: params[ 0 ] === '0',
126 formatValue: function ( v ) {
128 return 'St. Tib\'s Day';
130 return mw.widgets.datetime.DateTimeFormatter.prototype.formatSpecValue.call( this, v );
132 parseValue: function ( v ) {
133 if ( /^\s*(st.?\s*)?tib('?s)?(\s*day)?\s*$/i.test( v ) ) {
136 return mw.widgets.datetime.DateTimeFormatter.prototype.parseSpecValue.call( this, v );
148 component: tag.charAt( 0 ).toUpperCase() + tag.slice( 1 ),
151 zeropad: params[ 0 ] === '0'
155 case 'millisecond|#':
156 case 'millisecond|0':
158 component: 'Millisecond',
161 zeropad: params[ 0 ] === '0'
166 return mw.widgets.datetime.DiscordianDateTimeFormatter[ 'super' ].prototype.getFieldForTag.call( this, tag, params );
170 if ( spec.editable === undefined ) {
171 spec.editable = true;
173 if ( spec.component !== 'Day' ) {
174 spec.formatValue = this.formatSpecValue;
175 spec.parseValue = this.parseSpecValue;
178 spec.size = Math.max.apply(
179 null, $.map( spec.values, function ( v ) { return v.length; } )
188 * Get components from a Date object
192 * - Season {number} 1-5
193 * - Day {number|string} 1-73 or 'tib'
194 * - DOW {number} 0-4, or -1 on St. Tib's Day
195 * - Hour {number} 0-23
196 * - Minute {number} 0-59
197 * - Second {number} 0-59
198 * - Millisecond {number} 0-999
199 * - intercalary {string} '1' on St. Tib's Day
201 * @param {Date|null} date
202 * @return {Object} Components
204 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.getComponentsFromDate = function ( date ) {
206 monthDays = [ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 ];
208 if ( !( date instanceof Date ) ) {
209 date = this.defaultDate;
213 day = date.getDate();
214 month = date.getMonth();
216 Year: date.getFullYear() + 1166,
217 Hour: date.getHours(),
218 Minute: date.getMinutes(),
219 Second: date.getSeconds(),
220 Millisecond: date.getMilliseconds(),
221 zone: date.getTimezoneOffset()
224 day = date.getUTCDate();
225 month = date.getUTCMonth();
227 Year: date.getUTCFullYear() + 1166,
228 Hour: date.getUTCHours(),
229 Minute: date.getUTCMinutes(),
230 Second: date.getUTCSeconds(),
231 Millisecond: date.getUTCMilliseconds(),
236 if ( month === 1 && day === 29 ) {
240 ret.intercalary = '1';
242 day = monthDays[ month ] + day - 1;
243 ret.Season = Math.floor( day / 73 ) + 1;
244 ret.Day = ( day % 73 ) + 1;
246 ret.intercalary = '';
255 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.adjustComponent = function ( date, component, delta, mode ) {
256 return this.getDateFromComponents(
257 this.adjustComponentInternal(
258 this.getComponentsFromDate( date ), component, delta, mode
264 * Adjust the components directly
267 * @param {Object} components Modified in place
268 * @param {string} component
269 * @param {number} delta
270 * @param {string} mode
271 * @return {Object} components
273 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.adjustComponentInternal = function ( components, component, delta, mode ) {
274 var i, min, max, range, next, preTib, postTib, wasTib;
280 switch ( component ) {
292 if ( components.Day === 'tib' ) {
293 components.Day = 59; // Could choose either one...
294 components.Season = 1;
335 if ( component === 'Day' ) {
336 i = Math.abs( delta );
337 delta = delta < 0 ? -1 : 1;
338 preTib = delta > 0 ? 59 : 60;
339 postTib = delta > 0 ? 60 : 59;
341 if ( components.Day === preTib && components.Season === 1 && this.isLeapYear( components.Year ) ) {
342 components.Day = 'tib';
343 } else if ( components.Day === 'tib' ) {
344 components.Day = postTib;
345 components.Season = 1;
347 components.Day += delta;
348 if ( components.Day < min ) {
351 components.Day = max;
352 this.adjustComponentInternal( components, 'Season', -1, mode );
355 components.Day = max;
358 components.Day = min;
363 if ( components.Day > max ) {
366 components.Day = min;
367 this.adjustComponentInternal( components, 'Season', 1, mode );
370 components.Day = min;
373 components.Day = max;
381 if ( component === 'Week' ) {
385 if ( components.Day === 'tib' ) {
387 components.Season = 1;
391 if ( components.Day === 'tib' && ( component === 'Season' || component === 'Year' ) ) {
392 components.Day = 59; // Could choose either one...
397 i = Math.abs( delta );
398 delta = delta < 0 ? -1 : 1;
400 components[ component ] += delta;
401 if ( components[ component ] < min ) {
402 components[ component ] = max;
403 components = this.adjustComponentInternal( components, next, -1, mode );
405 if ( components[ component ] > max ) {
406 components[ component ] = min;
407 components = this.adjustComponentInternal( components, next, 1, mode );
410 if ( wasTib && components.Season === 1 && this.isLeapYear( components.Year ) ) {
411 components.Day = 'tib';
415 range = max - min + 1;
416 components[ component ] += delta;
417 while ( components[ component ] < min ) {
418 components[ component ] += range;
420 while ( components[ component ] > max ) {
421 components[ component ] -= range;
425 components[ component ] += delta;
426 if ( components[ component ] < min ) {
427 components[ component ] = min;
429 if ( components[ component ] > max ) {
430 components[ component ] = max;
434 if ( components.Day === 'tib' &&
435 ( components.Season !== 1 || !this.isLeapYear( components.Year ) )
437 components.Day = 59; // Could choose either one...
447 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.getDateFromComponents = function ( components ) {
448 var month, day, days,
450 monthDays = [ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 ];
452 components = $.extend( {}, this.getComponentsFromDate( null ), components );
453 if ( components.Day === 'tib' ) {
457 days = components.Season * 73 + components.Day - 74;
459 while ( days >= monthDays[ month + 1 ] ) {
462 day = days - monthDays[ month ] + 1;
465 if ( components.zone ) {
466 // Can't just use the constructor because that's stupid about ancient years.
467 date.setFullYear( components.Year - 1166, month, 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 - 1166, month, day );
472 date.setUTCHours( components.Hour, components.Minute, components.Second, components.Millisecond );
479 * Get whether the year is a leap year
482 * @param {number} year
485 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.isLeapYear = function ( year ) {
489 } else if ( year % 100 ) {
492 return ( year % 400 ) === 0;
498 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.getCalendarHeadings = function () {
499 return [ 'SM', 'BT', 'PD', 'PP', null, 'SO' ];
505 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.sameCalendarGrid = function ( date1, date2 ) {
506 var components1 = this.getComponentsFromDate( date1 ),
507 components2 = this.getComponentsFromDate( date2 );
509 return components1.Year === components2.Year && components1.Season === components2.Season;
515 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.getCalendarData = function ( date ) {
516 var dt, components, season, i, row,
519 weekComponent: 'Week',
520 monthComponent: 'Season'
522 seasons = [ 'Chaos', 'Discord', 'Confusion', 'Bureaucracy', 'The Aftermath' ],
523 seasonStart = [ 0, -3, -1, -4, -2 ];
525 if ( !( date instanceof Date ) ) {
526 date = this.defaultDate;
529 components = this.getComponentsFromDate( date );
531 season = components.Season;
533 ret.header = seasons[ season - 1 ] + ' ' + components.Year;
535 if ( seasonStart[ season - 1 ] ) {
536 this.adjustComponentInternal( components, 'Day', seasonStart[ season - 1 ], 'overflow' );
542 for ( i = 0; i < 6; i++ ) {
543 dt = this.getDateFromComponents( components );
545 display: components.Day === 'tib' ? 'Tib' : String( components.Day ),
547 extra: components.Season < season ? 'prev' : components.Season > season ? 'next' : null
550 this.adjustComponentInternal( components, 'Day', 1, 'overflow' );
551 if ( components.Day !== 'tib' && i === 3 ) {
556 ret.rows.push( row );
557 } while ( components.Season === season );
562 }( jQuery, mediaWiki ) );