1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.cts.tradefed.result; 17 18 import java.text.SimpleDateFormat; 19 import java.util.Date; 20 import java.util.concurrent.TimeUnit; 21 22 /** 23 * Utility class for formatting times as strings. 24 */ 25 class TimeUtil { 26 27 /** 28 * Return a prettified version of the given elapsed time 29 * @return 30 */ formatElapsedTime(long elapsedTimeMs)31 static String formatElapsedTime(long elapsedTimeMs) { 32 long seconds = TimeUnit.MILLISECONDS.toSeconds(elapsedTimeMs) % 60; 33 long minutes = TimeUnit.MILLISECONDS.toMinutes(elapsedTimeMs) % 60; 34 long hours = TimeUnit.MILLISECONDS.toHours(elapsedTimeMs); 35 StringBuilder time = new StringBuilder(); 36 if (hours > 0) { 37 time.append(hours); 38 time.append("h "); 39 } 40 if (minutes > 0) { 41 time.append(minutes); 42 time.append("m "); 43 } 44 time.append(seconds); 45 time.append("s"); 46 47 return time.toString(); 48 } 49 50 /** 51 * Return the current timestamp as a {@link String} suitable for displaying. 52 * <p/> 53 * Example: Fri Aug 20 15:13:03 PDT 2010 54 */ getTimestamp()55 static String getTimestamp() { 56 return getTimestamp(System.currentTimeMillis()); 57 } 58 59 /** 60 * Return the given time as a {@link String} suitable for displaying. 61 * <p/> 62 * Example: Fri Aug 20 15:13:03 PDT 2010 63 * 64 * @param time the epoch time in ms since midnight Jan 1, 1970 65 */ getTimestamp(long time)66 static String getTimestamp(long time) { 67 SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); 68 return dateFormat.format(new Date(time)); 69 } 70 71 /** 72 * Return the current timestamp in a compressed format, used to uniquely identify results. 73 * <p/> 74 * Example: 2010.08.16_11.42.12 75 */ getResultTimestamp()76 static String getResultTimestamp() { 77 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd_HH.mm.ss"); 78 return dateFormat.format(new Date()); 79 } 80 81 } 82