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

@ -11,12 +11,12 @@
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either * License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version. * version 2.1 of the License, or (at your option) any later version.
* *
* This library is distributed in the hope that it will be useful, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with this program in the file lgpl21.txt * along with this program in the file lgpl21.txt
* If not, see <http://www.gnu.org/licenses/>. * If not, see <http://www.gnu.org/licenses/>.
@ -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;
@ -37,7 +38,7 @@ public class GenericFormatter extends AbstractFormatter implements DateFormatter
public static final String PATTERN_SHORT_MILSEC = "yyyyMMddHHmmssSSS"; public static final String PATTERN_SHORT_MILSEC = "yyyyMMddHHmmssSSS";
public static final String PATTERN_RFC1123_SHORT = "EEE, dd MMM yyyy"; public static final String PATTERN_RFC1123_SHORT = "EEE, dd MMM yyyy";
public static final String PATTERN_ANSIC = "EEE MMM d HH:mm:ss yyyy"; public static final String PATTERN_ANSIC = "EEE MMM d HH:mm:ss yyyy";
public static final SimpleDateFormat FORMAT_SHORT_DAY = new SimpleDateFormat(PATTERN_SHORT_DAY, Locale.US); public static final SimpleDateFormat FORMAT_SHORT_DAY = new SimpleDateFormat(PATTERN_SHORT_DAY, Locale.US);
public static final SimpleDateFormat FORMAT_SHORT_SECOND = new SimpleDateFormat(PATTERN_SHORT_SECOND, Locale.US); public static final SimpleDateFormat FORMAT_SHORT_SECOND = new SimpleDateFormat(PATTERN_SHORT_SECOND, Locale.US);
public static final SimpleDateFormat FORMAT_SHORT_MILSEC = new SimpleDateFormat(PATTERN_SHORT_MILSEC, Locale.US); public static final SimpleDateFormat FORMAT_SHORT_MILSEC = new SimpleDateFormat(PATTERN_SHORT_MILSEC, Locale.US);
@ -64,11 +65,11 @@ public class GenericFormatter extends AbstractFormatter implements DateFormatter
public static final GenericFormatter SHORT_MILSEC_FORMATTER = new GenericFormatter(FORMAT_SHORT_MILSEC, 1); public static final GenericFormatter SHORT_MILSEC_FORMATTER = new GenericFormatter(FORMAT_SHORT_MILSEC, 1);
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 long maxCacheDiff;
public GenericFormatter(SimpleDateFormat dateFormat, long maxCacheDiff) { private final SimpleDateFormat dateFormat;
private final 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 = "";
@ -91,21 +92,21 @@ public class GenericFormatter extends AbstractFormatter implements DateFormatter
return this.dateFormat.format(date); return this.dateFormat.format(date);
} }
} }
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;
} }
/** /**
* Parse a String representation of a Date in short day format assuming the date * Parse a String representation of a Date in short day format assuming the date
* is aligned to the GMT/UTC timezone. An example for such a date string is "20071218". * is aligned to the GMT/UTC timezone. An example for such a date string is "20071218".
@ -119,7 +120,7 @@ public class GenericFormatter extends AbstractFormatter implements DateFormatter
return this.dateFormat.parse(timeString); return this.dateFormat.parse(timeString);
} }
} }
/** /**
* Like {@link #parseShortSecond(String)} using additional timezone information provided in an * Like {@link #parseShortSecond(String)} using additional timezone information provided in an
* offset String, like "+0100" for CET. * offset String, like "+0100" for CET.
@ -151,7 +152,7 @@ public class GenericFormatter extends AbstractFormatter implements DateFormatter
final long om = Long.parseLong(diffString.substring(3)); final long om = Long.parseLong(diffString.substring(3));
return ((ahead) ? (long) 1 : (long) -1) * (oh * AbstractFormatter.hourMillis + om * AbstractFormatter.minuteMillis); return ((ahead) ? (long) 1 : (long) -1) * (oh * AbstractFormatter.hourMillis + om * AbstractFormatter.minuteMillis);
} }
private static long UTCDiff() { private static long UTCDiff() {
// DST_OFFSET is dependent on the time of the Calendar, so it has to be updated // DST_OFFSET is dependent on the time of the Calendar, so it has to be updated
// to get the correct current offset // to get the correct current offset
@ -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