This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi-xmlbeans.git
The following commit(s) were added to refs/heads/trunk by this push:
new cf8bdedd share the GDate canonical-string normalization check and fix
the normalize() javadoc (#164)
cf8bdedd is described below
commit cf8bdedd0f62aa642a51d2e469623d8a81676663
Author: PJ Fanning <[email protected]>
AuthorDate: Wed Nov 11 07:48:06 2026 +0100
share the GDate canonical-string normalization check and fix the
normalize() javadoc (#164)
GDate.ensureCanonicalString and GDateBuilder.canonicalString carried
identical copies of the "does this need normalizing" logic; move it to a
single package-private GDate.needsNormalization helper. Update the
GDateBuilder.normalize() javadoc, which still claimed the timezone is left
as-is for time-less or partial dates, now that a zero offset becomes Z
(#161).
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
src/main/java/org/apache/xmlbeans/GDate.java | 38 +++++++++++++---------
.../java/org/apache/xmlbeans/GDateBuilder.java | 19 ++---------
2 files changed, 25 insertions(+), 32 deletions(-)
diff --git a/src/main/java/org/apache/xmlbeans/GDate.java
b/src/main/java/org/apache/xmlbeans/GDate.java
index 2ee67120..c452c631 100644
--- a/src/main/java/org/apache/xmlbeans/GDate.java
+++ b/src/main/java/org/apache/xmlbeans/GDate.java
@@ -19,7 +19,6 @@ import org.apache.xmlbeans.impl.util.ExceptionUtil;
import org.apache.xmlbeans.impl.util.MathUtil;
import java.math.BigDecimal;
-import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;
@@ -874,21 +873,7 @@ public final class GDate implements GDateSpecification,
java.io.Serializable {
return;
}
- boolean needNormalize =
- (hasTimeZone() && getTimeZoneSign() != 0 && hasTime() &&
- ((hasDay() == hasMonth() && hasDay() == hasYear())));
-
- // +00:00 and -00:00 are the same timezone as Z
- if (!needNormalize && hasTimeZone() && getTimeZoneSign() != 0) {
- needNormalize = (getTimeZoneHour() == 0 && getTimeZoneMinute() ==
0);
- }
-
- if (!needNormalize && getFraction() != null && getFraction().scale() >
0) {
- BigInteger bi = getFraction().unscaledValue();
- needNormalize = (bi.mod(GDateBuilder.TEN).signum() == 0);
- }
-
- if (!needNormalize) {
+ if (!needsNormalization(this)) {
_canonicalString = toString();
} else {
GDateBuilder gdb = new GDateBuilder(this);
@@ -897,6 +882,27 @@ public final class GDate implements GDateSpecification,
java.io.Serializable {
}
}
+ /**
+ * Whether the canonical string of the given date differs from its natural
+ * string, so that it must be built from a normalized copy.
+ */
+ static boolean needsNormalization(GDateSpecification date) {
+ if (date.hasTimeZone() && date.getTimeZoneSign() != 0) {
+ // a time with a complete or empty date is normalized to UTC
+ if (date.hasTime() && date.hasDay() == date.hasMonth() &&
date.hasDay() == date.hasYear()) {
+ return true;
+ }
+ // +00:00 and -00:00 are the same timezone as Z
+ if (date.getTimeZoneHour() == 0 && date.getTimeZoneMinute() == 0) {
+ return true;
+ }
+ }
+
+ // trailing zeros in the fractional seconds are dropped
+ BigDecimal fs = date.getFraction();
+ return fs != null && fs.scale() > 0 &&
fs.unscaledValue().mod(GDateBuilder.TEN).signum() == 0;
+ }
+
/**
* The natural string representation. This represents the information
* that is available, including timezone. For types that correspond
diff --git a/src/main/java/org/apache/xmlbeans/GDateBuilder.java
b/src/main/java/org/apache/xmlbeans/GDateBuilder.java
index 5d4622b2..3758f9c8 100644
--- a/src/main/java/org/apache/xmlbeans/GDateBuilder.java
+++ b/src/main/java/org/apache/xmlbeans/GDateBuilder.java
@@ -629,7 +629,8 @@ public final class GDateBuilder implements
GDateSpecification, java.io.Serializa
* their normal ranges.
* <p>
* If no timezone or no time is specified, or if a partial date is
specified, this
- * method does nothing, and leaves the timezone information as-is.
+ * method leaves the timezone information as-is, except that a zero offset
+ * (+00:00 or -00:00) is replaced by Z.
* <p>
* If a time or time and date is specified, this method normalizes the
timezone
* to UTC.
@@ -1487,21 +1488,7 @@ public final class GDateBuilder implements
GDateSpecification, java.io.Serializa
* information.
*/
public String canonicalString() {
- boolean needNormalize =
- (hasTimeZone() && getTimeZoneSign() != 0 && hasTime() &&
- ((hasDay() == hasMonth() && hasDay() == hasYear())));
-
- // +00:00 and -00:00 are the same timezone as Z
- if (!needNormalize && hasTimeZone() && getTimeZoneSign() != 0) {
- needNormalize = (getTimeZoneHour() == 0 && getTimeZoneMinute() ==
0);
- }
-
- if (!needNormalize && getFraction() != null && getFraction().scale() >
0) {
- BigInteger bi = getFraction().unscaledValue();
- needNormalize = (bi.mod(TEN).signum() == 0);
- }
-
- if (!needNormalize) {
+ if (!GDate.needsNormalization(this)) {
return toString();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]