1 /* 2 * Copyright (C) 2010 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 17 package vogar.util; 18 19 /** 20 * Utilities to make it easier to work with ISO 8601 dates and times. 21 * This is a subset of the original class from http://software.jessies.org/salma-hayek/ --- please submit fixes upstream. 22 */ 23 public class TimeUtilities { 24 /** 25 * Returns the ISO 8601-format String corresponding to the given duration (measured in milliseconds). 26 */ msToIsoString(long duration)27 public static String msToIsoString(long duration) { 28 long milliseconds = duration % 1000; 29 duration /= 1000; 30 long seconds = duration % 60; 31 duration /= 60; 32 long minutes = duration % 60; 33 duration /= 60; 34 long hours = duration; 35 36 StringBuilder result = new StringBuilder("P"); 37 if (hours != 0) { 38 result.append(hours); 39 result.append('H'); 40 } 41 if (result.length() > 1 || minutes != 0) { 42 result.append(minutes); 43 result.append('M'); 44 } 45 result.append(seconds); 46 if (milliseconds != 0) { 47 result.append('.'); 48 result.append(milliseconds); 49 } 50 result.append('S'); 51 return result.toString(); 52 } 53 54 /** 55 * Returns a string representation of the given number of milliseconds. 56 */ msToString(long ms)57 public static String msToString(long ms) { 58 return nsToString(ms * 1000000); 59 } 60 61 /** 62 * Returns a string representation of the given number of nanoseconds. 63 */ nsToString(long ns)64 public static String nsToString(long ns) { 65 if (ns < 1000L) { 66 return Long.toString(ns) + "ns"; 67 } else if (ns < 1000000L) { 68 return Long.toString(ns/1000L) + "us"; 69 } else if (ns < 1000000000L) { 70 return Long.toString(ns/1000000L) + "ms"; 71 } else if (ns < 60000000000L) { 72 return String.format("%.2fs", nsToS(ns)); 73 } else { 74 long duration = ns; 75 long nanoseconds = duration % 1000; 76 duration /= 1000; 77 long microseconds = duration % 1000; 78 duration /= 1000; 79 long milliseconds = duration % 1000; 80 duration /= 1000; 81 long seconds = duration % 60; 82 duration /= 60; 83 long minutes = duration % 60; 84 duration /= 60; 85 long hours = duration % 24; 86 duration /= 24; 87 long days = duration; 88 89 StringBuilder result = new StringBuilder(); 90 if (days != 0) { 91 result.append(days); 92 result.append('d'); 93 } 94 if (result.length() > 1 || hours != 0) { 95 result.append(hours); 96 result.append('h'); 97 } 98 if (result.length() > 1 || minutes != 0) { 99 result.append(minutes); 100 result.append('m'); 101 } 102 result.append(seconds); 103 result.append('s'); 104 return result.toString(); 105 } 106 } 107 108 /** 109 * Converts nanoseconds into (fractional) seconds. 110 */ nsToS(long ns)111 public static double nsToS(long ns) { 112 return ((double) ns)/1000000000.0; 113 } 114 TimeUtilities()115 private TimeUtilities() { 116 } 117 } 118