You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlbeans.apache.org by da...@apache.org on 2003/11/04 17:09:00 UTC
cvs commit: xml-xmlbeans/v2/test/src/drt/drtcases GDateTests.java
davidbau 2003/11/04 08:09:00
Modified: v1/src/xmlpublic/org/apache/xmlbeans GDateBuilder.java
v1/test/src/drt/drtcases GDateTests.java
v2/src/xmlpublic/org/apache/xmlbeans GDateBuilder.java
v2/test/src/drt/drtcases GDateTests.java
Log:
Added a single-argument form of setTimeZone and normalizeTimeZone
utility methods on GDateBuilder. The single argument is the number
of minutes in the offset, and can be between -840 (-14 hours) and
840 (+14 hours). This allows you to avoid the clumsiness of setting
the "tzsign", "tzhours", "tzminutes" fields separately.
Code review: cezar
Regression tests: one added, passed
Revision Changes Path
1.3 +36 -0 xml-xmlbeans/v1/src/xmlpublic/org/apache/xmlbeans/GDateBuilder.java
Index: GDateBuilder.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v1/src/xmlpublic/org/apache/xmlbeans/GDateBuilder.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- GDateBuilder.java 24 Sep 2003 23:31:13 -0000 1.2
+++ GDateBuilder.java 4 Nov 2003 16:09:00 -0000 1.3
@@ -497,6 +497,24 @@
_tzh = tzHour;
_tzm = tzMinute;
}
+
+ /**
+ * Sets the time zone based on a number of offset minutes rather
+ * than sign/hour/minute; for example, setTimeZone(-60) is the
+ * same as setTimeZone(-1, 1, 0).
+ */
+ public void setTimeZone(int tzTotalMinutes)
+ {
+ if (tzTotalMinutes < -14 * 60 || tzTotalMinutes > 14 * 60)
+ throw new IllegalArgumentException("time zone out of range (-840 to 840 minutes). (" + tzTotalMinutes + ")");
+
+ int tzSign = tzTotalMinutes < 0 ? -1 : tzTotalMinutes > 0 ? 1 : 0;
+ tzTotalMinutes *= tzSign;
+ int tzH = tzTotalMinutes / 60;
+ int tzM = tzTotalMinutes - tzH * 60;
+
+ setTimeZone(tzSign, tzH, tzM);
+ }
/**
* Clears the year. After clearing, hasYear returns false and the
@@ -687,6 +705,24 @@
_tzh = tzHour;
_tzm = tzMinute;
addDuration(1, 0, 0, 0, hshift, mshift, 0, null);
+ }
+
+ /**
+ * Normalizes to a time zone specified by a number of offset minutes rather
+ * than sign/hour/minute; for example, normalizeToTimeZone(-60) is the
+ * same as normalizeToTimeZone(-1, 1, 0).
+ */
+ public void normalizeToTimeZone(int tzTotalMinutes)
+ {
+ if (tzTotalMinutes < -14 * 60 || tzTotalMinutes > 14 * 60)
+ throw new IllegalArgumentException("time zone out of range (-840 to 840 minutes). (" + tzTotalMinutes + ")");
+
+ int tzSign = tzTotalMinutes < 0 ? -1 : tzTotalMinutes > 0 ? 1 : 0;
+ tzTotalMinutes *= tzSign;
+ int tzH = tzTotalMinutes / 60;
+ int tzM = tzTotalMinutes - tzH * 60;
+
+ normalizeToTimeZone(tzSign, tzH, tzM);
}
1.3 +11 -0 xml-xmlbeans/v1/test/src/drt/drtcases/GDateTests.java
Index: GDateTests.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v1/test/src/drt/drtcases/GDateTests.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- GDateTests.java 24 Sep 2003 23:31:17 -0000 1.2
+++ GDateTests.java 4 Nov 2003 16:09:00 -0000 1.3
@@ -482,6 +482,17 @@
Assert.assertEquals(2, new GDate("23:59:59").compareToGDate(new GDate("00:00:00-10:00")));
Assert.assertEquals(1, new GDate("23:59:59").compareToGDate(new GDate("00:00:00+14:00")));
}
+
+ public static void testAPI() throws Exception
+ {
+ GDateBuilder builder = new GDateBuilder("1970-12-20T04:14:22Z");
+ builder.setTimeZone(615);
+ Assert.assertEquals("1970-12-20T04:14:22+10:15", builder.toString());
+ builder.setTimeZone(-345);
+ Assert.assertEquals("1970-12-20T04:14:22-05:45", builder.toString());
+ builder.normalizeToTimeZone(-300);
+ Assert.assertEquals("1970-12-20T04:59:22-05:00", builder.toString());
+ }
public static void testFailure() throws Exception
1.2 +36 -0 xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/GDateBuilder.java
Index: GDateBuilder.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/GDateBuilder.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- GDateBuilder.java 26 Sep 2003 21:23:34 -0000 1.1
+++ GDateBuilder.java 4 Nov 2003 16:09:00 -0000 1.2
@@ -497,6 +497,24 @@
_tzh = tzHour;
_tzm = tzMinute;
}
+
+ /**
+ * Sets the time zone based on a number of offset minutes rather
+ * than sign/hour/minute; for example, setTimeZone(-60) is the
+ * same as setTimeZone(-1, 1, 0).
+ */
+ public void setTimeZone(int tzTotalMinutes)
+ {
+ if (tzTotalMinutes < -14 * 60 || tzTotalMinutes > 14 * 60)
+ throw new IllegalArgumentException("time zone out of range (-840 to 840 minutes). (" + tzTotalMinutes + ")");
+
+ int tzSign = tzTotalMinutes < 0 ? -1 : tzTotalMinutes > 0 ? 1 : 0;
+ tzTotalMinutes *= tzSign;
+ int tzH = tzTotalMinutes / 60;
+ int tzM = tzTotalMinutes - tzH * 60;
+
+ setTimeZone(tzSign, tzH, tzM);
+ }
/**
* Clears the year. After clearing, hasYear returns false and the
@@ -687,6 +705,24 @@
_tzh = tzHour;
_tzm = tzMinute;
addDuration(1, 0, 0, 0, hshift, mshift, 0, null);
+ }
+
+ /**
+ * Normalizes to a time zone specified by a number of offset minutes rather
+ * than sign/hour/minute; for example, normalizeToTimeZone(-60) is the
+ * same as normalizeToTimeZone(-1, 1, 0).
+ */
+ public void normalizeToTimeZone(int tzTotalMinutes)
+ {
+ if (tzTotalMinutes < -14 * 60 || tzTotalMinutes > 14 * 60)
+ throw new IllegalArgumentException("time zone out of range (-840 to 840 minutes). (" + tzTotalMinutes + ")");
+
+ int tzSign = tzTotalMinutes < 0 ? -1 : tzTotalMinutes > 0 ? 1 : 0;
+ tzTotalMinutes *= tzSign;
+ int tzH = tzTotalMinutes / 60;
+ int tzM = tzTotalMinutes - tzH * 60;
+
+ normalizeToTimeZone(tzSign, tzH, tzM);
}
1.2 +11 -0 xml-xmlbeans/v2/test/src/drt/drtcases/GDateTests.java
Index: GDateTests.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/test/src/drt/drtcases/GDateTests.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- GDateTests.java 26 Sep 2003 21:23:40 -0000 1.1
+++ GDateTests.java 4 Nov 2003 16:09:00 -0000 1.2
@@ -482,6 +482,17 @@
Assert.assertEquals(2, new GDate("23:59:59").compareToGDate(new GDate("00:00:00-10:00")));
Assert.assertEquals(1, new GDate("23:59:59").compareToGDate(new GDate("00:00:00+14:00")));
}
+
+ public static void testAPI() throws Exception
+ {
+ GDateBuilder builder = new GDateBuilder("1970-12-20T04:14:22Z");
+ builder.setTimeZone(615);
+ Assert.assertEquals("1970-12-20T04:14:22+10:15", builder.toString());
+ builder.setTimeZone(-345);
+ Assert.assertEquals("1970-12-20T04:14:22-05:45", builder.toString());
+ builder.normalizeToTimeZone(-300);
+ Assert.assertEquals("1970-12-20T04:59:22-05:00", builder.toString());
+ }
public static void testFailure() throws Exception
---------------------------------------------------------------------
To unsubscribe, e-mail: xmlbeans-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-cvs-help@xml.apache.org