Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Addition of a comparison method for dates #284

Closed
wants to merge 1 commit into from

2 participants

@flongo
Collaborator

No description provided.

@piuccio

Integrated in bc474b4

@piuccio piuccio closed this
@susant123 susant123 referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 14, 2012
  1. @flongo
This page is out of date. Refresh to see the latest.
View
22 src/aria/utils/Date.js
@@ -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}
*/
@@ -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) {
@@ -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);
}
}
});
View
70 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");
+
+ }
+ }
+});
View
1  test/aria/utils/UtilsTestSuite.js
@@ -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");
Something went wrong with that request. Please try again.