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.applicationtrace.util; 17 18 import java.util.ArrayList; 19 import java.util.List; 20 import java.util.Locale; 21 import java.util.concurrent.TimeUnit; 22 import java.util.stream.Collectors; 23 24 /** 25 * TimeUtils 26 * 27 * @since 2021/5/13 13:06 28 */ 29 public class TimeUtils { 30 /** 31 * getTimeWithUnit function 32 * 33 * @param nsL nsL 34 * @return String time 35 */ getTimeWithUnit(final long nsL)36 public static String getTimeWithUnit(final long nsL) { 37 long ns = nsL; 38 StringBuilder time = new StringBuilder(); 39 long hours = TimeUnit.NANOSECONDS.toHours(ns); 40 if (hours > 1) { 41 time.append(hours).append("h"); 42 return time.toString(); 43 } 44 long minute = TimeUnit.NANOSECONDS.toMinutes(ns); 45 long second; 46 if (minute > 0) { 47 ns = ns - TimeUnit.MINUTES.toNanos(minute); 48 second = Math.round(TimeUnit.NANOSECONDS.toSeconds(ns) / 6D * 10); 49 time.append(minute).append(".").append(String.format(Locale.ENGLISH, "%02d", second)).append("m"); 50 return time.toString(); 51 } 52 long millis; 53 second = TimeUnit.NANOSECONDS.toSeconds(ns); 54 if (second > 0) { 55 ns = ns - TimeUnit.SECONDS.toNanos(second); 56 long round = Math.round(TimeUnit.NANOSECONDS.toMillis(ns) / 10D); 57 millis = 58 round >= 100L ? Double.valueOf(Math.floor(TimeUnit.NANOSECONDS.toMillis(ns) / 10D)).longValue() : round; 59 time.append(second).append(".").append(String.format(Locale.ENGLISH, "%02d", millis)).append("s"); 60 return time.toString(); 61 } 62 long micros; 63 millis = TimeUnit.NANOSECONDS.toMillis(ns); 64 if (millis > 0) { 65 ns = ns - TimeUnit.MILLISECONDS.toNanos(millis); 66 long round = Math.round(TimeUnit.NANOSECONDS.toMicros(ns) / 10D); 67 micros = 68 round >= 100L ? Double.valueOf(Math.floor(TimeUnit.NANOSECONDS.toMicros(ns) / 10D)).longValue() : round; 69 time.append(millis).append(".").append(String.format(Locale.ENGLISH, "%02d", micros)).append("ms"); 70 return time.toString(); 71 } 72 micros = TimeUnit.NANOSECONDS.toMicros(ns); 73 if (micros > 0) { 74 ns = ns - TimeUnit.MICROSECONDS.toNanos(micros); 75 long nanos = Math.round(ns / 1000D); 76 if (nanos > 0) { 77 micros++; 78 } 79 time.append(micros).append("μs"); 80 return time.toString(); 81 } 82 return "0μs"; 83 } 84 85 /** 86 * get the time range is in other range function 87 * 88 * @param startNs startNs 89 * @param endNs endNs 90 * @param startRNs startRNs 91 * @param endLNs endLNs 92 * @return String time 93 */ isInRange(long startNs, long endNs, long startRNs, long endLNs)94 public static boolean isInRange(long startNs, long endNs, long startRNs, long endLNs) { 95 return endLNs >= startNs && startRNs <= endNs; 96 } 97 98 /** 99 * get the time range is cross function 100 * 101 * @param startNs startNs 102 * @param endNs endNs 103 * @param startRNs startRNs 104 * @param endLNs endLNs 105 * @return String time 106 */ isRangeCross(long startNs, long endNs, long startRNs, long endLNs)107 public static boolean isRangeCross(long startNs, long endNs, long startRNs, long endLNs) { 108 long leftMin = Math.max(startNs, startRNs); 109 long rightMax = Math.min(endNs, endLNs); 110 return rightMax > leftMin; 111 } 112 113 /** 114 * get the time string format function 115 * 116 * @param nsL nsL 117 * @return String time 118 */ getTimeFormatString(final long nsL)119 public static String getTimeFormatString(final long nsL) { 120 long ns = nsL; 121 long hours = TimeUnit.NANOSECONDS.toHours(ns); 122 ns = ns - TimeUnit.HOURS.toNanos(hours); 123 long minute = TimeUnit.NANOSECONDS.toMinutes(ns); 124 ns = ns - TimeUnit.MINUTES.toNanos(minute); 125 long second = TimeUnit.NANOSECONDS.toSeconds(ns); // second 126 ns = ns - TimeUnit.SECONDS.toNanos(second); 127 long millis = TimeUnit.NANOSECONDS.toMillis(ns); 128 StringBuffer time = new StringBuffer(); 129 time.append(String.format(Locale.ENGLISH, "%02d", minute)); 130 time.append(":"); 131 time.append(String.format(Locale.ENGLISH, "%02d", second)); 132 time.append("."); 133 time.append(String.format(Locale.ENGLISH, "%03d", millis)); 134 return time.toString(); 135 } 136 137 /** 138 * get Intersection function 139 * 140 * @param fStartNs fStartNs 141 * @param fEndNs fEndNs 142 * @param sStartNs sStartNs 143 * @param sEndns sEndns 144 * @return long time 145 */ getIntersection(long fStartNs, long fEndNs, long sStartNs, long sEndns)146 public static long getIntersection(long fStartNs, long fEndNs, long sStartNs, long sEndns) { 147 long leftMin = Math.max(fStartNs, sStartNs); 148 long rightMax = Math.min(fEndNs, sEndns); 149 if (rightMax > leftMin) { 150 return TimeUnit.NANOSECONDS.toMicros(rightMax) - TimeUnit.NANOSECONDS.toMicros(leftMin); 151 } 152 return 0L; 153 } 154 155 /** 156 * get Intersection unit nanos function 157 * 158 * @param fStartNs fStartNs 159 * @param fEndNs fEndNs 160 * @param sStartNs sStartNs 161 * @param sEndns sEndns 162 * @return long time 163 */ getNanoIntersection(long fStartNs, long fEndNs, long sStartNs, long sEndns)164 public static long getNanoIntersection(long fStartNs, long fEndNs, long sStartNs, long sEndns) { 165 long leftMin = Math.max(fStartNs, sStartNs); 166 long rightMax = Math.min(fEndNs, sEndns); 167 if (rightMax > leftMin) { 168 return rightMax - leftMin; 169 } 170 return 0L; 171 } 172 173 /** 174 * Get the current formatting time 175 * 176 * @param nsL nsL 177 * @return String 178 */ getTimeString(final long nsL)179 public static String getTimeString(final long nsL) { 180 long ns = nsL; 181 long hours = TimeUnit.NANOSECONDS.toHours(ns); 182 ns = ns - TimeUnit.HOURS.toNanos(hours); 183 long minutes = TimeUnit.NANOSECONDS.toMinutes(ns); 184 ns = ns - TimeUnit.MINUTES.toNanos(minutes); 185 long second = TimeUnit.NANOSECONDS.toSeconds(ns); // second 186 ns = ns - TimeUnit.SECONDS.toNanos(second); 187 long millis = TimeUnit.NANOSECONDS.toMillis(ns); // millisecond 188 ns = ns - TimeUnit.MILLISECONDS.toNanos(millis); 189 long micros = TimeUnit.NANOSECONDS.toMicros(ns); // microsecond 190 ns = ns - TimeUnit.MICROSECONDS.toNanos(micros); 191 List<String> list = new ArrayList<>(); 192 if (hours > 0) { 193 list.add(hours + "h"); 194 } 195 if (minutes > 0) { 196 list.add(minutes + "m"); 197 } 198 if (second > 0) { 199 list.add(second + "s"); 200 } 201 if (millis > 0) { 202 list.add(millis + "ms"); 203 } 204 if (micros > 0) { 205 list.add(micros + "μs"); 206 } 207 long nanos = ns; 208 if (nanos > 0) { 209 list.add(nanos + "ns"); 210 } 211 return list.stream().collect(Collectors.joining(" ")); 212 } 213 214 /** 215 * get time ns by time string 216 * 217 * @param timeStr time string 218 * @return ns 219 */ getNSByTimeString(final String timeStr)220 public static long getNSByTimeString(final String timeStr) { 221 try { 222 if (timeStr.contains("ns")) { 223 String timeString = timeStr.replaceAll("ns", ""); 224 double timeDouble = Double.parseDouble(timeString); 225 return (long) (timeDouble * 1); 226 } else if (timeStr.contains("μs")) { 227 String timeString = timeStr.replaceAll("μs", ""); 228 double timeDouble = Double.parseDouble(timeString); 229 return (long) (timeDouble * 1000); 230 } else if (timeStr.contains("ms")) { 231 String timeString = timeStr.replaceAll("ms", ""); 232 double timeDouble = Double.parseDouble(timeString); 233 return (long) (timeDouble * 1000 * 1000); 234 } else if (timeStr.contains("s")) { 235 String timeString = timeStr.replaceAll("s", ""); 236 double timeDouble = Double.parseDouble(timeString); 237 return (long) (timeDouble * 1000 * 1000 * 1000); 238 } else if (timeStr.contains("m")) { 239 String timeString = timeStr.replaceAll("m", ""); 240 double timeDouble = Double.parseDouble(timeString); 241 return (long) (timeDouble * 60 * 1000 * 1000 * 1000); 242 } else if (timeStr.contains("h")) { 243 String timeString = timeStr.replaceAll("h", ""); 244 double timeDouble = Double.parseDouble(timeString); 245 return (long) (timeDouble * 3600 * 1000 * 1000 * 1000); 246 } else { 247 return 0; 248 } 249 } catch (NumberFormatException exception) { 250 exception.printStackTrace(); 251 return 0; 252 } 253 } 254 255 /** 256 * Get the current formatting time 257 * 258 * @param nsL nsL 259 * @return String 260 */ getDistributedTotalTime(final long nsL)261 public static String getDistributedTotalTime(final long nsL) { 262 long ns = nsL; 263 long hours = TimeUnit.NANOSECONDS.toHours(ns); 264 ns = ns - TimeUnit.HOURS.toNanos(hours); 265 long minutes = TimeUnit.NANOSECONDS.toMinutes(ns); 266 ns = ns - TimeUnit.MINUTES.toNanos(minutes); 267 long second = TimeUnit.NANOSECONDS.toSeconds(ns); // second 268 ns = ns - TimeUnit.SECONDS.toNanos(second); 269 long millis = TimeUnit.NANOSECONDS.toMillis(ns); // millisecond 270 ns = ns - TimeUnit.MILLISECONDS.toNanos(millis); 271 long micros = TimeUnit.NANOSECONDS.toMicros(ns); // microsecond 272 ns = ns - TimeUnit.MICROSECONDS.toNanos(micros); 273 List<String> list = new ArrayList<>(); 274 if (hours > 0) { 275 list.add(hours + ""); 276 } else { 277 list.add("00"); 278 } 279 if (minutes > 0) { 280 list.add(minutes + ""); 281 } else { 282 list.add("00"); 283 } 284 if (second > 0) { 285 list.add(second + ""); 286 } else { 287 list.add("00"); 288 } 289 return list.stream().collect(Collectors.joining(":")) + "." + millis; 290 } 291 } 292