From 10786e27052cced50a81ed7455d9f5974aa9dcc8 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Fri, 16 Dec 2022 09:53:28 +0100 Subject: [PATCH 1/4] feat: allow to use `DateTimeKind.Local` and `DateTimeKind.Local` as a timestamp for data point --- Client.Test/PointDataTest.cs | 30 ++++++++++++++++++++++++++---- Client/Writes/PointData.cs | 11 +++++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) 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/Writes/PointData.cs b/Client/Writes/PointData.cs index ebabc8597..862ce4524 100644 --- a/Client/Writes/PointData.cs +++ b/Client/Writes/PointData.cs @@ -252,12 +252,19 @@ 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 + { + DateTimeKind.Local => timestamp.ToUniversalTime(), + DateTimeKind.Unspecified => DateTime.SpecifyKind(timestamp, DateTimeKind.Utc), + var _ => timestamp + }; + + if (utcTimestamp.Kind != DateTimeKind.Utc) { throw new ArgumentException("Timestamps must be specified as UTC", nameof(timestamp)); } - var timeSpan = timestamp.Subtract(EpochStart); + var timeSpan = utcTimestamp.Subtract(EpochStart); return Timestamp(timeSpan, timeUnit); } From c84b3447d7374598c4e13e1d16b3dcf5d566503e Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Fri, 16 Dec 2022 10:15:33 +0100 Subject: [PATCH 2/4] docs: update CHANGELOG.md --- CHANGELOG.md | 3 +++ Client.Test/WriteApiTest.cs | 12 +++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) 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/WriteApiTest.cs b/Client.Test/WriteApiTest.cs index 71a44fb45..ee3174d8f 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,16 @@ 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 From 154e450a5904b9b09cf78506ac447fa74d7a9d71 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Fri, 16 Dec 2022 10:32:03 +0100 Subject: [PATCH 3/4] fix: code style --- Client.Test/WriteApiTest.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Client.Test/WriteApiTest.cs b/Client.Test/WriteApiTest.cs index ee3174d8f..ec2b3fc3a 100644 --- a/Client.Test/WriteApiTest.cs +++ b/Client.Test/WriteApiTest.cs @@ -605,14 +605,15 @@ public void WritesToDifferentBucketsJitter() 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 + [Column("value")] + public int Value { - get => _value == -1 ? throw new ArgumentException("Something is wrong"): _value; + get => _value == -1 ? throw new ArgumentException("Something is wrong") : _value; set => _value = value; } } From 98ebdc3d72be6ec64278b793e131e79c4111e845 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Fri, 16 Dec 2022 13:44:41 +0100 Subject: [PATCH 4/4] feat: allow to use `DateTimeKind.Local` and `DateTimeKind.Local` as a timestamp for data point --- Client/Writes/PointData.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Client/Writes/PointData.cs b/Client/Writes/PointData.cs index 862ce4524..10f06d82d 100644 --- a/Client/Writes/PointData.cs +++ b/Client/Writes/PointData.cs @@ -259,11 +259,6 @@ public PointData Timestamp(DateTime timestamp, WritePrecision timeUnit) var _ => timestamp }; - if (utcTimestamp.Kind != DateTimeKind.Utc) - { - throw new ArgumentException("Timestamps must be specified as UTC", nameof(timestamp)); - } - 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