50df91caa3cec0a704f917f3ee9360cd2e8ca6de
[bgpcep.git] / util / src / main / java / org / opendaylight / protocol / util / StatisticsUtil.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.util;
10
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.TimeUnit;
13
14 /**
15  * Statistics utility class.
16  */
17 public final class StatisticsUtil {
18
19     private StatisticsUtil() {
20         throw new UnsupportedOperationException();
21     }
22
23     /**
24      * Formats elapsed time in seconds to form days:hours:minutes:seconds.
25      *
26      * @param seconds Elapsed time in seconds.
27      * @return Formated time as string d:hh:mm:ss
28      */
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)));
36     }
37
38     /**
39      * Get current time in seconds. See also {@link System#currentTimeMillis()}.
40      *
41      * @return the difference, measured in seconds, between the current time and midnight, January 1, 1970 UTC.
42      */
43     public static long getCurrentTimestampInSeconds() {
44         return TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
45     }
46 }