Skip to content

Commit 2692940

Browse files
committed
Write out timestamp values with full precision.
Fixes #31
1 parent 33e0ae4 commit 2692940

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

doc/release-notes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# NetTopologySuite.IO.GPX Release Notes
22

33
## [0.5.0](https://github.com/NetTopologySuite/NetTopologySuite.IO.GPX/milestone/10)
4-
*Nothing yet...*
4+
- `GpxWriterSettings.TimeZoneInfo` is no longer completely ignored ([#30](https://github.com/NetTopologySuite/NetTopologySuite.IO.GPX/issues/30)).
5+
- Fix an issue where written timestamps were being rounded to the nearest second instead of preserving as many significant digits as `DateTime` allows ([#31](https://github.com/NetTopologySuite/NetTopologySuite.IO.GPX/issues/31)).
56

67
## [0.4.0](https://github.com/NetTopologySuite/NetTopologySuite.IO.GPX/milestone/7)
78
- `GpxReaderSettings` now has an `IgnoreBadDateTime` property to use for ignoring a timestamp value that we cannot parse as an instance of the `DateTime` struct, to work around a `0000-00-00T00:00:00Z` coming from CompeGPS ([#29](https://github.com/NetTopologySuite/NetTopologySuite.IO.GPX/issues/29)).

src/NetTopologySuite.IO.GPX/Helpers.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,10 @@ public static void WriteOptionalGpxElementValue(this XmlWriter writer, string lo
329329
{
330330
if (valueUtc.HasValue)
331331
{
332-
const string Format = "yyyy-MM-ddTHH:mm:ssK";
332+
// Format string is hardcoded, instead of allowing the user to override it, to
333+
// ensure that we always write out timestamps that are valid according to the
334+
// published XML schema.
335+
const string Format = "yyyy-MM-ddTHH:mm:ss.FFFFFFFK";
333336

334337
string text;
335338
if (timeZoneInfo == TimeZoneInfo.Utc)

tests/NetTopologySuite.IO.GPX.Tests/GpxFileTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,5 +323,30 @@ public void TimestampsShouldBeWrittenInGivenTimeZone()
323323
Assert.Contains("1234-05-06T10:38:09+03:30", text);
324324
Assert.Contains("5432-10-10T14:52:33+03:30", text);
325325
}
326+
327+
[Fact]
328+
[Regression]
329+
[GitHubIssue(31)]
330+
public void TimestampsShouldPreserveFractionalSecondsWithinDefinedPrecision()
331+
{
332+
const string GpxText = @"
333+
<gpx xmlns='http://www.topografix.com/GPX/1/1' version='1.1' creator='airbreather'>
334+
<metadata>
335+
<time>1234-05-06T07:08:09.7654321</time>
336+
</metadata>
337+
<wpt lat='0.1' lon='2.3'>
338+
<time>5432-10-10T11:22:33.87654321</time>
339+
</wpt>
340+
<wpt lat='4.5' lon='6.7'>
341+
<time>1111-11-11T11:11:11.12345</time>
342+
</wpt>
343+
</gpx>
344+
";
345+
string text = GpxFile.Parse(GpxText, null). BuildString(null);
346+
347+
Assert.Contains("1234-05-06T07:08:09.7654321Z", text);
348+
Assert.Contains("5432-10-10T11:22:33.8765432Z", text); // DateTime resolution is 100ns, so the value gets rounded to 7 digits
349+
Assert.Contains("1111-11-11T11:11:11.12345Z", text); // don't output extra zeroes
350+
}
326351
}
327352
}

0 commit comments

Comments
 (0)