[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Tue May 29 23:49:32 UTC 2007


gordonp
Tue May 29 19:49:32 EDT 2007
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data
In directory dev.open-bio.org:/tmp/cvs-serv31174/src/main/org/biomoby/shared/data

Modified Files:
	MobyDataDateTime.java 
Log Message:
Updates to fix timezone offsets from Zulu, which are now maintained.  Ditched SimpleDateFormat and replaced with homegrown formatter.
moby-live/Java/src/main/org/biomoby/shared/data MobyDataDateTime.java,1.7,1.8
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataDateTime.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataDateTime.java	2007/04/03 02:27:33	1.7
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataDateTime.java	2007/05/29 23:49:32	1.8
@@ -46,7 +46,7 @@
      * @param stringISO8601 if null, the current local date and time is used
      */
     public MobyDataDateTime(String articleName, String stringISO8601) throws IllegalArgumentException{
-	super(articleName, stringISO8601);
+	super(articleName);
 	setDataType(MobyDataType.getDataType("DateTime"));
 	value = parseISO8601(stringISO8601);
     }
@@ -59,7 +59,6 @@
 	super(articleName, "");
 	setDataType(MobyDataType.getDataType("DateTime"));
 	value = cal;
-	((StringBuffer) super.getObject()).append(toString());  // set string representation in case super.toXML() is called explicitly
     }
 
     public MobyDataDateTime(GregorianCalendar cal){
@@ -171,21 +170,25 @@
 		}
 		boolean plus = tok.equals("+");
 		if (! st.hasMoreTokens()) {
-		    throw new IllegalArgumentException("Missing hour field");
+		    throw new IllegalArgumentException("Missing hour field in timezone offset");
 		}
-		int tzhour = Integer.parseInt(st.nextToken());
-		int tzmin  = 0;
+		String tzhour = st.nextToken();
+		String tzmin  = "00";
 		if (check(st, ":") && (st.hasMoreTokens())) {
-		    tzmin = Integer.parseInt(st.nextToken());
+		    tzmin = st.nextToken();
 		} else {
-		    throw new IllegalArgumentException("Missing minute field");
+		    throw new IllegalArgumentException("Missing minute field in timezone offset");
 		}
 		if (plus) {
-		    calendar.add(Calendar.HOUR, tzhour);
-		    calendar.add(Calendar.MINUTE, tzmin);
+		    calendar.setTimeZone(TimeZone.getTimeZone("GMT+"+tzhour+":"+tzmin));
+		    calendar.add(Calendar.HOUR, Integer.parseInt(tzhour));
+		    calendar.add(Calendar.MINUTE, Integer.parseInt(tzmin));
+		    calendar.set(Calendar.DST_OFFSET, 0);  //ISO8601 does not deal with DST
 		} else {
-		    calendar.add(Calendar.HOUR, -tzhour);
-		    calendar.add(Calendar.MINUTE, -tzmin);
+		    calendar.setTimeZone(TimeZone.getTimeZone("GMT-"+tzhour+":"+tzmin));
+		    calendar.add(Calendar.HOUR, Integer.parseInt("-"+tzhour));
+		    calendar.add(Calendar.MINUTE, Integer.parseInt("-"+tzmin));
+		    calendar.set(Calendar.DST_OFFSET, 0);
 		}
 	    }
 	} catch (NumberFormatException ex) {
@@ -228,39 +231,39 @@
      * <simon at jasmine.org.uk>. Does not yet deal with fractions
      * of seconds.
      *************************************************************************/
-    public static String getString (GregorianCalendar value) {
+    public static String getString (GregorianCalendar cvalue) {
 
-	String timef = "'T'hh:mm:ss";
+	String timef = "'T'HH:mm:ss";
 	String datef = "yyyy-MM-dd";
-	String bothf = "yyyy-MM-dd'T'hh:mm:ss";
+	String bothf = "yyyy-MM-dd'T'HH:mm:ss";
 	boolean doTimeZone = true;
 	
 	String format = bothf;    // initially assume this is a date/time
 	
-	if((value.isSet(Calendar.DAY_OF_MONTH) == false || value.get(Calendar.DAY_OF_MONTH) == 1) &&
-	   (value.isSet(Calendar.MONTH) == false || value.get(Calendar.MONTH) == 0) &&
-	   (value.isSet(Calendar.YEAR) == false || value.get(Calendar.YEAR) == 1970)){
+	if((cvalue.isSet(Calendar.DAY_OF_MONTH) == false || cvalue.get(Calendar.DAY_OF_MONTH) == 1) &&
+	   (cvalue.isSet(Calendar.MONTH) == false || cvalue.get(Calendar.MONTH) == 0) &&
+	   (cvalue.isSet(Calendar.YEAR) == false || cvalue.get(Calendar.YEAR) == 1970)){
 	    // it's highly probable that we're
 	    // looking at a time-of-day.
 	    format = timef;
 	}
 	else{
-	    if((value.isSet(Calendar.HOUR) == false || value.get(Calendar.HOUR) == 0) &&
-	       (value.isSet(Calendar.MINUTE) == false || value.get(Calendar.MINUTE) == 0) &&
-	       (value.isSet(Calendar.SECOND) == false || value.get(Calendar.SECOND) == 0)){
+	    if((cvalue.isSet(Calendar.HOUR) == false || cvalue.get(Calendar.HOUR) == 0) &&
+	       (cvalue.isSet(Calendar.MINUTE) == false || cvalue.get(Calendar.MINUTE) == 0) &&
+	       (cvalue.isSet(Calendar.SECOND) == false || cvalue.get(Calendar.SECOND) == 0)){
 		// It's highly probable that we're looking at a date
 		format = datef;
 		doTimeZone = false;
 	    }
 	}
 	
-	StringBuffer result = new StringBuffer((new SimpleDateFormat(format)).format(value.getTime()));
+	StringBuffer result = new StringBuffer((new SimpleDateFormat(format)).format(cvalue.getTime()));
 	
 	// We don't need to worry about timezone in date only strings but otherwise we do...
 	if (doTimeZone){
-	    int offsetTotalMillis = value.get(java.util.Calendar.ZONE_OFFSET);
+	    int offsetTotalMillis = cvalue.getTimeZone().getOffset(cvalue.getTime().getTime());
 
-	    // UTC
+	    // UTC, a.k.a. Zulu time
 	    if (offsetTotalMillis == 0){ 
 		result.append("Z");
 	    }
@@ -269,24 +272,39 @@
 		DecimalFormat decf = new DecimalFormat("00");
 		
 		int offsetTotalMinutes = offsetTotalMillis/60000;
-		int offsetMinutes = Math.abs( offsetTotalMinutes % 60);
-		int offsetHours = Math.abs( offsetTotalMinutes / 60);
-		String offsetString = "+";
-		
-		if (offsetTotalMinutes < 0){
-		    offsetString = "-";
-		}
-		result.append(offsetString).append(decf.format(offsetHours));
-		
-		if (offsetMinutes > 0){
-		    result.append(decf.format(offsetMinutes));
-		}
+		int offsetMinutes = offsetTotalMinutes % 60;
+		int offsetHours = offsetTotalMinutes / 60;
+		String offsetString = offsetTotalMinutes < 0 ? "-":"+";
+
+		// We will maintain the time zone data, so roll back/forward the clock to GMT, and append
+		// the offset data to the representation
+		cvalue.add(Calendar.MINUTE, -offsetMinutes);
+		cvalue.add(Calendar.HOUR_OF_DAY, -offsetHours);
+		result = new StringBuffer(formatDateTime(cvalue));
+		// Reset, so we don't mess things up permanently for the given calendar
+		cvalue.add(Calendar.MINUTE, offsetMinutes);
+		cvalue.add(Calendar.HOUR_OF_DAY, offsetHours);
+
+		result.append(offsetString).append(decf.format(Math.abs(offsetHours)));
+		result.append(":"+decf.format(Math.abs(offsetMinutes)));
 	    }
 	}
 	
 	return result.toString();
     }
 
+    protected static String formatDateTime(Calendar ctime){
+	DecimalFormat decf = new DecimalFormat("00");
+	return
+	    ctime.get(Calendar.YEAR)+"-"+
+	    decf.format(ctime.get(Calendar.MONTH)+1)+"-"+
+	    decf.format(ctime.get(Calendar.DAY_OF_MONTH))+"T"+
+	    decf.format(ctime.get(Calendar.HOUR_OF_DAY))+":"+
+	    decf.format(ctime.get(Calendar.MINUTE))+":"+
+	    decf.format(ctime.get(Calendar.SECOND));
+    }
+
+
     public Object clone(){
 	MobyDataDateTime copy = new MobyDataDateTime(getName(), getValue());
 	copy.setDataType(getDataType());




More information about the MOBY-guts mailing list