Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #151 - support Timestamp precision in text format #152

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Amazon.IonDotnet.Tests/Internals/TextWriterJsonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
41 changes: 38 additions & 3 deletions Amazon.IonDotnet/Timestamp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading