Skip to content

Commit

Permalink
Add a difference method to SC.DateTime
Browse files Browse the repository at this point in the history
This method allow to compute the number of weeks, days, hours, minutes,
or seconds between two dates.
  • Loading branch information
Nicolas BADIA authored and Nicolas BADIA committed Oct 5, 2012
1 parent 066ae08 commit 2f02e5f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
34 changes: 34 additions & 0 deletions frameworks/datetime/frameworks/core/system/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1127,8 +1127,42 @@ SC.DateTime.mixin(SC.Comparable,
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) {
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 ret>0?Math.floor(ret):Math.ceil(ret);
}



});

/**
Expand Down
14 changes: 14 additions & 0 deletions frameworks/datetime/frameworks/core/tests/system/datetime.js
Original file line number Diff line number Diff line change
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'), 0);
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);
});

3 comments on commit 2f02e5f

@dcporter
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1! This would have been extremely useful on a couple of projects I worked on when a total newbie.

This code would throw an error if null values are passed in. Thoughts on some gatekeeping code? Opens up a (smallish) can of worms, might not be worth it.

@nicolasbadia
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make this method throw an error if a or b are null, but in this case, I should also do it for the compare and compareDate methods.

Not sure to understand what you mean about gatekeeping code :(

@dcporter
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You got it - gatekeeping code is fancy words for code that quits the function early if all the ducks aren't in a row. My instinct is to return null when the arguments don't make sense (i.e. a or b is SC.none()) but I bet it would be easier for developers to debug if it threw a well-described error. Thoughts?

Please sign in to comment.