Only use en-US locale in date conversion.
en-GB breaks some date conversions.
Keeping locale in timezone info, because we should at some point be able to move TimeUtils.format into Timestamp class, and format based on the ABT locale as well.
Currently we always format in en-US.
Bug: b/272430478
Test: npm run test:unit:ci
Change-Id: I0a314ed6ceb05ea5d08659444b718e3b95edbc7e
diff --git a/tools/winscope/src/common/time_utils.ts b/tools/winscope/src/common/time_utils.ts
index 5350c52..aef660a 100644
--- a/tools/winscope/src/common/time_utils.ts
+++ b/tools/winscope/src/common/time_utils.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import {Timestamp, TimestampType, TimezoneInfo} from 'common/time';
+import {Timestamp, TimestampType} from 'common/time';
import {NO_TIMEZONE_OFFSET_FACTORY} from './timestamp_factory';
export class TimeUtils {
@@ -133,10 +133,10 @@
return ts1;
}
- static addTimezoneOffset(timezoneInfo: TimezoneInfo, timestampNs: bigint): bigint {
+ static addTimezoneOffset(timezone: string, timestampNs: bigint): bigint {
const utcDate = new Date(Number(timestampNs / 1000000n));
- const timezoneDateFormatted = utcDate.toLocaleString(timezoneInfo.locale, {
- timeZone: timezoneInfo.timezone,
+ const timezoneDateFormatted = utcDate.toLocaleString('en-US', {
+ timeZone: timezone,
});
const timezoneDate = new Date(timezoneDateFormatted);
const hoursDiff = timezoneDate.getHours() - utcDate.getHours();
diff --git a/tools/winscope/src/common/time_utils_test.ts b/tools/winscope/src/common/time_utils_test.ts
index 38ea474..3f34054 100644
--- a/tools/winscope/src/common/time_utils_test.ts
+++ b/tools/winscope/src/common/time_utils_test.ts
@@ -341,19 +341,39 @@
expect(TimeUtils.format(timestamp)).toEqual('100ms0ns');
});
- it('addTimezoneOffset', () => {
- const timestampNs = 1000000000000n;
- expect(
- TimeUtils.addTimezoneOffset({timezone: 'Europe/London', locale: 'en-GB'}, timestampNs)
- ).toEqual(4600000000000n);
- expect(
- TimeUtils.addTimezoneOffset({timezone: 'Europe/Zurich', locale: 'en-US'}, timestampNs)
- ).toEqual(4600000000000n);
- expect(
- TimeUtils.addTimezoneOffset({timezone: 'America/Los_Angeles', locale: 'en-US'}, timestampNs)
- ).toEqual(58600000000000n);
- expect(
- TimeUtils.addTimezoneOffset({timezone: 'Asia/Kolkata', locale: 'en-US'}, timestampNs)
- ).toEqual(20800000000000n);
+ it('addTimezoneOffset for elapsed timestamps', () => {
+ const elapsedTimestampNs = 1000000000000n;
+ expect(TimeUtils.addTimezoneOffset('Europe/London', elapsedTimestampNs)).toEqual(
+ 4600000000000n
+ );
+ expect(TimeUtils.addTimezoneOffset('Europe/Zurich', elapsedTimestampNs)).toEqual(
+ 4600000000000n
+ );
+ expect(TimeUtils.addTimezoneOffset('America/Los_Angeles', elapsedTimestampNs)).toEqual(
+ 58600000000000n
+ );
+ expect(TimeUtils.addTimezoneOffset('Asia/Kolkata', elapsedTimestampNs)).toEqual(
+ 20800000000000n
+ );
+ });
+
+ it('addTimezoneOffset for real timestamps', () => {
+ const realTimestampNs = 1706094750112797658n;
+ expect(TimeUtils.addTimezoneOffset('Europe/London', realTimestampNs)).toEqual(
+ 1706094750112797658n
+ );
+ expect(TimeUtils.addTimezoneOffset('Europe/Zurich', realTimestampNs)).toEqual(
+ 1706098350112797658n
+ );
+ expect(TimeUtils.addTimezoneOffset('America/Los_Angeles', realTimestampNs)).toEqual(
+ 1706065950112797658n
+ );
+ expect(TimeUtils.addTimezoneOffset('Asia/Kolkata', realTimestampNs)).toEqual(
+ 1706114550112797658n
+ );
+ });
+
+ it('addTimezoneOffset throws for invalid timezone', () => {
+ expect(() => TimeUtils.addTimezoneOffset('Invalid/Timezone', 10n)).toThrow();
});
});
diff --git a/tools/winscope/src/common/timestamp_factory.ts b/tools/winscope/src/common/timestamp_factory.ts
index 00872a9..d89fcff 100644
--- a/tools/winscope/src/common/timestamp_factory.ts
+++ b/tools/winscope/src/common/timestamp_factory.ts
@@ -24,7 +24,7 @@
const valueWithRealtimeOffset = valueNs + (realToElapsedTimeOffsetNs ?? 0n);
const localNs =
this.timezoneInfo.timezone !== 'UTC'
- ? TimeUtils.addTimezoneOffset(this.timezoneInfo, valueWithRealtimeOffset)
+ ? TimeUtils.addTimezoneOffset(this.timezoneInfo.timezone, valueWithRealtimeOffset)
: valueWithRealtimeOffset;
return new Timestamp(TimestampType.REAL, localNs, localNs - valueWithRealtimeOffset);
}