FileDocCategorySizeDatePackage
TimeFormatter.javaAPI DocAzureus 3.0.3.44886Tue Oct 02 17:34:42 BST 2007org.gudy.azureus2.core3.util

TimeFormatter

public class TimeFormatter extends Object
author
Olivier

Fields Summary
static final String[]
TIME_SUFFIXES
private static final SimpleDateFormat
http_date_format
Constructors Summary
Methods Summary
public static java.lang.Stringformat(long time)
Format time into two time sections, the first chunk trimmed, the second with always with 2 digits. Sections are *d, **h, **m, **s. Section will be skipped if 0.

param
time time in seconds
return
Formatted time string


	
			// see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1
		
		http_date_format.setTimeZone(TimeZone.getTimeZone("GMT"));
	
		if (time == Constants.INFINITY_AS_INT || time >= Constants.INFINITE_AS_LONG)
			return Constants.INFINITY_STRING;

		if (time < 0)
			return "";

		// secs, mins, hours, days
		int[] vals = {
			(int) time % 60,
			(int) (time / 60) % 60,
			(int) (time / 3600) % 24,
			(int) (time / 86400) % 365,
			(int) (time / 31536000)
			};

		int end = vals.length - 1;
		while (vals[end] == 0 && end > 0) {
			end--;
		}
		
		String result = vals[end] + TIME_SUFFIXES[end];

		// skip until we have a non-zero time section
		do {
			end--;
		} while (end >= 0 && vals[end] == 0);
		
		if (end >= 0)
			result += " " + twoDigits(vals[end]) + TIME_SUFFIXES[end];

		return result;
	
public static java.lang.StringformatColon(long time)
Format time into "[[# y] # d] 00:00:00" format

param
time time in seconds
return

      if (time == Constants.INFINITY_AS_INT || time >= Constants.INFINITE_AS_LONG) return Constants.INFINITY_STRING;
      if (time < 0) return "";

      int secs = (int) time % 60;
      int mins = (int) (time / 60) % 60;
      int hours = (int) (time /3600) % 24;
      int days = (int) (time / 86400) % 365;
      int years = (int) (time / 31536000);
      
      String result = "";
      if (years > 0) result += years + "y ";
      if (years > 0 || days > 0) result += days + "d ";
      result += twoDigits(hours) + ":" + twoDigits(mins) + ":" + twoDigits(secs);

      return result;
    
public static java.lang.StringformatNanoAsMilli(long nanos)

    	final long truncator = 60*1000000000L;
    	
    	nanos = nanos - ((nanos/truncator) * truncator);
    	
    	return( String.valueOf(((double)nanos)/1000000) + " ms" );
    
public static java.lang.StringgetHTTPDate(long millis)

		synchronized( http_date_format ){
			
			return( http_date_format.format(new Date( millis )));
		}
    
public static java.lang.StringmilliStamp()

    	long nanos = SystemTime.getHighPrecisionCounter();
    	
    	final long truncator = 60*1000000000L;
    	
    	nanos = nanos - ((nanos/truncator) * truncator);

    	String	str = String.valueOf( nanos/1000000 );
    	
    	while( str.length() < 5 ){
    		
    		str = "0" + str;
    	}
    	
    	return( str + ": " );
    
public static voidmilliTrace(java.lang.String str)

    	System.out.println( milliStamp() + str );
    
public static intparseColon(java.lang.String str)
parse time in h:m:s format to SECONDS

param
str
return

    	final int[]	multipliers = { 1, 60, 3600, 86400, 31536000 };
    	
    	String[]	bits = str.split( ":" );
    	
    	int	result = 0;
    	
    	for (int i=0;i<bits.length;i++){
    		
    		String bit = bits[bits.length-(i+1)].trim();
    		
    		if ( bit.length() > 0 ){
    			
    			result += multipliers[i] * Integer.parseInt( bit );
    		}
    	}
    	
    	return( result );
    
private static java.lang.StringtwoDigits(int i)

      return (i < 10) ? "0" + i : String.valueOf(i);