// Duration Constructor
function Duration(duration) {
- var data = this._data = {},
- years = duration.years || duration.year || duration.y || 0,
+ var years = duration.years || duration.year || duration.y || 0,
months = duration.months || duration.month || duration.M || 0,
weeks = duration.weeks || duration.week || duration.w || 0,
days = duration.days || duration.day || duration.d || 0,
this._months = months +
years * 12;
- // The following code bubbles up values, see the tests for
- // examples of what that means.
- data.milliseconds = milliseconds % 1000;
- seconds += absRound(milliseconds / 1000);
+ this._data = {};
- data.seconds = seconds % 60;
- minutes += absRound(seconds / 60);
-
- data.minutes = minutes % 60;
- hours += absRound(minutes / 60);
-
- data.hours = hours % 24;
- days += absRound(hours / 24);
-
- days += weeks * 7;
- data.days = days % 30;
-
- months += absRound(days / 30);
-
- data.months = months % 12;
- years += absRound(months / 12);
-
- data.years = years;
+ this._bubble();
}
moment.duration.fn = Duration.prototype = {
+ _bubble : function () {
+ var milliseconds = this._milliseconds,
+ days = this._days,
+ months = this._months,
+ data = this._data,
+ seconds, minutes, hours, years;
+
+ // The following code bubbles up values, see the tests for
+ // examples of what that means.
+ data.milliseconds = milliseconds % 1000;
+
+ seconds = absRound(milliseconds / 1000);
+ data.seconds = seconds % 60;
+
+ minutes = absRound(seconds / 60);
+ data.minutes = minutes % 60;
+
+ hours = absRound(minutes / 60);
+ data.hours = hours % 24;
+
+ days += absRound(hours / 24);
+ data.days = days % 30;
+
+ months += absRound(days / 30);
+ data.months = months % 12;
+
+ years = absRound(months / 12);
+ data.years = years;
+ },
+
weeks : function () {
return absRound(this.days() / 7);
},
this._days += dur._days;
this._months += dur._months;
+ this._bubble();
+
return this;
},
this._days -= dur._days;
this._months -= dur._months;
+ this._bubble();
+
return this;
},
test.deepEqual(moment.duration(complicated), complicated, "complicated clones are equal");
test.done();
},
-
+
"instatiation from serialized C# TimeSpan zero" : function(test) {
test.expect(6);
test.equal(moment.duration("00:00:00").years(), 0, "0 years");
test.equal(moment.duration("00:00:00").milliseconds(), 0, "0 milliseconds");
test.done();
},
-
+
"instatiation from serialized C# TimeSpan with days" : function(test) {
test.expect(6);
test.equal(moment.duration("1.02:03:04.9999999").years(), 0, "0 years");
test.equal(moment.duration("1.02:03:04.9999999").milliseconds(), 999, "999 milliseconds");
test.done();
},
-
+
"instatiation from serialized C# TimeSpan without days" : function(test) {
test.expect(6);
test.equal(moment.duration("01:02:03.9999999").years(), 0, "0 years");
test.done();
},
+ "add and bubble" : function(test) {
+ test.expect(4);
+
+ test.equal(moment.duration(1, 'second').add(1000, 'milliseconds').seconds(), 2, 'Adding milliseconds should bubble up to seconds');
+ test.equal(moment.duration(1, 'minute').add(60, 'second').minutes(), 2, 'Adding seconds should bubble up to minutes');
+ test.equal(moment.duration(1, 'hour').add(60, 'minutes').hours(), 2, 'Adding minutes should bubble up to hours');
+ test.equal(moment.duration(1, 'day').add(24, 'hours').days(), 2, 'Adding hours should bubble up to days');
+
+ test.done();
+ },
+
+ "subtract and bubble" : function(test) {
+ test.expect(4);
+
+ test.equal(moment.duration(2, 'second').subtract(1000, 'milliseconds').seconds(), 1, 'Subtracting milliseconds should bubble up to seconds');
+ test.equal(moment.duration(2, 'minute').subtract(60, 'second').minutes(), 1, 'Subtracting seconds should bubble up to minutes');
+ test.equal(moment.duration(2, 'hour').subtract(60, 'minutes').hours(), 1, 'Subtracting minutes should bubble up to hours');
+ test.equal(moment.duration(2, 'day').subtract(24, 'hours').days(), 1, 'Subtracting hours should bubble up to days');
+
+ test.done();
+ },
+
"subtract" : function(test) {
test.expect(4);