Skip to content

Commit

Permalink
feat: rounds expiry down to nearest minute
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock committed Oct 26, 2023
1 parent 11ffdbf commit 0f92a31
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/agent/src/agent/http/transforms.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Expiry } from './transforms';

jest.useFakeTimers();
test('it should round down to the nearest second', () => {
test('it should round down to the nearest minute', () => {
// 2021-04-26T17:47:11.314Z - high precision
jest.setSystemTime(new Date(1619459231314));

const expiry = new Expiry(5 * 60 * 1000);
expect(expiry['_value']).toEqual(BigInt(1619459471000000000));
expect(expiry['_value']).toEqual(BigInt(1619459460000000000));

const expiry_as_date_string = new Date(
Number(expiry['_value'] / BigInt(1_000_000)),
).toISOString();
expect(expiry_as_date_string).toBe('2021-04-26T17:51:11.000Z');
expect(expiry_as_date_string).toBe('2021-04-26T17:51:00.000Z');
});
7 changes: 6 additions & 1 deletion packages/agent/src/agent/http/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ export class Expiry {

// round down to the nearest second
const ingress_as_seconds = raw_value / BigInt(1_000_000_000);
const rounded_down_nanos = ingress_as_seconds * BigInt(1_000_000_000);

// round down to nearest minute
const ingress_as_minutes = ingress_as_seconds / BigInt(60);

const rounded_down_nanos = ingress_as_minutes * BigInt(60) * BigInt(1_000_000_000);

this._value = rounded_down_nanos;
}

Expand Down

0 comments on commit 0f92a31

Please sign in to comment.