[MNG-5975] Use Java 7's SimpleDateFormat in CLIReportingUtils#formatTimestamp

This commit is contained in:
Michael Osipov 2016-02-12 23:30:47 +01:00
parent a2358ba7bb
commit 355f4dff03
1 changed files with 10 additions and 22 deletions

View File

@ -19,15 +19,15 @@ package org.apache.maven.cli;
* under the License. * under the License.
*/ */
import org.codehaus.plexus.util.Os;
import org.slf4j.Logger;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.Properties; import java.util.Properties;
import java.util.TimeZone;
import org.codehaus.plexus.util.Os;
import org.slf4j.Logger;
/** /**
* Utility class used to report errors, statistics, application version info, etc. * Utility class used to report errors, statistics, application version info, etc.
@ -152,24 +152,8 @@ public final class CLIReportingUtils
public static String formatTimestamp( long timestamp ) public static String formatTimestamp( long timestamp )
{ {
// Manual construction of the tz offset because only Java 7 is aware of ISO 8601 time zones SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssXXX" );
TimeZone tz = TimeZone.getDefault(); return sdf.format( new Date( timestamp ) );
int offset = tz.getRawOffset();
// Raw offset ignores DST, so check if we are in DST now and add the offset
if ( tz.inDaylightTime( new Date( timestamp ) ) )
{
offset += tz.getDSTSavings();
}
// CHECKSTYLE_OFF: MagicNumber
long m = Math.abs( ( offset / ONE_MINUTE ) % 60 );
long h = Math.abs( ( offset / ONE_HOUR ) % 24 );
// CHECKSTYLE_ON: MagicNumber
int offsetDir = (int) Math.signum( (float) offset );
char offsetSign = offsetDir >= 0 ? '+' : '-';
return String.format( "%tFT%<tT%s%02d:%02d", timestamp, offsetSign, h, m );
} }
public static String formatDuration( long duration ) public static String formatDuration( long duration )
@ -185,18 +169,22 @@ public final class CLIReportingUtils
String format; String format;
if ( d > 0 ) if ( d > 0 )
{ {
// Length 11+ chars
format = "%d d %02d:%02d h"; format = "%d d %02d:%02d h";
} }
else if ( h > 0 ) else if ( h > 0 )
{ {
// Length 7 chars
format = "%2$02d:%3$02d h"; format = "%2$02d:%3$02d h";
} }
else if ( m > 0 ) else if ( m > 0 )
{ {
// Length 9 chars
format = "%3$02d:%4$02d min"; format = "%3$02d:%4$02d min";
} }
else else
{ {
// Length 7-8 chars
format = "%4$d.%5$03d s"; format = "%4$d.%5$03d s";
} }