Skip to content

Commit

Permalink
Intermediate commit adding new functions, isBefore, isAfter, equals, …
Browse files Browse the repository at this point in the history
…schedule, and unschedule with unit tests and updated documentation
  • Loading branch information
kensnyder committed Jun 11, 2012
1 parent db528e5 commit 393a06f
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 15 deletions.
31 changes: 19 additions & 12 deletions reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -141,25 +141,30 @@ <h3 id="ref">Full Reference</h3>
<tr><th colspan="2" class="section">New Date Instance Methods</th></tr>
<tr><th>Method</th><th>Description</th></tr>
<tr><td class="signature">Date#add(iAmount[, sUnit='day']) -&gt; Date</td><td>Alters the date by adding an amount expressed in the given unit.</td></tr>
<tr><td class="signature">Date#strftime(sFormat) -&gt; String</td><td>Returns the date expressed in a string according to the given format.</td></tr>
<tr><td class="signature">Date#clone() -&gt; Date</td><td>Returns a date object with the same value.</td></tr>
<tr><td class="signature">Date#diff(date[, sUnit='day'][, bAllowDecimal=false]) -&gt; Number</td><td>Returns the amount difference between the given date in the given format. Round toward zero unless <em>bAllowDecimal</em> is true.</td></tr>
<tr><td class="signature">Date#diffText() -&gt; String</td><td>Returns a string with a conversational date string such as "5 days ago".</td></tr>
<tr><td class="signature">Date#clone() -&gt; Date</td><td>Returns a date object with the same value.</td></tr>
<tr><td class="signature">Date#succ([sUnit='day']) -&gt; Date</td><td>Returns a copy of the date incremented by one day (or any other unit).</td></tr>
<tr><td class="signature">Date#getShortYear() -&gt; Number</td><td>Returns number of years past the century (one or two digit year).</td></tr>
<tr><td class="signature">Date#getMonthNumber() -&gt; Number</td><td>Returns the number of the month 1-12.</td></tr>
<tr><td class="signature">Date#getMonthName() -&gt; String</td><td>Returns the name of the month as defined in Date.MONTHNAMES.</td></tr>
<tr><td class="signature">Date#equals() -&gt; Boolean</td><td>Date objects cannot be compared with an equals sign. To check for equality you can use diff() or equals(). Also using equals() allows you to pass a unit to which to round to.</td></tr>
<tr><td class="signature">Date#getAbbrDayName() -&gt; String</td><td>Returns the abbreviated name of the day of the week as defined in Date.ABBR_DAYNAMES.</td></tr>
<tr><td class="signature">Date#getAbbrMonthName() -&gt; String</td><td>Returns the abreviated name of the month as defined in Date.ABBR_MONTHNAMES.</td></tr>
<tr><td class="signature">Date#getAmPm() -&gt; String</td><td>Returns AM or PM for the date.</td></tr>
<tr><td class="signature">Date#getDayName() -&gt; String</td><td>Returns the name of the day of the week as defined in Date.DAYNAMES.</td></tr>
<tr><td class="signature">Date#getAbbrDayName() -&gt; String</td><td>Returns the abbreviated name of the day of the week as defined in Date.ABBR_DAYNAMES.</td></tr>
<tr><td class="signature">Date#getDayOrdinal() -&gt; String</td><td>Returns the ordinal suffix for the day of the month as defined in Date.ORDINALNAMES.</td></tr>
<tr><td class="signature">Date#getHours12() -&gt; String</td><td>Returns hours in 12-hour format.</td></tr>
<tr><td class="signature">Date#getAmPm() -&gt; String</td><td>Returns AM or PM for the date.</td></tr>
<tr><td class="signature">Date#getUnix() -&gt; Number</td><td>Returns the number of seconds past 1970-01-01-00:00:00.</td></tr>
<tr><td class="signature">Date#getGmtOffset() -&gt; String</td><td>Returns the minutes and seconds offset from GMT.</td></tr>
<tr><td class="signature">Date#getHours12() -&gt; String</td><td>Returns hours in 12-hour format.</td></tr>
<tr><td class="signature">Date#getMonthName() -&gt; String</td><td>Returns the name of the month as defined in Date.MONTHNAMES.</td></tr>
<tr><td class="signature">Date#getMonthNumber() -&gt; Number</td><td>Returns the number of the month 1-12.</td></tr>
<tr><td class="signature">Date#getShortYear() -&gt; Number</td><td>Returns number of years past the century (one or two digit year).</td></tr>
<tr><td class="signature">Date#getTimezoneName() -&gt; String</td><td>Returns the browser-reported name of the timezone.</td></tr>
<tr><td class="signature">Date#toYmdInt() -&gt; Number</td><td>Returns an integer in the format YYYYMMDD (useful for quickly sorting by date)</td></tr>
<tr><td class="signature">Date#getUnix() -&gt; Number</td><td>Returns the number of seconds past 1970-01-01-00:00:00.</td></tr>
<tr><td class="signature">Date#isAfter([sUnit='millisecond']) -&gt; Boolean</td><td>You can also compare Date objects with greater-than and less-than signs, but isAfter() allows you to specify the unit you want to round to. It rounds to the nearest unit.</td></tr>
<tr><td class="signature">Date#isBefore([sUnit='millisecond']) -&gt; Boolean</td><td>You can also compare Date objects with greater-than and less-than signs, but isBefore() allows you to specify the unit you want to round to. It rounds to the nearest unit.</td></tr>
<tr><td class="signature">Date#schedule(callback[, context]) -&gt; Date</td><td>Schedule a function to be executed at this time in the given context. If context is not given, default to the Date object. Use unschedule() to cancel.</td></tr>
<tr><td class="signature">Date#setUTCOffsetString(sOffset) -&gt; Date</td><td>Set the UTC offset with a four-digit string such as `+04:00` or `-0700`</td></tr>
<tr><td class="signature">Date#strftime(sFormat) -&gt; String</td><td>Returns the date expressed in a string according to the given format.</td></tr>
<tr><td class="signature">Date#succ([sUnit='day']) -&gt; Date</td><td>Returns a copy of the date incremented by one day (or any other unit).</td></tr>
<tr><td class="signature">Date#toYmdInt() -&gt; Number</td><td>Returns an integer in the format YYYYMMDD (useful for quickly sorting by date)</td></tr>
<tr><td class="signature">Date#unschedule(callback) -&gt; Date</td><td>Cancel the scheduled execution of a function registered with schedule().</td></tr>

<tr><th colspan="2" class="section">New Date Static Properties</th></tr>
<tr><th>Property</th><th>Description</th></tr>
Expand All @@ -177,6 +182,7 @@ <h3 id="ref">Full Reference</h3>
<tr><td class="signature">Date.addFormat(sName, oRules) -&gt; Object</td><td>Add a set of format codes and a method called sName to accept the rules (see <a href="#custom">customization</a>)</td></tr>
<tr><td class="signature">Date.current() -&gt; Date</td><td>Return a Date object representing the date at that moment</td></tr>
<tr><td class="signature">Date.create.patterns</td><td>An object with pairs defining parsers for custom formats (see <a href="#custom">customization</a>)</td></tr>

<tr><th colspan="2" class="section">Native Date Instance Methods (from <a href="http://www.w3schools.com/jsref/jsref_obj_date.asp">w3schools.com</a>)</th></tr>
<tr><th>Method</th><th>Description</th></tr>
<tr><td class="signature">Date#getDate() -&gt; Number</td><td>Returns the day of the month from a Date object (from 1-31)</td></tr>
Expand Down Expand Up @@ -220,10 +226,11 @@ <h3 id="ref">Full Reference</h3>
<tr><td class="signature">Date#toUTCString() -&gt; String</td><td>Converts a Date object, according to universal time, to a string</td></tr>
<tr><td class="signature">Date#toLocaleString() -&gt; String</td><td>Converts a Date object, according to local time, to a string</td></tr>
<tr><td class="signature">Date#valueOf() -&gt; Number</td><td>Returns the number of milliseconds since midnight Jan 1, 1970 (same as Date#getTime)</td></tr>
<tr><td colspan="2"><em>Note: the Number returned by native Date setters is the number of milliseconds since midnight Jan 1, 1970.</em></td></tr>
<tr><td colspan="2"><em>Note: the Number returned by native Date setters is the new number of milliseconds since midnight Jan 1, 1970.</em></td></tr>

<tr><th colspan="2" class="section">Native Date Static Methods (from <a href="http://www.w3schools.com/jsref/jsref_obj_date.asp">w3schools.com</a>)</th></tr>
<tr><th>Method</th><th>Description</th></tr>
<tr><td class="signature">Date.now() -&gt; Number</td><td>The number of milliseconds since midnight of January 1, 1970 according to UTC time.</td></tr>
<tr><td class="signature">Date.parse(sDate) -&gt; Number</td><td>Takes a date string and returns the number of milliseconds since midnight of January 1, 1970</td></tr>
<tr><td class="signature">Date.UTC(year,month,day,hours,minutes,seconds,ms) -&gt; Number</td><td>Takes a date and returns the number of milliseconds since midnight of January 1, 1970 according to universal time</td></tr>
</table>
Expand Down
43 changes: 41 additions & 2 deletions src/Date.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
}
}
//
//
//
var scheduled = [];
//
// set up integers and functions for adding to a date or subtracting two dates
//
var multipliers = {
Expand Down Expand Up @@ -462,6 +466,41 @@
*/
isLeapYear: function() {
return Date.daysInMonth(this.getFullYear(), 1) == 29 ? 1 : 0;
},
isBefore: function(date, units) {
return Math.round(this.diff(date, units || 'milliseconds', true), 0) < 0;
},
isAfter: function(date, units) {
return Math.round(this.diff(date, units || 'milliseconds', true), 0) > 0;
},
equals: function(date, units) {
return Math.round(this.diff(date, units || 'milliseconds', true), 0) == 0;
},
schedule: function(callback, context) {
var inMs = this.diff(Date.current(), 'milliseconds');
context = context || this;
if (inMs <= 0) {
callback.call(context);
return this;
}
var id = setTimeout(function() {
callback.call(context);
}, inMs);
scheduled.push([callback, this.getTime(), id]);
return this;
},
unschedule: function(callback) {
var i = 0;
var time = this.getTime();
while (i--) {
// iterate backwards so that if we splice out an item,
// we don't have to worry about array indexes changing
if (scheduled[i][0] == callback && scheduled[i][1] == time) {
clearTimeout(scheduled[i][2]);
scheduled = scheduled.splice(i, 1);
}
}
return this;
}
};
extend(Date.prototype, instanceMethods);
Expand Down Expand Up @@ -590,7 +629,7 @@
/**
* @var {Date} The date of the script load
*/
SCRIPT_LOAD: new Date,
SCRIPT_LOAD: new Date(),
/**
* Return the number of days in the given year and month. January = 1
*
Expand Down Expand Up @@ -691,7 +730,7 @@
* @return {Date}
*/
current: function() {
return new Date;
return new Date();
}
};
extend(Date, staticMethods);
Expand Down
54 changes: 53 additions & 1 deletion unit-tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ <h2 id="qunit-userAgent"></h2>
test('Date.create() - invalid strings return NaN', function() {
equal($D('bubba')+'', "NaN", '`bubba`');
equal($D('25 13')+'', "NaN", '`25 13`');
Date.create.debug = 1;
equal($D('1')+'', "NaN", '`1`');
});

Expand Down Expand Up @@ -354,6 +353,28 @@ <h2 id="qunit-userAgent"></h2>

equal($D('12-16-2006') < $D('2006-12-17'), true, 'comparing dates with <');
equal($D('12-16-2006') > $D('2006-12-17'), false, 'comparing dates with >');
});

test('comparison functions: equals, isBefore, isAfter', function() {
equal($D('2012-06-09 20:00:00').equals('06/09/2012 20:00:00'), true, 'equals');
equal($D('2012-06-09 20:00:00').equals('06/09/2012 20:00:01'), false, 'not equals');
equal($D('2012-06-09 20:00:00').equals('06/09/2012 20:00:00', 'millisecond'), true, 'equals to nearest millisecond 2');
equal($D('2012-06-09T20:00:00.123').equals('06/09/2012 20:00:00', 'second'), true, 'equals to nearest second');
equal($D('2012-06-09 20:00:29').equals('06/09/2012 20:00:00', 'minute'), true, 'equals to nearest minute');
equal($D('2012-06-09 19:30:00').equals('06/09/2012 20:00:00', 'hour'), true, 'equals to nearest hour');
equal($D('2012-06-09 21:29:29').equals('06/09/2012 20:00:00', 'day'), true, 'equals to nearest day');
equal($D('2012-06-09 20:00:01').equals('06/05/2012 20:00:01', 'month'), true, 'equals to nearest month');
equal($D('2012-06-09 20:00:00').equals('05/05/2012 20:00:00', 'year'), true, 'equals to nearest year');

equal($D('12-16-2006').isBefore('2006-12-17'), true, 'isBefore');
equal($D('12-16-2006').isBefore('2006-12-15'), false, 'not isBefore');







});

test('customize patterns', function() {
Expand All @@ -367,6 +388,37 @@ <h2 id="qunit-userAgent"></h2>
Date.addPattern(us);
equal(Date.create.patterns[0]+'', us+'', 'us pattern added to beginning');
});

test('schedule', 1, function() {
stop(300);
var callback1 = function() {
var diff = d.diff(new Date(), 'milliseconds');
// expect 250 ms, but give it a bit of wiggle room
equal(diff > 249 && diff < 299, true, 'scheduled for 250 milliseconds in the future (actually fired at ' + diff + 'ms)');
start();
};
var d = $D('+250 milliseconds');
d.schedule(callback1);
});

test('schedule in past', 1, function() {
var callback1 = function() {
ok(true, 'called immediately if scheduled in past');
};
var d = $D('-250 milliseconds');
d.schedule(callback1);
});

test('unschedule', 1, function() {
stop(400);
var callback1 = function() {
ok(false, 'oops. it was called even after unscheduling.');
};
var d = $D('+250 milliseconds');
d.schedule(callback1);
d.unschedule(callback1);
setTimeout(start, 350);
});

}
</script>
Expand Down

0 comments on commit 393a06f

Please sign in to comment.