Skip to content

Commit

Permalink
extract time utils
Browse files Browse the repository at this point in the history
  • Loading branch information
sadym-chromium committed Dec 9, 2024
1 parent 2e72c05 commit 20cef2f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/utils/time.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2024 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {expect} from 'chai';

import {getTimestamp} from './time.js';

describe('getTimestamp', () => {
it('should return a number', () => {
const timestamp = getTimestamp();
expect(timestamp).to.be.a('number');
});

it(`should return a timestamp between '2020-01-01 00:00:00' and '2100-01-01 00:00:00'`, () => {
const timestamp = getTimestamp();
// Using a large delta to account for execution time and clock drift.
// In a real test, consider mocking the underlying time source.
expect(timestamp).to.be.within(1577833200000, 4102441200000);
});
});
24 changes: 24 additions & 0 deletions src/utils/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright 2024 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export function getTimestamp(): number {
// `timestamp` from the event is MonotonicTime, not real time, so
// the best Mapper can do is to set the timestamp to the epoch time
// of the event arrived.
// https://chromedevtools.github.io/devtools-protocol/tot/Network/#type-MonotonicTime
return new Date().getTime();
}

0 comments on commit 20cef2f

Please sign in to comment.