Mail Archive Home | sync4j-commits List | August 2008 Index
| <-- Date Index --> | <-- Thread Index --> |
Date: Wednesday, August 20, 2008 @ 14:03:53
Author: mauro
Path: .../pim-framework/src/main/java/com/funambol/common/pim/converter
Modified: TimeZoneHelper.java
Fixed bug with transition parsing.
Introduced TZID shortcut.
---------------------+
TimeZoneHelper.java | 133 ++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 114 insertions(+), 19 deletions(-)
Index:
funambol/common/pim-framework/src/main/java/com/funambol/common/pim/converter/TimeZoneHelper.java
diff -u
funambol/common/pim-framework/src/main/java/com/funambol/common/pim/converter/TimeZoneHelper.java:1.5
funambol/common/pim-framework/src/main/java/com/funambol/common/pim/converter/TimeZoneHelper.java:1.6
---
funambol/common/pim-framework/src/main/java/com/funambol/common/pim/converter/TimeZoneHelper.java:1.5
Thu Jul 17 15:42:25 2008
+++
funambol/common/pim-framework/src/main/java/com/funambol/common/pim/converter/TimeZoneHelper.java
Wed Aug 20 14:03:53 2008
@@ -61,11 +61,13 @@
import com.funambol.common.pim.model.TzStandardComponent;
import com.funambol.common.pim.model.VComponent;
import com.funambol.common.pim.model.VTimezone;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
* This class implements the time-zone conversions.
*
- * @version $Id: TimeZoneHelper.java,v 1.5 2008-07-17 13:42:25 nichele Exp $
+ * @version $Id: TimeZoneHelper.java,v 1.6 2008-08-20 12:03:53 mauro Exp $
*/
public class TimeZoneHelper {
@@ -81,10 +83,10 @@
private static final DecimalFormat MM = new DecimalFormat("00");
private static final TimeZone UTC_TIME_ZONE =
TimeZone.getTimeZone("UTC");
- private static final long SEVEN_MONTHS = 18144000000L; // 210 days
- private static final long FIVE_MONTHS = 12960000000L; // 150 days
+ private static final long NINE_MONTHS = 23328000000L; // 270 days
+ private static final long THREE_MONTHS = 7776000000L; // 90 days
- private static String[] FAVOURITE_TIME_ZONE_IDS = {
+ private static String[] FAVORITE_TIME_ZONE_IDS = {
"Etc/UTC",
"Europe/Berlin",
"Europe/London",
@@ -123,10 +125,15 @@
"Australia/Sydney",
"Antarctica/South_Pole"
};
+
+ private Pattern OLSON_ID_PATTERN = Pattern.compile(
+
"(Europe|A((meric)|(si)|(fric)|(ustrali)|(ntarctic))a|Pacific|Atlantic)"
+ + "/[A-Z]([A-Z,a-z,_,',\\-])+"
+ + "(/[A-Z]([A-Z,a-z,_,',\\-])+)?");
//---------------------------------------------------------------
Properties
- private String name;
+ private String name = null;
private int basicOffset; // in milliseconds
private List<TimeZoneTransition> transitions =
new ArrayList<TimeZoneTransition>();
@@ -137,6 +144,16 @@
protected String getName() {
return name;
}
+
+ /**
+ * This setter is only for test purposes. Usually, the name is set by
+ * constructors.
+ *
+ * @param name the new name to set
+ */
+ protected void setName(String name) {
+ this.name = name;
+ }
protected int getBasicOffset() {
return basicOffset;
@@ -339,7 +356,15 @@
setFormattersToUTC();
Property tzID = vTimeZone.getProperty("TZID");
if (tzID != null) {
- this.name = tzID.getValue();
+ this.name = tzID.getValue();
+
+ // Try and skip the parsing by using just the TZID:
+ String extracted = extractID(name);
+ if (extracted != null) {
+ cacheID(extracted);
+ processID(extracted, from, to);
+ return;
+ }
List<VComponent> standardTimeRules =
vTimeZone.getComponents("STANDARD");
@@ -404,7 +429,10 @@
protected void processID(String id, long from, long to) {
DateTimeZone tz = DateTimeZone.forID(id);
- name = id; // The Olson ID is perfect as a unique name
+ if (name == null) { // The name could have been set already using
TZID
+ // and in this case it is important not to
change it
+ name = id; // The Olson ID is perfect as a unique name
+ }
basicOffset = tz.getStandardOffset(from);
transitions.clear();
@@ -439,13 +467,22 @@
//----------------------------------------------------------- Public
methods
+ /**
+ * Gets an Olson ID corresponding to the transitions and offsets saved.
+ * First it looks for a cached ID. If it is not found, it looks for a
+ * matching ID among the favorite ones. Otherwise it looks for it among
all
+ * available IDs with the same basic offset.
+ *
+ * @return a string containing the Olson ID or null if no matching ID is
+ * found
+ */
public String toID() {
if (cachedID) {
return id;
}
- for (String idGuess : FAVOURITE_TIME_ZONE_IDS) {
+ for (String idGuess : FAVORITE_TIME_ZONE_IDS) {
if (matchesID(idGuess)) {
return cacheID(idGuess);
}
@@ -459,21 +496,55 @@
return cacheID(null); // No matching time zone found
}
+ /**
+ * Gets an Olson ID corresponding to the information saved and a
suggestion.
+ * First it looks for a cached ID. If it is not found, it checks if the
+ * saved name simply contains an Olson ID. If it does, it will be
returned
+ * as the result without further investigation. If that is not the case,
the
+ * suggested ID is checked against the transitions and offsets saved. If
+ * this also fails, it looks for a matching ID among the favorite ones.
+ * Otherwise it looks for it among all available IDs with the same basic
+ * offset.
+ *
+ * @param suggested the suggested ID (as a string)
+ * @return a string containing the Olson ID or null if no matching ID is
+ * found
+ */
public String toID(String suggested) {
- if (matchesID(suggested)) {
+
+ if (cachedID) {
+ return id;
+ }
+ String extractedID = extractID(name);
+ if (extractedID != null) {
+ return cacheID(extractedID);
+ }
+ if ((suggested != null) && (matchesID(suggested))) {
return cacheID(suggested);
- } else {
- return toID();
}
+ return toID();
}
+ /**
+ * Gets an Olson ID corresponding to the information saved and a
suggestion.
+ * First it looks for a cached ID. If it is not found, it checks if the
+ * saved name simply contains an Olson ID. If it does, it will be
returned
+ * as the result without further investigation. If that is not the case,
the
+ * ID of the suggested time zone is checked against the transitions and
+ * offsets saved. If this also fails, it looks for a matching ID among
the
+ * favorite ones. Otherwise it looks for it among all available IDs with
the
+ * same basic offset.
+ *
+ * @param suggested the suggested time zone (as a TimeZone object)
+ * @return a string containing the Olson ID or null if no matching ID is
+ * found
+ */
public String toID(TimeZone suggested) {
-
- if ((suggested != null) && (matchesID(suggested.getID()))) {
- return cacheID(suggested.getID());
- } else {
- return toID();
+
+ if (suggested != null) {
+ return toID(suggested.getID());
}
+ return toID((String) null);
}
public Property getTZ() {
@@ -526,7 +597,7 @@
public VTimezone getVTimezone() {
- return toVTimezone(getICalendarTransitions(),
getName(),getBasicOffset());
+ return toVTimezone(getICalendarTransitions(), getName(),
getBasicOffset());
}
public String getSIF() {
@@ -718,7 +789,11 @@
if (l > j + 1) {
l = j + 1; // l is now the best acceptable end for a
// combined start-end
series
+ } else {
+ j = l - 1;
}
+ // At this point, l + 1 = j
+
if (l > i + 1) { // more than one year: there's a recurrence
if (summerTimeRDates != null) {
vtz.addComponent(summerTimeRDates);
@@ -1108,8 +1183,8 @@
}
private static boolean areHalfYearFar(long time0, long time1) {
- return ((time1 - time0 < SEVEN_MONTHS) &&
- (time1 - time0 > FIVE_MONTHS ));
+ return ((time1 - time0 < NINE_MONTHS ) &&
+ (time1 - time0 > THREE_MONTHS));
}
private void setFormattersToUTC() {
@@ -1155,4 +1230,24 @@
}
return closeXMLTag(openXMLTag(buffer, tag).append(content), tag);
}
+
+ /**
+ * Looks for a substring that corresponds to an Olson ID.
+ *
+ * @param label the string to search through
+ * @return the substring that represents an Olson ID
+ */
+ private String extractID(String label) {
+ Matcher matcher = OLSON_ID_PATTERN.matcher(label);
+ if (matcher.find()) {
+ String id = matcher.group();
+ try {
+ DateTimeZone.forID(id); // just to check whether it exists
+ } catch (IllegalArgumentException e) { // not found
+ return null;
+ }
+ return id;
+ }
+ return null;
+ }
}
\ No newline at end of file
| <-- Date Index --> | <-- Thread Index --> |
Powered by MHonArc.
Copyright © 2006-2007, OW2 Consortium | contact | webmaster.