View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.audit.rest;
18  
19  import java.text.DecimalFormat;
20  
21  public class ElapsedUtil {
22  
23      private static long NANO_PER_SECOND = 1000000000L;
24      private static long NANO_PER_MINUTE = NANO_PER_SECOND * 60;
25      private static long NANO_PER_HOUR = NANO_PER_MINUTE * 60;
26  
27      static void addElapsed(long elapsed, StringBuilder msg) {
28          long nanoseconds = elapsed;
29          // Get elapsed hours
30          long hours = nanoseconds / NANO_PER_HOUR;
31          // Get remaining nanoseconds
32          nanoseconds = nanoseconds % NANO_PER_HOUR;
33          // Get minutes
34          long minutes = nanoseconds / NANO_PER_MINUTE;
35          // Get remaining nanoseconds
36          nanoseconds = nanoseconds % NANO_PER_MINUTE;
37          // Get seconds
38          long seconds = nanoseconds / NANO_PER_SECOND;
39          // Get remaining nanoseconds
40          nanoseconds = nanoseconds % NANO_PER_SECOND;
41  
42          if (hours > 0) {
43              msg.append(hours).append(" hours ");
44          }
45          if (minutes > 0 || hours > 0) {
46              msg.append(minutes).append(" minutes ");
47          }
48  
49          DecimalFormat numFormat = null;
50          numFormat = new DecimalFormat("#0");
51          msg.append(numFormat.format(seconds)).append('.');
52          numFormat = new DecimalFormat("000000000");
53          msg.append(numFormat.format(nanoseconds)).append(" seconds");
54      }
55  }