1/* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import {Timestamp} from 'common/time'; 18 19export class TimestampUtils { 20 // (?=.) checks there is at least one character with a lookahead match 21 private static readonly REAL_TIME_ONLY_REGEX = 22 /^(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])(\.[0-9]{1,9})?Z?$/; 23 private static readonly REAL_DATE_TIME_REGEX = 24 /^[0-9]{4}-((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])|(0[469]|11)-(0[1-9]|[12][0-9]|30)|(02)-(0[1-9]|[12][0-9])),\s(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])(\.[0-9]{1,9})?Z?$/; 25 private static readonly ISO_TIMESTAMP_REGEX = 26 /^[0-9]{4}-((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])|(0[469]|11)-(0[1-9]|[12][0-9]|30)|(02)-(0[1-9]|[12][0-9]))T(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])(\.[0-9]{1,9})?Z?$/; 27 private static readonly ELAPSED_TIME_REGEX = 28 /^(?=.)([0-9]+d)?([0-9]+h)?([0-9]+m)?([0-9]+s)?([0-9]+ms)?([0-9]+ns)?$/; 29 private static readonly NS_TIME_REGEX = /^\s*[0-9]+(\s?ns)?\s*$/; 30 31 static isNsFormat(timestampHuman: string): boolean { 32 return TimestampUtils.NS_TIME_REGEX.test(timestampHuman); 33 } 34 35 static isHumanElapsedTimeFormat(timestampHuman: string): boolean { 36 return TimestampUtils.ELAPSED_TIME_REGEX.test(timestampHuman); 37 } 38 39 static isRealTimeOnlyFormat(timestampHuman: string): boolean { 40 return TimestampUtils.REAL_TIME_ONLY_REGEX.test(timestampHuman); 41 } 42 43 static isRealDateTimeFormat(timestampHuman: string): boolean { 44 return TimestampUtils.REAL_DATE_TIME_REGEX.test(timestampHuman); 45 } 46 47 static isISOFormat(timestampHuman: string): boolean { 48 return TimestampUtils.ISO_TIMESTAMP_REGEX.test(timestampHuman); 49 } 50 51 static isHumanRealTimestampFormat(timestampHuman: string): boolean { 52 return ( 53 TimestampUtils.isISOFormat(timestampHuman) || 54 TimestampUtils.isRealDateTimeFormat(timestampHuman) || 55 TimestampUtils.isRealTimeOnlyFormat(timestampHuman) 56 ); 57 } 58 59 static extractDateFromHumanTimestamp( 60 timestampHuman: string, 61 ): string | undefined { 62 if ( 63 !TimestampUtils.isRealDateTimeFormat(timestampHuman) && 64 !TimestampUtils.isISOFormat(timestampHuman) 65 ) { 66 return undefined; 67 } 68 return timestampHuman.slice(0, 10); 69 } 70 71 static extractTimeFromHumanTimestamp( 72 timestampHuman: string, 73 ): string | undefined { 74 if (TimestampUtils.isRealDateTimeFormat(timestampHuman)) { 75 return timestampHuman.slice(12); 76 } 77 if (TimestampUtils.isISOFormat(timestampHuman)) { 78 return timestampHuman.slice(11); 79 } 80 if (TimestampUtils.isRealTimeOnlyFormat(timestampHuman)) { 81 return timestampHuman; 82 } 83 return undefined; 84 } 85 86 static compareFn(a: Timestamp, b: Timestamp): number { 87 return Number(a.getValueNs() - b.getValueNs()); 88 } 89 90 static min(ts1: Timestamp, ts2: Timestamp): Timestamp { 91 if (ts2.getValueNs() < ts1.getValueNs()) { 92 return ts2; 93 } 94 95 return ts1; 96 } 97 98 static max(ts1: Timestamp, ts2: Timestamp): Timestamp { 99 if (ts2.getValueNs() > ts1.getValueNs()) { 100 return ts2; 101 } 102 103 return ts1; 104 } 105} 106