diff --git a/CHANGELOG.md b/CHANGELOG.md index 628530960..8ea10a1ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## 4.10.0 [unreleased] +### Bug Fixes +1. [#442](https://github.com/influxdata/influxdb-client-csharp/pull/442): Allow to use `DateTimeKind.Local` and `DateTimeKind.Local` as a timestamp for Data point + ### Dependencies Update dependencies: diff --git a/Client.Test/PointDataTest.cs b/Client.Test/PointDataTest.cs index 3cc8ca87e..7ada8fec8 100644 --- a/Client.Test/PointDataTest.cs +++ b/Client.Test/PointDataTest.cs @@ -290,15 +290,37 @@ public void DateTimeFormatting() } [Test] - public void DateTimeUtc() + public void DateTimeUnspecified() { - var dateTime = new DateTime(2015, 10, 15, 8, 20, 15); + var dateTime = new DateTime(2015, 10, 15, 8, 20, 15, DateTimeKind.Unspecified); var point = PointData.Measurement("h2o") .Tag("location", "europe") - .Field("level", 2); + .Field("level", 2) + .Timestamp(dateTime, WritePrecision.Ms); + + Assert.AreEqual("h2o,location=europe level=2i 1444897215000", point.ToLineProtocol()); + } + + [Test] + public void DateTimeLocal() + { + var dateTime = new DateTime(2015, 10, 15, 8, 20, 15, DateTimeKind.Local); + + var point = PointData.Measurement("h2o") + .Tag("location", "europe") + .Field("level", 2) + .Timestamp(dateTime, WritePrecision.Ms); + + var lineProtocolLocal = point.ToLineProtocol(); + + point = PointData.Measurement("h2o") + .Tag("location", "europe") + .Field("level", 2) + .Timestamp(TimeZoneInfo.ConvertTimeToUtc(dateTime), WritePrecision.Ms); + var lineProtocolUtc = point.ToLineProtocol(); - Assert.Throws(() => point.Timestamp(dateTime, WritePrecision.Ms)); + Assert.AreEqual(lineProtocolUtc, lineProtocolLocal); } [Test] diff --git a/Client.Test/WriteApiTest.cs b/Client.Test/WriteApiTest.cs index 71a44fb45..ec2b3fc3a 100644 --- a/Client.Test/WriteApiTest.cs +++ b/Client.Test/WriteApiTest.cs @@ -494,14 +494,14 @@ public void WriteRuntimeException() { Time = new DateTime(2020, 11, 15, 8, 20, 15), Device = "id-1", - Value = 15 + Value = -1 }; _writeApi.WriteMeasurement(measurement, WritePrecision.S, "b1", "org1"); var error = listener.Get(); Assert.IsNotNull(error); - StringAssert.StartsWith("Timestamps must be specified as UTC", error.Exception.Message); + StringAssert.StartsWith("Something is wrong", error.Exception.InnerException?.Message); Assert.AreEqual(0, MockServer.LogEntries.Count()); } @@ -604,10 +604,17 @@ public void WritesToDifferentBucketsJitter() [Measurement("m")] public class SimpleModel { + private int _value; + [Column(IsTimestamp = true)] public DateTime Time { get; set; } [Column("device", IsTag = true)] public string Device { get; set; } - [Column("value")] public int Value { get; set; } + [Column("value")] + public int Value + { + get => _value == -1 ? throw new ArgumentException("Something is wrong") : _value; + set => _value = value; + } } } \ No newline at end of file diff --git a/Client/Writes/PointData.cs b/Client/Writes/PointData.cs index ebabc8597..10f06d82d 100644 --- a/Client/Writes/PointData.cs +++ b/Client/Writes/PointData.cs @@ -252,12 +252,14 @@ public PointData Timestamp(TimeSpan timestamp, WritePrecision timeUnit) /// public PointData Timestamp(DateTime timestamp, WritePrecision timeUnit) { - if (timestamp != null && timestamp.Kind != DateTimeKind.Utc) + var utcTimestamp = timestamp.Kind switch { - throw new ArgumentException("Timestamps must be specified as UTC", nameof(timestamp)); - } + DateTimeKind.Local => timestamp.ToUniversalTime(), + DateTimeKind.Unspecified => DateTime.SpecifyKind(timestamp, DateTimeKind.Utc), + var _ => timestamp + }; - var timeSpan = timestamp.Subtract(EpochStart); + var timeSpan = utcTimestamp.Subtract(EpochStart); return Timestamp(timeSpan, timeUnit); } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy