diff --git a/Amazon.IonDotnet.Tests/Internals/TextWriterJsonTest.cs b/Amazon.IonDotnet.Tests/Internals/TextWriterJsonTest.cs index 45d1953..2187947 100644 --- a/Amazon.IonDotnet.Tests/Internals/TextWriterJsonTest.cs +++ b/Amazon.IonDotnet.Tests/Internals/TextWriterJsonTest.cs @@ -151,6 +151,43 @@ public void TestTimestamp() Assert.AreEqual("{\"value\":\"2010-06-15T03:30:45.0000000-00:00\"}", this.sw.ToString()); } + [DataTestMethod] + [DataRow(Timestamp.Precision.Year, "{\"value\":\"2010T\"}")] + [DataRow(Timestamp.Precision.Month, "{\"value\":\"2010-06T\"}")] + [DataRow(Timestamp.Precision.Day, "{\"value\":\"2010-06-15\"}")] + [DataRow(Timestamp.Precision.Minute, "{\"value\":\"2010-06-15T03:30-00:00\"}")] + public void TestTimestampPrecision(Timestamp.Precision p, string expected) + { + Timestamp ts = new Timestamp(2010, 6, 15, 3, 30, 45, p); + value.SetField("value", factory.NewTimestamp(ts)); + var reader = IonReaderBuilder.Build(value); + jsonWriter.WriteValues(reader); + Assert.AreEqual(expected, this.sw.ToString()); + } + + [TestMethod] + public void TestMinutePrecisionTimestamp() + { + var p = Timestamp.Precision.Minute; + Timestamp ts = new Timestamp(2010, 6, 15, 3, 30, 45, 5 * 60, 0, p); + value.SetField("value", factory.NewTimestamp(ts)); + var reader = IonReaderBuilder.Build(value); + jsonWriter.WriteValues(reader); + Assert.AreEqual("{\"value\":\"2010-06-15T03:30+05:00\"}", this.sw.ToString()); + } + + [TestMethod] + public void TestMinutePrecisionTimestampUtc() + { + var p = Timestamp.Precision.Minute; + Timestamp ts = + new Timestamp(2010, 6, 15, 3, 30, 45, 0, 0, p, DateTimeKind.Utc); + value.SetField("value", factory.NewTimestamp(ts)); + var reader = IonReaderBuilder.Build(value); + jsonWriter.WriteValues(reader); + Assert.AreEqual("{\"value\":\"2010-06-15T03:30Z\"}", this.sw.ToString()); + } + [TestMethod] [ExpectedException(typeof(NotSupportedException))] public void TestClob() diff --git a/Amazon.IonDotnet/Timestamp.cs b/Amazon.IonDotnet/Timestamp.cs index 28501fc..1752977 100644 --- a/Amazon.IonDotnet/Timestamp.cs +++ b/Amazon.IonDotnet/Timestamp.cs @@ -473,9 +473,44 @@ public DateTimeOffset AsDateTimeOffset() public override string ToString() { - return this.DateTimeValue.Kind == DateTimeKind.Unspecified - ? this.DateTimeValue.ToString("O") + "-00:00" - : this.AsDateTimeOffset().ToString("O", System.Globalization.CultureInfo.InvariantCulture); + var culture = System.Globalization.CultureInfo.InvariantCulture; + switch (this.TimestampPrecision) + { + case Precision.Year: + return this.DateTimeValue.ToString("yyyyT", culture); + case Precision.Month: + return this.DateTimeValue.ToString("yyyy-MMT", culture); + case Precision.Day: + return this.DateTimeValue.ToString("yyyy-MM-dd", culture); + case Precision.Minute: + switch (this.DateTimeValue.Kind) + { + case DateTimeKind.Utc: + return this.DateTimeValue.ToString("yyyy-MM-ddTHH:mmK", culture); + case DateTimeKind.Unspecified: + return this.DateTimeValue.ToString("yyyy-MM-ddTHH:mm", culture) + "-00:00"; + case DateTimeKind.Local: + return this.AsDateTimeOffset().ToString("yyyy-MM-ddTHH:mmK", culture); + default: + throw new ArgumentOutOfRangeException(); + } + + case Precision.Second: + switch (this.DateTimeValue.Kind) + { + case DateTimeKind.Utc: + return this.DateTimeValue.ToString("O", culture); + case DateTimeKind.Unspecified: + return this.DateTimeValue.ToString("O", culture) + "-00:00"; + case DateTimeKind.Local: + return this.AsDateTimeOffset().ToString("yyyy-MM-ddTHH:mmK", culture); + default: + throw new ArgumentOutOfRangeException(); + } + + default: + throw new ArgumentOutOfRangeException(); + } } public bool Equals(Timestamp other)