--- /dev/null
+/*******************************************************************************
+ * Copyright
+ * 2013 axYus - www.axyus.com
+ * 2013 P.Renier - paul.renier@axyus.com
+ *
+ * This file is part of XEMELIOS.
+ *
+ * XEMELIOS is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * XEMELIOS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with XEMELIOS; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *******************************************************************************/
+package fr.gouv.finances.dgfip.xemelios.utils;
+
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * @author paul.renier
+ *
+ */
+public class UnitUtils {
+
+ public static final TreeMap<Long, String> DURATION_CONV_MAPS = new TreeMap<Long, String>();
+ static {
+ DURATION_CONV_MAPS.put(31536000000L, "y");// 365 * 24 * 60 * 60 * 1000
+ DURATION_CONV_MAPS.put(108000000L, "m");// 30 * 60 * 60 * 1000
+ DURATION_CONV_MAPS.put(86400000L, "j");// 24 * 60 * 60 * 1000
+ DURATION_CONV_MAPS.put(3600000L, "h");// 60 * 60 * 1000
+ DURATION_CONV_MAPS.put(60000L, "mn");// 60 * 1000
+ DURATION_CONV_MAPS.put(1000L, "s");
+ DURATION_CONV_MAPS.put(1L, "ms");
+ }
+
+ public static String formatDuration(final long d) {
+ return format(d, DURATION_CONV_MAPS);
+ }
+
+ private static String format(long rest, final TreeMap<Long, String> convMap) {
+ final StringBuilder result = new StringBuilder();
+ for (final Map.Entry<Long, String> unit : convMap.descendingMap().entrySet()) {
+ final Long factor = unit.getKey();
+ final String suffix = unit.getValue();
+
+ final long qt = (rest / factor);
+ rest = (rest % factor);
+ result.append(qt).append(suffix).append(" ");
+ }
+
+ return result.toString();
+ }
+
+}