Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of a comparison method for dates #284

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/aria/utils/Date.js
Expand Up @@ -245,8 +245,8 @@ Aria.classDefinition({
*/
this._dayOrIATAMonth_format = "(d{1,2}|I|[a-z|A-Z]{3})";
/**
* Checks if the pattern is yyyy*MM*dd, * can be any character except alphanumeric the order of year, month and day
* can be reversed
* Checks if the pattern is yyyy*MM*dd, * can be any character except alphanumeric the order of year, month and
* day can be reversed
* @private
* @type {RegExp}
*/
Expand Down Expand Up @@ -821,7 +821,9 @@ Aria.classDefinition({

var dateBeforeMonth, entry, entrylen, dateOptions;
/* Code for Reference Date backward compatibility */
dateOptions = aria.utils.Type.isDate(options) ? {referenceDate : options} : options || {};
dateOptions = aria.utils.Type.isDate(options) ? {
referenceDate : options
} : options || {};
/* Code for Reference Date backward compatibility ends */

if (!entryStr) {
Expand Down Expand Up @@ -1757,6 +1759,20 @@ Aria.classDefinition({
var january1 = new Date(refDate.getFullYear(), 0, 1);

return Math.floor(Math.round((refTime - january1) / this.MS_IN_A_DAY) / 7) + 1;
},

/**
* Compare two dates
* @param {Date} firstDate
* @param {Date} secondDate
* @param {Boolean} time whether time should be taken into account for the comparison. It defaults to false
* @return {Number} -1 if firstDate < secondDate, 0 if firstDate == secondDate, 1 if firstDate > secondDate
*/
compare : function (firstDate, secondDate, time) {
var firstTime = time ? firstDate.getTime() : this.removeTime(firstDate).getTime();
var secondTime = time ? secondDate.getTime() : this.removeTime(secondDate).getTime();
var difference = firstTime - secondTime;
return (difference === 0) ? 0 : difference / Math.abs(difference);
}
}
});
70 changes: 70 additions & 0 deletions test/aria/utils/DateCompare.js
@@ -0,0 +1,70 @@
/*
* Copyright 2012 Amadeus s.a.s.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Test case for aria.utils.Date.compare method
*/
Aria.classDefinition({
$classpath : "test.aria.utils.DateCompare",
$extends : "aria.jsunit.TestCase",
$dependencies : ["aria.utils.Date"],
$prototype : {

/**
* compare dates by taking time into account
* @public
*/
testCompareWithTime : function () {

var dateUtil = aria.utils.Date;

var date1 = new Date(2010, 3, 1, 11, 33, 1);
var date2 = new Date(2010, 3, 1, 11, 33, 2);
var date3 = new Date(2010, 3, 2, 11, 33, 1);
var date4 = new Date(2010, 3, 1, 11, 33, 1);

this.assertEquals(dateUtil.compare(date1, date2, true), -1, "Wrong result for comparison with time");
this.assertEquals(dateUtil.compare(date3, date1, true), 1, "Wrong result for comparison with time");
this.assertEquals(dateUtil.compare(date4, date1, true), 0, "Wrong result for comparison with time when dates are equal");

},

/**
* compare dates by ignoring time
* @public
*/
testCompareWithoutTime : function () {

var dateUtil = aria.utils.Date;

var date1 = new Date(2010, 3, 1, 11, 33, 1);
var date2 = new Date(2010, 3, 1, 11, 33, 2);
var date3 = new Date(2010, 3, 2, 11, 33, 1);
var date4 = new Date(2010, 3, 1, 11, 33, 1);

this.assertEquals(dateUtil.compare(date1, date2, false), 0, "Wrong result for comparison without time");
this.assertEquals(dateUtil.compare(date3, date1, false), 1, "Wrong result for comparison without time");
this.assertEquals(dateUtil.compare(date1, date3, false), -1, "Wrong result for comparison without time");
this.assertEquals(dateUtil.compare(date4, date1, false), 0, "Wrong result for comparison without time when dates are equal");

// Check that the third argument defaults to false
this.assertEquals(dateUtil.compare(date1, date2), 0, "Wrong result for comparison without time");
this.assertEquals(dateUtil.compare(date3, date1), 1, "Wrong result for comparison without time");
this.assertEquals(dateUtil.compare(date1, date3), -1, "Wrong result for comparison without time");
this.assertEquals(dateUtil.compare(date4, date1), 0, "Wrong result for comparison without time when dates are equal");

}
}
});
1 change: 1 addition & 0 deletions test/aria/utils/UtilsTestSuite.js
Expand Up @@ -32,6 +32,7 @@ Aria.classDefinition({
this.addTests("test.aria.utils.Date");
this.addTests("test.aria.utils.DateInterpret");
this.addTests("test.aria.utils.DatePatternInterpret");
this.addTests("test.aria.utils.DateCompare");
this.addTests("test.aria.utils.Delegate");
this.addTests("test.aria.utils.Dom");
this.addTests("test.aria.utils.Ellipsis");
Expand Down