package com.mousetech.gourmetj.utils; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Handle conversion of complex time fields where days, hours, * minutes and seconds may be present as separate entities. * * @author timh * @since Jan 8, 2022 */ public class TimeFormatter { /* Logger */ private static final Logger log = LoggerFactory.getLogger(TimeFormatter.class); public static String formatTime(Long ltime) { if (ltime == null) { return ""; } int time = ltime.intValue(); int dd, hh, mm, ss; ss = time % 60; time /= 60; mm = time % 60; time /= 60; hh = time % 24; dd = time / 24; StringBuffer sb = new StringBuffer(20); if (dd > 0) { sb.append(dd).append("d "); } if (hh > 0) { sb.append(hh).append("h "); } if (mm > 0) { sb.append(mm); if ((ss == 0) && (hh == 0)) { sb.append(" minutes"); } else { sb.append("min. "); } } if (ss > 0) { sb.append(dd).append("sec. "); } return sb.toString().trim(); } // private enum MunchUnit { // mu_NONE, mu_DAY, mu_HOUR, mu_MINUTE, mw_SECOND // } private static class TimeMuncher { public class MunchException extends Exception { /** * */ private static final long serialVersionUID = 1L; } final private String SPAT_DAYS = "days?|d"; final private String SPAT_HOURS = "hours?|h|hr"; final private String SPAT_MINUTES = "minutes?|mins?|m"; final private String SPAT_SECONDS = "seconds?|secs?|s"; final Pattern pat_number; final Pattern pat_days; final Pattern pat_hours; final Pattern pat_mins; final Pattern pat_secs; final Scanner scanner; public TimeMuncher(String timestr) { pat_number = Pattern.compile("(\\d[\\,\\.]?\\d*)?(.*)"); // Compile here for future I18N support pat_days = Pattern.compile(SPAT_DAYS); pat_hours = Pattern.compile(SPAT_HOURS); pat_mins = Pattern.compile(SPAT_MINUTES); pat_secs = Pattern.compile(SPAT_SECONDS); scanner = new Scanner(timestr); scanner.useDelimiter("\\W"); } public Long scan() { Double sum = null; Double dvalue = null; try { while (scanner.hasNext()) { if (scanner.hasNextDouble()) { dvalue = scanner.nextDouble(); } else { String units = scanner.next(); sum = setSum(sum, dvalue, units); dvalue = null; } } } catch (MunchException e) { log.warn("Unparseable time value"); e.printStackTrace(); } if (sum == null) { return null; } return sum.longValue(); } private Double setSum(Double sum, Double dvalue, String units) throws MunchException { Matcher m; m = pat_number.matcher(units); if (!m.matches()) { throw new MunchException(); } if (m.group(1) != null) { String a = m.group(1); if (dvalue != null) { throw new MunchException(); } dvalue = Double .valueOf(a); //units = units.substring(m.end()); } units = m.group(2); m = pat_days.matcher(units); if (m.matches()) { return addToSum(sum, dvalue, 24L * 60L * 60L * 1000L); } m = pat_hours.matcher(units); if (m.matches()) { return addToSum(sum, dvalue, 60L * 60L * 1000L); } m = pat_mins.matcher(units); if (m.matches()) { return addToSum(sum, dvalue, 60L * 1000L); } m = pat_secs.matcher(units); if (m.matches()) { return addToSum(sum, dvalue, 1000L); } return sum; } private Double addToSum(Double sum, Double dvalue, long l) throws MunchException { if (dvalue == null) { throw new TimeMuncher.MunchException(); } dvalue *= l; if (sum == null) { sum = 0.0; } sum += dvalue; return sum; } } public static Long parseTime(String timestr) { TimeMuncher muncher = new TimeMuncher(timestr); Long interval = muncher.scan(); return interval; } }