jenkins-bot has submitted this change and it was merged.
Change subject: [time.js] Added time.settings setting for day vs. month first
and its support in parser
......................................................................
[time.js] Added time.settings setting for day vs. month first and its support
in parser
* settings.daybeforemonth which is set to true by default. This is a global
setting, just like all
the other settings so far.
* changed the parser's tests to use time.validTimeDefinitions and added a few
definitions.
TODOs (follow-up):
* In the MediaWiki context, the setting has to be set depending on the language
* Adding tests for the parser, considering this setting. For testing this
without changing the
global settings, a re-write of how the parser should be used might be in
order.
Change-Id: I8fe50f85ad057ddd90d9e59cb70d439924b48d5e
---
M DataValues/resources/time.js/src/time.Time.parse.js
M DataValues/resources/time.js/src/time.js
M DataValues/resources/time.js/tests/time.Time.parse.tests.js
M DataValues/resources/time.js/tests/time.validTimeDefinitions.js
M DataValues/resources/values/TimeValue.js
5 files changed, 74 insertions(+), 61 deletions(-)
Approvals:
Henning Snater: Looks good to me, approved
jenkins-bot: Verified
diff --git a/DataValues/resources/time.js/src/time.Time.parse.js
b/DataValues/resources/time.js/src/time.Time.parse.js
index 4b86965..c630d6c 100644
--- a/DataValues/resources/time.js/src/time.Time.parse.js
+++ b/DataValues/resources/time.js/src/time.Time.parse.js
@@ -22,11 +22,8 @@
function parse( text ) {
var tokens = tokenize( text ),
retval = {},
- result = matchGrammars( [
- 'y', '-y', 'my', 'm-y', 'yb', 'myb', 'mdy',
'md-y', 'dmy', 'dm-y',
- 'mdyb', 'dmyb', 'mdyc', ',md-yc', 'dmyc',
'dm-yc',
- 'mdybc', 'dmybc', 'ymd', '-ymd', 'ym', '-ym'
- ], tokens );
+ grammars = getGrammars( settings.daybeforemonth ),
+ result = matchGrammars( grammars, tokens );
if( result === null ) {
return null;
@@ -77,10 +74,10 @@
if( result.calendar !== undefined ) {
retval.calendarname = result.calendar;
- } else if( (result.year < 1583) && (retval.precision > 10) ) {
- retval.calendarname = 'Julian';
+ } else if( ( result.year < 1583 ) && ( retval.precision > 10 )
) {
+ retval.calendarname = time.Time.CALENDAR.JULIAN;
} else {
- retval.calendarname = 'Gregorian';
+ retval.calendarname = time.Time.CALENDAR.GREGORIAN;
}
delete( retval.bce ); // nothing we want to expose since this
is redundant with "year"
@@ -88,6 +85,32 @@
return retval;
}
+ /**
+ * Returns an array of grammars which should be used.
+ *
+ * @param {boolean} daybeforemonth Whether the day is usually written
before the month.
+ * @return {string[]}
+ */
+ function getGrammars( daybeforemonth ) {
+ var grammars = [
+ 'y', '-y', 'my', 'm-y', 'yb', 'myb', 'mdy', 'md-y',
'dmy', 'dm-y',
+ 'mdyb', 'dmyb', 'mdyc', ',md-yc', 'dmyc', 'dm-yc',
+ 'mdybc', 'dmybc'
+ ];
+
+ // If the language prefers the day before the month, we have to
switch the above grammar
+ // priorities (switch the "md" with the "dm" versions of the
equivalent grammar).
+ if( daybeforemonth ) {
+ for( var i in grammars ) {
+ grammars[i] = grammars[i]
+ .replace( 'md', '@@' )
+ .replace( 'dm', 'md' )
+ .replace( '@@', 'dm' );
+ }
+ }
+ return grammars.concat( [ 'ymd', '-ymd', 'ym', '-ym' ] );
+ }
+
function matchGrammars( grammars, tokens ) {
var result = null;
for( var i = 0; i < grammars.length; i++ ) {
diff --git a/DataValues/resources/time.js/src/time.js
b/DataValues/resources/time.js/src/time.js
index caaaf19..116d593 100644
--- a/DataValues/resources/time.js/src/time.js
+++ b/DataValues/resources/time.js/src/time.js
@@ -23,6 +23,7 @@
return time;
};
+ // TODO: get rid of global settings, inject them where required
var settings = {};
settings.bce = ['BCE', 'BC', 'B.C.', 'before Common Era', 'before
Christ'];
@@ -36,6 +37,7 @@
settings.calendarnames[1] = [
'Julian', 'J', 'JD', 'JC', 'OS', 'O.S.', 'Old Style', 'Julian
calendar', 'Julian date'
];
+ settings.daybeforemonth = true;
settings.monthnames = [];
settings.monthnames[0] = ['January', 'Jan'];
diff --git a/DataValues/resources/time.js/tests/time.Time.parse.tests.js
b/DataValues/resources/time.js/tests/time.Time.parse.tests.js
index cde934e..c2ec1fd 100644
--- a/DataValues/resources/time.js/tests/time.Time.parse.tests.js
+++ b/DataValues/resources/time.js/tests/time.Time.parse.tests.js
@@ -6,7 +6,7 @@
* @licence GNU GPL v2+
* @author Daniel Werner
*/
-( function( QUnit, $, Time ) {
+( function( QUnit, $, Time, validTimeDefinitions ) {
'use strict';
var PRECISION = Time.PRECISION,
@@ -15,56 +15,10 @@
QUnit.module( 'time.js: time.Time.parse()' );
- var times = {
- '45 BC': {
- calendarname: G,
- year: -44,
- precision: PRECISION.YEAR
- },
- '12 October 1492': {
- calendarname: J,
- year: 1492,
- month: 10,
- day: 12,
- precision: PRECISION.DAY
- },
- 'March 45 BC': {
- calendarname: G,
- month: 3,
- year: -44,
- precision: PRECISION.MONTH
- },
- 'April 23, 1616 Old Style': {
- calendarname: J,
- year: 1616,
- month: 4,
- day: 23,
- precision: PRECISION.DAY
- },
- '22.4.1616 Gregorian': {
- calendarname: G,
- year: 1616,
- month: 4,
- day: 22,
- precision: PRECISION.DAY
- },
- '2001-01-01': {
- calendarname: G,
- year: 2001,
- month: 1,
- day: 1,
- precision: PRECISION.DAY
- },
- 'November 20, 1989': {
- calendarname: G,
- year: 1989,
- month: 11,
- day: 20,
- precision: PRECISION.DAY
- },
+ var times = $.extend( {}, validTimeDefinitions, {
'foo': null, // TODO: in error case, the parser should throw an
error, not just return null!
'42 abc': null
- };
+ } );
QUnit.test( 'random parsing', function( assert ) {
$.each( times, function( timeInput, exptectedTimeDefinition ) {
@@ -89,4 +43,4 @@
} );
} );
-}( QUnit, jQuery, time.Time ) );
+}( QUnit, jQuery, time.Time, time.validTimeDefinitions ) );
diff --git a/DataValues/resources/time.js/tests/time.validTimeDefinitions.js
b/DataValues/resources/time.js/tests/time.validTimeDefinitions.js
index cf7c2b0..bf3e0e2 100644
--- a/DataValues/resources/time.js/tests/time.validTimeDefinitions.js
+++ b/DataValues/resources/time.js/tests/time.validTimeDefinitions.js
@@ -2,6 +2,7 @@
* Object holding example time definitions indexed by a string describing it.
The string describing
* it is basically a string that could be fed to the time parser, the parsed
result should be a
* time definition equal to the ones given here. This is particularly useful
for testing purposes.
+ * The definition assumes that the settings "daybeforemonth" is set to true.
*
* @since 0.1
* @file
@@ -51,11 +52,11 @@
day: 22,
precision: PRECISION.DAY
},
- '2001-01-01': {
+ '2001-01-02': {
calendarname: G,
year: 2001,
month: 1,
- day: 1,
+ day: 2,
precision: PRECISION.DAY
},
'November 20, 1989': {
@@ -64,6 +65,32 @@
month: 11,
day: 20,
precision: PRECISION.DAY
+ },
+ '1.2.3': {
+ calendarname: J,
+ year: 3,
+ month: 2,
+ day: 1,
+ precision: PRECISION.DAY
+ },
+ '15.2.3': {
+ calendarname: J,
+ year: 3,
+ month: 2,
+ day: 15,
+ precision: PRECISION.DAY
+ },
+ '111-10': {
+ calendarname: G,
+ year: 111,
+ month: 10,
+ precision: PRECISION.MONTH
+ },
+ '-2003-10': {
+ calendarname: G,
+ year: -2003,
+ month: 10,
+ precision: PRECISION.MONTH
}
};
diff --git a/DataValues/resources/values/TimeValue.js
b/DataValues/resources/values/TimeValue.js
index d96ecef..3436ee5 100644
--- a/DataValues/resources/values/TimeValue.js
+++ b/DataValues/resources/values/TimeValue.js
@@ -62,7 +62,14 @@
if ( !( value instanceof SELF ) ) {
return false;
}
- return this.getValue().equals( value.getValue() );
+
+ var ownTime = this.getValue(),
+ otherTime = value.getValue();
+
+ // no need to check for isValid() since constructor
won't allow invalid Time values
+
+ return ownTime.precision() === otherTime.precision()
+ && ownTime.iso8601() === otherTime.iso8601();
},
/**
--
To view, visit https://gerrit.wikimedia.org/r/64115
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I8fe50f85ad057ddd90d9e59cb70d439924b48d5e
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/DataValues
Gerrit-Branch: master
Gerrit-Owner: Daniel Werner <[email protected]>
Gerrit-Reviewer: Henning Snater <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits