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

Add a difference method to SC.DateTime #845

Closed
wants to merge 3 commits 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
37 changes: 37 additions & 0 deletions frameworks/datetime/frameworks/core/system/datetime.js
Expand Up @@ -1104,6 +1104,7 @@ SC.DateTime.mixin(SC.Comparable,
0 if a == b
*/
compare: function(a, b) {
if (SC.none(a) || SC.none(b)) throw "You must pass two valid dates to compare()";
var ma = a.get('milliseconds');
var mb = b.get('milliseconds');
return ma < mb ? -1 : ma === mb ? 0 : 1;
Expand All @@ -1123,12 +1124,48 @@ SC.DateTime.mixin(SC.Comparable,
don't have the same timezone
*/
compareDate: function(a, b) {
if (SC.none(a) || SC.none(b)) throw "You must pass two valid dates to compareDate()";
if (a.get('timezone') !== b.get('timezone')) throw SC.DATETIME_COMPAREDATE_TIMEZONE_ERROR;
var ma = a.adjust({hour: 0}).get('milliseconds');
var mb = b.adjust({hour: 0}).get('milliseconds');
return ma < mb ? -1 : ma === mb ? 0 : 1;
},

/**
This will tell you the interval of time between the two passed DateTime.
You can display the difference in week (w), day (d), hour (h), minute (M)
and second (S)

@param {SC.DateTime} a the first DateTime instance
@param {SC.DateTime} b the second DateTime instance
@param {String} format the interval to get the difference in
*/
difference: function(a, b, format) {
if (SC.none(a) || SC.none(b)) throw "You must pass two valid dates to difference()";
var ma = a.get('milliseconds'),
mb = b.get('milliseconds'),
diff = mb - ma,
divider;

switch(format) {
case 'd':
case 'D': divider = 864e5; break; // day: 1000 * 60 * 60 * 24
case 'h':
case 'H': divider = 36e5; break; // hour: 1000 * 60 * 60
case 'M': divider = 6e4; break; // minute: 1000 * 60
case 'S': divider = 1e3; break; // second: 1000
case 's': divider = 1; break;
case 'W': divider = 6048e5; break; // week: 1000 * 60 * 60 * 24 * 7
default: throw format+" is not supported"; break;
}

var ret = diff/divider;

return Math.round(ret);
}



});

/**
Expand Down
14 changes: 14 additions & 0 deletions frameworks/datetime/frameworks/core/tests/system/datetime.js
Expand Up @@ -402,3 +402,17 @@ test('extend', function() {
var parsedDateTimeExt = dateTimeExt.parse('2011-10-15T21:30:00Z');
ok(SC.instanceOf(parsedDateTimeExt, dateTimeExt), 'Correctly produced an instance of the extended type.');
});

test('difference', function() {
equals(SC.DateTime.difference(SC.DateTime.parse('2010-12-08', '%Y-%m-%d'), SC.DateTime.parse('2010-12-18', '%Y-%m-%d'), 'W'), 1);
equals(SC.DateTime.difference(SC.DateTime.parse('2010-01-01', '%Y-%m-%d'), SC.DateTime.parse('2010-02-01', '%Y-%m-%d'), 'W'), 4);
equals(SC.DateTime.difference(SC.DateTime.parse('2010-02-01', '%Y-%m-%d'), SC.DateTime.parse('2010-01-01', '%Y-%m-%d'), 'W'), -4);
equals(SC.DateTime.difference(SC.DateTime.parse('2010-12-08', '%Y-%m-%d'), SC.DateTime.parse('2010-12-09', '%Y-%m-%d'), 'd'), 1);
equals(SC.DateTime.difference(SC.DateTime.parse('2010-12-18', '%Y-%m-%d'), SC.DateTime.parse('2010-12-09', '%Y-%m-%d'), 'd'), -9);
equals(SC.DateTime.difference(SC.DateTime.parse('2010-12-09 02:00:00', '%Y-%m-%d %H:%M:%S'), SC.DateTime.parse('2010-12-09 01:00:00', '%Y-%m-%d %H:%M:%S'), 'H'), -1);
equals(SC.DateTime.difference(SC.DateTime.parse('2010-12-09 01:00:00', '%Y-%m-%d %H:%M:%S'), SC.DateTime.parse('2010-12-09 01:10:00', '%Y-%m-%d %H:%M:%S'), 'H'), 0);
equals(SC.DateTime.difference(SC.DateTime.parse('2010-12-09 01:00:00', '%Y-%m-%d %H:%M:%S'), SC.DateTime.parse('2010-12-09 01:50:00', '%Y-%m-%d %H:%M:%S'), 'H'), 1);
equals(SC.DateTime.difference(SC.DateTime.parse('2010-12-09 01:01:00', '%Y-%m-%d %H:%M:%S'), SC.DateTime.parse('2010-12-09 01:10:10', '%Y-%m-%d %H:%M:%S'), 'M'), 9);
equals(SC.DateTime.difference(SC.DateTime.parse('2010-12-09 01:00:00', '%Y-%m-%d %H:%M:%S'), SC.DateTime.parse('2010-12-09 01:00:20', '%Y-%m-%d %H:%M:%S'), 'S'), 20);
equals(SC.DateTime.difference(SC.DateTime.parse('2010-12-09 01:10:00', '%Y-%m-%d %H:%M:%S'), SC.DateTime.parse('2010-12-09 01:00:00', '%Y-%m-%d %H:%M:%S'), 'S'), -600);
});