Fix issue 109: ancient years must be dumped with leading zeros
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 59f289f..27110c5 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -7,6 +7,9 @@
</properties>
<body>
<release version="1.9-SNAPSHOT" date="in Mercurial" description="Development">
+ <action dev="py4fun" type="fix" issue="109" due-to="cjalmeida">
+ Fix: ancient years must be dumped with leading zeros (2011-02-19)
+ </action>
<action dev="py4fun" type="remove" due-to="JordanAngold">
Remove unused code in Constructor: Modifier.isAbstract() is not needed any more (2011-02-18)
</action>
diff --git a/src/main/java/org/yaml/snakeyaml/representer/SafeRepresenter.java b/src/main/java/org/yaml/snakeyaml/representer/SafeRepresenter.java
index 8573dc7..fac24b7 100644
--- a/src/main/java/org/yaml/snakeyaml/representer/SafeRepresenter.java
+++ b/src/main/java/org/yaml/snakeyaml/representer/SafeRepresenter.java
@@ -234,6 +234,10 @@
int seconds = calendar.get(Calendar.SECOND); // 0..59
int millis = calendar.get(Calendar.MILLISECOND);
StringBuilder buffer = new StringBuilder(String.valueOf(years));
+ while (buffer.length() < 4) {
+ // ancient years
+ buffer.insert(0, "0");
+ }
buffer.append("-");
if (months < 10) {
buffer.append("0");
diff --git a/src/test/java/examples/jodatime/JodaTimeExampleTest.java b/src/test/java/examples/jodatime/JodaTimeExampleTest.java
index d62407a..6b86756 100644
--- a/src/test/java/examples/jodatime/JodaTimeExampleTest.java
+++ b/src/test/java/examples/jodatime/JodaTimeExampleTest.java
@@ -21,6 +21,7 @@
import junit.framework.TestCase;
+import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.yaml.snakeyaml.Yaml;
@@ -54,4 +55,18 @@
MyBean parsed = (MyBean) loader.load(doc);
assertEquals(etalon, parsed.getDate());
}
+
+ /**
+ * test issue 109
+ */
+ public void test109() throws IOException {
+ Date someDate = new DateMidnight(9, 2, 21).toDate();
+ Yaml yaml = new Yaml();
+ String timestamp = yaml.dump(someDate);
+ assertEquals("0009-02-22T23:40:28Z\n", timestamp);
+ // System.out.println(timestamp);
+ Object o = yaml.load(timestamp);
+ assert o.equals(someDate);
+ }
+
}