enhanced UTCDiffString

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7782 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 14 years ago
parent 07cbb6cb5f
commit aa6c32d753

@ -24,6 +24,7 @@
package net.yacy.cora.date; package net.yacy.cora.date;
import java.text.DecimalFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
@ -65,10 +66,10 @@ public class GenericFormatter extends AbstractFormatter implements DateFormatter
public static final GenericFormatter ANSIC_FORMATTER = new GenericFormatter(FORMAT_ANSIC, time_second); public static final GenericFormatter ANSIC_FORMATTER = new GenericFormatter(FORMAT_ANSIC, time_second);
public static final GenericFormatter RFC1123_SHORT_FORMATTER = new GenericFormatter(FORMAT_RFC1123_SHORT, time_minute); public static final GenericFormatter RFC1123_SHORT_FORMATTER = new GenericFormatter(FORMAT_RFC1123_SHORT, time_minute);
private SimpleDateFormat dateFormat; private final SimpleDateFormat dateFormat;
private long maxCacheDiff; private final long maxCacheDiff;
public GenericFormatter(SimpleDateFormat dateFormat, long maxCacheDiff) { public GenericFormatter(final SimpleDateFormat dateFormat, final long maxCacheDiff) {
this.dateFormat = (SimpleDateFormat) dateFormat.clone(); // avoid concurrency locking effects this.dateFormat = (SimpleDateFormat) dateFormat.clone(); // avoid concurrency locking effects
this.last_time = 0; this.last_time = 0;
this.last_format = ""; this.last_format = "";
@ -93,17 +94,17 @@ public class GenericFormatter extends AbstractFormatter implements DateFormatter
} }
public String format() { public String format() {
if (Math.abs(System.currentTimeMillis() - last_time) < maxCacheDiff) return last_format; if (Math.abs(System.currentTimeMillis() - this.last_time) < this.maxCacheDiff) return this.last_format;
synchronized (this.dateFormat) { synchronized (this.dateFormat) {
// threads that had been waiting here may use the cache now instead of calculating the date again // threads that had been waiting here may use the cache now instead of calculating the date again
long time = System.currentTimeMillis(); final long time = System.currentTimeMillis();
if (Math.abs(time - last_time) < maxCacheDiff) return last_format; if (Math.abs(time - this.last_time) < this.maxCacheDiff) return this.last_format;
// if the cache is not fresh, calculate the date // if the cache is not fresh, calculate the date
last_format = this.dateFormat.format(new Date(time)); this.last_format = this.dateFormat.format(new Date(time));
last_time = time; this.last_time = time;
} }
return last_format; return this.last_format;
} }
/** /**
@ -163,6 +164,8 @@ public class GenericFormatter extends AbstractFormatter implements DateFormatter
} }
} }
private final static DecimalFormat D2 = new DecimalFormat("00");
public static String UTCDiffString() { public static String UTCDiffString() {
// we express the UTC Difference in 5 digits: // we express the UTC Difference in 5 digits:
// SHHMM // SHHMM
@ -173,19 +176,22 @@ public class GenericFormatter extends AbstractFormatter implements DateFormatter
// we need too show also the minutes of the time shift // we need too show also the minutes of the time shift
// Examples: http://www.timeanddate.com/library/abbreviations/timezones/ // Examples: http://www.timeanddate.com/library/abbreviations/timezones/
final long offsetHours = UTCDiff(); final long offsetHours = UTCDiff();
final int om = Math.abs((int) (offsetHours / AbstractFormatter.minuteMillis)) % 60; final StringBuilder sb = new StringBuilder(5);
final int oh = Math.abs((int) (offsetHours / AbstractFormatter.hourMillis));
String diff = Integer.toString(om);
if (diff.length() < 2) diff = "0" + diff;
diff = Integer.toString(oh) + diff;
if (diff.length() < 4) diff = "0" + diff;
if (offsetHours < 0) { if (offsetHours < 0) {
return "-" + diff; sb.append('-');
} else {
sb.append('+');
} }
return "+" + diff; sb.append(D2.format(Math.abs((int) (offsetHours / AbstractFormatter.hourMillis))));
sb.append(D2.format(Math.abs((int) (offsetHours / AbstractFormatter.minuteMillis)) % 60));
return sb.toString();
} }
public static long correctedUTCTime() { public static long correctedUTCTime() {
return System.currentTimeMillis() - UTCDiff(); return System.currentTimeMillis() - UTCDiff();
} }
public static void main(final String[] args) {
System.out.println(UTCDiffString());
}
} }

Loading…
Cancel
Save