1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 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 */ 15 16 package ohos.devtools.views.trace.util; 17 18 import java.text.DecimalFormat; 19 import java.util.ArrayList; 20 import java.util.List; 21 import java.util.concurrent.TimeUnit; 22 import java.util.stream.Collectors; 23 import java.util.stream.Stream; 24 25 /** 26 * Time formatting tool 27 * 28 * @since 2021/04/22 12:25 29 */ 30 public final class TimeUtils { 31 private static DecimalFormat df = new DecimalFormat("#.0"); 32 TimeUtils()33 private TimeUtils() { 34 } 35 36 /** 37 * Convert to s according to ns 38 * 39 * @param ns ns 40 * @return String 41 */ getSecondFromNSecond(final long ns)42 public static String getSecondFromNSecond(final long ns) { 43 final long second1 = 1_000_000_000L; // 1 second 44 final long millisecond1 = 1_000_000L; // 1 millisecond 45 final long microsecond1 = 1_000L; // 1 microsecond 46 final double nanosecond1 = 1000.0D; 47 String res; 48 if (ns >= second1) { 49 res = df.format(TimeUnit.MILLISECONDS.convert(ns, TimeUnit.NANOSECONDS) / nanosecond1) + "s"; 50 } else if (ns >= millisecond1) { 51 res = df.format(TimeUnit.MICROSECONDS.convert(ns, TimeUnit.NANOSECONDS) / nanosecond1) + "ms"; 52 } else if (ns >= microsecond1) { 53 res = df.format(ns / nanosecond1) + "μs"; 54 } else { 55 res = ns + "ns"; 56 } 57 return res; 58 } 59 60 /** 61 * Get the current formatting time 62 * 63 * @param nsL nsL 64 * @return String 65 */ getTimeString(final long nsL)66 public static String getTimeString(final long nsL) { 67 long ns = nsL; 68 long hours = TimeUnit.NANOSECONDS.toHours(ns); 69 ns = ns - TimeUnit.HOURS.toNanos(hours); 70 long minutes = TimeUnit.NANOSECONDS.toMinutes(ns); 71 ns = ns - TimeUnit.MINUTES.toNanos(minutes); 72 long second = TimeUnit.NANOSECONDS.toSeconds(ns); // second 73 ns = ns - TimeUnit.SECONDS.toNanos(second); 74 long millis = TimeUnit.NANOSECONDS.toMillis(ns); // millisecond 75 ns = ns - TimeUnit.MILLISECONDS.toNanos(millis); 76 long micros = TimeUnit.NANOSECONDS.toMicros(ns); // microsecond 77 ns = ns - TimeUnit.MICROSECONDS.toNanos(micros); 78 List<String> list = new ArrayList<>(); 79 if (hours > 0) { 80 list.add(hours + "h"); 81 } 82 if (minutes > 0) { 83 list.add(minutes + "m"); 84 } 85 if (second > 0) { 86 list.add(second + "s"); 87 } 88 if (millis > 0) { 89 list.add(millis + "ms"); 90 } 91 if (micros > 0) { 92 list.add(micros + "μs"); 93 } 94 long nanos = ns; 95 if (nanos > 0) { 96 list.add(nanos + "ns"); 97 } 98 return list.stream().collect(Collectors.joining(" ")); 99 } 100 101 /** 102 * Get the current formatting time 103 * 104 * @param ns ns 105 * @return String 106 */ getLogTimeString(long ns)107 public static String getLogTimeString(long ns) { 108 long second = TimeUnit.NANOSECONDS.toSeconds(ns); 109 long millis = TimeUnit.NANOSECONDS.toMillis(ns) - TimeUnit.SECONDS.toMillis(second); 110 long micros = TimeUnit.NANOSECONDS.toMicros(ns) - TimeUnit.SECONDS.toMicros(second) - TimeUnit.MILLISECONDS 111 .toMicros(millis); 112 long nanos = 113 TimeUnit.NANOSECONDS.toNanos(ns) - TimeUnit.SECONDS.toNanos(second) - TimeUnit.MILLISECONDS.toNanos(millis) 114 - TimeUnit.MICROSECONDS.toNanos(micros); 115 return Stream.of(second + "." + String.format("%03d", millis), String.format("%03d", micros), 116 String.format("%03d", nanos)).collect(Collectors.joining(" ")); 117 } 118 } 119