• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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';
16
17export enum TimestampFormat {
18  Timecode = 'timecode',
19  Raw = 'raw',
20  RawLocale = 'rawLocale',
21  Seconds = 'seconds',
22  UTC = 'utc',
23  TraceTz = 'traceTz',
24}
25
26let timestampFormatCached: TimestampFormat | undefined;
27
28const TIMESTAMP_FORMAT_KEY = 'timestampFormat';
29const DEFAULT_TIMESTAMP_FORMAT = TimestampFormat.Timecode;
30
31export function timestampFormat(): TimestampFormat {
32  if (timestampFormatCached !== undefined) {
33    return timestampFormatCached;
34  } else {
35    const storedFormat = localStorage.getItem(TIMESTAMP_FORMAT_KEY);
36    if (storedFormat && isEnumValue(TimestampFormat, storedFormat)) {
37      timestampFormatCached = storedFormat;
38    } else {
39      timestampFormatCached = DEFAULT_TIMESTAMP_FORMAT;
40    }
41    return timestampFormatCached;
42  }
43}
44
45export function setTimestampFormat(format: TimestampFormat) {
46  timestampFormatCached = format;
47  localStorage.setItem(TIMESTAMP_FORMAT_KEY, format);
48}
49
50export enum DurationPrecision {
51  Full = 'full',
52  HumanReadable = 'human_readable',
53}
54
55let durationFormatCached: DurationPrecision | undefined;
56
57const DURATION_FORMAT_KEY = 'durationFormat';
58const DEFAULT_DURATION_FORMAT = DurationPrecision.Full;
59
60export function durationPrecision(): DurationPrecision {
61  if (durationFormatCached !== undefined) {
62    return durationFormatCached;
63  } else {
64    const storedFormat = localStorage.getItem(DURATION_FORMAT_KEY);
65    if (storedFormat && isEnumValue(DurationPrecision, storedFormat)) {
66      durationFormatCached = storedFormat;
67    } else {
68      durationFormatCached = DEFAULT_DURATION_FORMAT;
69    }
70    return durationFormatCached;
71  }
72}
73
74export function setDurationPrecision(format: DurationPrecision) {
75  durationFormatCached = format;
76  localStorage.setItem(DURATION_FORMAT_KEY, format);
77}
78