xref: /aosp_15_r20/external/perfetto/ui/src/core/timestamp_format.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2023 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {isEnumValue} from '../base/object_utils';
16import {DurationPrecision, TimestampFormat} from '../public/timeline';
17
18let timestampFormatCached: TimestampFormat | undefined;
19
20const TIMESTAMP_FORMAT_KEY = 'timestampFormat';
21const DEFAULT_TIMESTAMP_FORMAT = TimestampFormat.Timecode;
22
23export function timestampFormat(): TimestampFormat {
24  if (timestampFormatCached !== undefined) {
25    return timestampFormatCached;
26  } else {
27    const storedFormat = localStorage.getItem(TIMESTAMP_FORMAT_KEY);
28    if (storedFormat && isEnumValue(TimestampFormat, storedFormat)) {
29      timestampFormatCached = storedFormat;
30    } else {
31      timestampFormatCached = DEFAULT_TIMESTAMP_FORMAT;
32    }
33    return timestampFormatCached;
34  }
35}
36
37export function setTimestampFormat(format: TimestampFormat) {
38  timestampFormatCached = format;
39  localStorage.setItem(TIMESTAMP_FORMAT_KEY, format);
40}
41
42let durationFormatCached: DurationPrecision | undefined;
43
44const DURATION_FORMAT_KEY = 'durationFormat';
45const DEFAULT_DURATION_FORMAT = DurationPrecision.Full;
46
47export function durationPrecision(): DurationPrecision {
48  if (durationFormatCached !== undefined) {
49    return durationFormatCached;
50  } else {
51    const storedFormat = localStorage.getItem(DURATION_FORMAT_KEY);
52    if (storedFormat && isEnumValue(DurationPrecision, storedFormat)) {
53      durationFormatCached = storedFormat;
54    } else {
55      durationFormatCached = DEFAULT_DURATION_FORMAT;
56    }
57    return durationFormatCached;
58  }
59}
60
61export function setDurationPrecision(format: DurationPrecision) {
62  durationFormatCached = format;
63  localStorage.setItem(DURATION_FORMAT_KEY, format);
64}
65