2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.protocol.util;
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.TimeUnit;
15 * Statistics utility class.
17 public final class StatisticsUtil {
19 private StatisticsUtil() {
20 throw new UnsupportedOperationException();
24 * Formats elapsed time in seconds to form days:hours:minutes:seconds.
26 * @param seconds Elapsed time in seconds.
27 * @return Formated time as string d:hh:mm:ss
29 public static String formatElapsedTime(final long seconds) {
30 Preconditions.checkArgument(seconds >= 0);
31 return String.format("%1d:%02d:%02d:%02d",
32 TimeUnit.SECONDS.toDays(seconds),
33 TimeUnit.SECONDS.toHours(seconds) - TimeUnit.DAYS.toHours(TimeUnit.SECONDS.toDays(seconds)),
34 TimeUnit.SECONDS.toMinutes(seconds) - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(seconds)),
35 seconds - TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(seconds)));
39 * Get current time in seconds. See also {@link System#currentTimeMillis()}.
41 * @return the difference, measured in seconds, between the current time and midnight, January 1, 1970 UTC.
43 public static long getCurrentTimestampInSeconds() {
44 return TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());