001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.util; 018 019import java.text.DecimalFormat; 020import java.text.DecimalFormatSymbols; 021import java.text.NumberFormat; 022import java.time.Duration; 023import java.util.Locale; 024import java.util.regex.Matcher; 025import java.util.regex.Pattern; 026 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030/** 031 * Time utils. 032 */ 033public final class TimeUtils { 034 035 private static final Logger LOG = LoggerFactory.getLogger(TimeUtils.class); 036 private static final Pattern NUMBERS_ONLY_STRING_PATTERN = Pattern.compile("^[-]?(\\d)+$", Pattern.CASE_INSENSITIVE); 037 private static final Pattern HOUR_REGEX_PATTERN = Pattern.compile("((\\d)*(\\d))\\s*h(our(s)?)?(?=\\b|\\d|$)", Pattern.CASE_INSENSITIVE); 038 private static final Pattern MINUTES_REGEX_PATTERN = Pattern.compile("((\\d)*(\\d))\\s*m(in(ute(s)?)?)?(?=\\b|\\d|$)", Pattern.CASE_INSENSITIVE); 039 private static final Pattern SECONDS_REGEX_PATTERN = Pattern.compile("((\\d)(\\d)*)(\\.(\\d+))?\\s*s(ec(ond)?(s)?)?(?=\\b|\\d|$)", Pattern.CASE_INSENSITIVE); 040 private static final Pattern MILLIS_REGEX_PATTERN = Pattern.compile("((\\d)(\\d)*)(\\.(\\d+))?\\s*m(illi)?s(ec(ond)?(s)?)?(?=\\b|\\d|$)", Pattern.CASE_INSENSITIVE); 041 042 private TimeUtils() { 043 } 044 045 public static boolean isPositive(Duration dur) { 046 return dur.getSeconds() > 0 || dur.getNano() != 0; 047 } 048 049 public static String printDuration(Duration uptime) { 050 return printDuration(uptime.toMillis()); 051 } 052 053 /** 054 * Prints the duration in a human readable format as X days Y hours Z minutes etc. 055 * 056 * @param uptime the uptime in millis 057 * @return the time used for displaying on screen or in logs 058 */ 059 public static String printDuration(double uptime) { 060 // Code taken from Karaf 061 // https://svn.apache.org/repos/asf/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/InfoAction.java 062 063 NumberFormat fmtI = new DecimalFormat("###,###", new DecimalFormatSymbols(Locale.ENGLISH)); 064 NumberFormat fmtD = new DecimalFormat("###,##0.000", new DecimalFormatSymbols(Locale.ENGLISH)); 065 066 uptime /= 1000; 067 if (uptime < 60) { 068 return fmtD.format(uptime) + " seconds"; 069 } 070 uptime /= 60; 071 if (uptime < 60) { 072 long minutes = (long) uptime; 073 String s = fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute"); 074 return s; 075 } 076 uptime /= 60; 077 if (uptime < 24) { 078 long hours = (long) uptime; 079 long minutes = (long) ((uptime - hours) * 60); 080 String s = fmtI.format(hours) + (hours > 1 ? " hours" : " hour"); 081 if (minutes != 0) { 082 s += " " + fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute"); 083 } 084 return s; 085 } 086 uptime /= 24; 087 long days = (long) uptime; 088 long hours = (long) ((uptime - days) * 24); 089 String s = fmtI.format(days) + (days > 1 ? " days" : " day"); 090 if (hours != 0) { 091 s += " " + fmtI.format(hours) + (hours > 1 ? " hours" : " hour"); 092 } 093 return s; 094 } 095 096 public static Duration toDuration(String source) throws IllegalArgumentException { 097 return Duration.ofMillis(toMilliSeconds(source)); 098 } 099 100 public static long toMilliSeconds(String source) throws IllegalArgumentException { 101 // quick conversion if its only digits 102 boolean digit = true; 103 for (int i = 0; i < source.length(); i++) { 104 char ch = source.charAt(i); 105 // special for fist as it can be negative number 106 if (i == 0 && ch == '-') { 107 continue; 108 } 109 // quick check if its 0..9 110 if (ch < '0' || ch > '9') { 111 digit = false; 112 break; 113 } 114 } 115 if (digit) { 116 return Long.parseLong(source); 117 } 118 119 long milliseconds = 0; 120 boolean foundFlag = false; 121 122 checkCorrectnessOfPattern(source); 123 Matcher matcher; 124 125 matcher = createMatcher(NUMBERS_ONLY_STRING_PATTERN, source); 126 if (matcher.find()) { 127 // Note: This will also be used for regular numeric strings. 128 // This String -> long converter will be used for all strings. 129 milliseconds = Long.parseLong(source); 130 } else { 131 matcher = createMatcher(HOUR_REGEX_PATTERN, source); 132 if (matcher.find()) { 133 milliseconds = milliseconds + (3600000 * Long.parseLong(matcher.group(1))); 134 foundFlag = true; 135 } 136 137 matcher = createMatcher(MINUTES_REGEX_PATTERN, source); 138 if (matcher.find()) { 139 long minutes = Long.parseLong(matcher.group(1)); 140 foundFlag = true; 141 milliseconds = milliseconds + (60000 * minutes); 142 } 143 144 matcher = createMatcher(SECONDS_REGEX_PATTERN, source); 145 if (matcher.find()) { 146 long seconds = Long.parseLong(matcher.group(1)); 147 milliseconds += 1000 * seconds; 148 if (matcher.group(5) != null && !matcher.group(5).isEmpty()) { 149 long ms = Long.parseLong(matcher.group(5)); 150 milliseconds += ms; 151 } 152 foundFlag = true; 153 } 154 155 matcher = createMatcher(MILLIS_REGEX_PATTERN, source); 156 if (matcher.find()) { 157 long millis = Long.parseLong(matcher.group(1)); 158 foundFlag = true; 159 milliseconds += millis; 160 } 161 162 // No pattern matched... initiating fallback check and conversion (if required). 163 // The source at this point may contain illegal values or special characters 164 if (!foundFlag) { 165 milliseconds = Long.parseLong(source); 166 } 167 } 168 169 LOG.trace("source: [{}], milliseconds: {}", source, milliseconds); 170 171 return milliseconds; 172 } 173 174 private static void checkCorrectnessOfPattern(String source) { 175 //replace only numbers once 176 Matcher matcher = createMatcher(NUMBERS_ONLY_STRING_PATTERN, source); 177 String replaceSource = matcher.replaceFirst(""); 178 179 //replace hour string once 180 matcher = createMatcher(HOUR_REGEX_PATTERN, replaceSource); 181 if (matcher.find() && matcher.find()) { 182 throw new IllegalArgumentException("Hours should not be specified more then once: " + source); 183 } 184 replaceSource = matcher.replaceFirst(""); 185 186 //replace minutes once 187 matcher = createMatcher(MINUTES_REGEX_PATTERN, replaceSource); 188 if (matcher.find() && matcher.find()) { 189 throw new IllegalArgumentException("Minutes should not be specified more then once: " + source); 190 } 191 replaceSource = matcher.replaceFirst(""); 192 193 //replace seconds once 194 matcher = createMatcher(SECONDS_REGEX_PATTERN, replaceSource); 195 if (matcher.find() && matcher.find()) { 196 throw new IllegalArgumentException("Seconds should not be specified more then once: " + source); 197 } 198 replaceSource = matcher.replaceFirst(""); 199 200 //replace millis once 201 matcher = createMatcher(MILLIS_REGEX_PATTERN, replaceSource); 202 if (matcher.find() && matcher.find()) { 203 throw new IllegalArgumentException("Milliseconds should not be specified more then once: " + source); 204 } 205 replaceSource = matcher.replaceFirst(""); 206 207 if (replaceSource.length() > 0) { 208 throw new IllegalArgumentException("Illegal characters: " + source); 209 } 210 } 211 212 private static Matcher createMatcher(Pattern pattern, String source) { 213 return pattern.matcher(source); 214 } 215 216}