Skip to content

Commit

Permalink
Merge branch 'master' into nija-at/kinesis-eventsource-token
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] committed Jul 14, 2020
2 parents 7d184f9 + 6d9be1b commit 8e1a294
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/@aws-cdk/core/lib/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ export class Duration {
this.unit = unit;
}

/**
* Add two Durations together
*/
public plus(rhs: Duration): Duration {
const targetUnit = finestUnit(this.unit, rhs.unit);
const total = convert(this.amount, this.unit, targetUnit, {}) + convert(rhs.amount, rhs.unit, targetUnit, {});
return new Duration(total, targetUnit);
}

/**
* Return the total number of milliseconds in this Duration
*
Expand Down Expand Up @@ -285,3 +294,10 @@ function convert(amount: number, fromUnit: TimeUnit, toUnit: TimeUnit, { integra
}
return value;
}

/**
* Return the time unit with highest granularity
*/
function finestUnit(a: TimeUnit, b: TimeUnit) {
return a.inMillis < b.inMillis ? a : b;
}
7 changes: 7 additions & 0 deletions packages/@aws-cdk/core/test/test.duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ export = nodeunit.testCase({

test.done();
},

'add two durations'(test: nodeunit.Test) {
test.equal(Duration.minutes(1).plus(Duration.seconds(30)).toSeconds(), Duration.seconds(90).toSeconds());
test.equal(Duration.minutes(1).plus(Duration.seconds(30)).toMinutes({ integral: false }), Duration.seconds(90).toMinutes({ integral: false }));

test.done();
},
});

function floatEqual(test: nodeunit.Test, actual: number, expected: number) {
Expand Down

0 comments on commit 8e1a294

Please sign in to comment.