Skip to content

feat: allow to use DateTimeKind.Local and DateTimeKind.Local as a timestamp for data point #442

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

Merged
merged 4 commits into from
Dec 16, 2022
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:

Expand Down
30 changes: 26 additions & 4 deletions Client.Test/PointDataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentException>(() => point.Timestamp(dateTime, WritePrecision.Ms));
Assert.AreEqual(lineProtocolUtc, lineProtocolLocal);
}

[Test]
Expand Down
13 changes: 10 additions & 3 deletions Client.Test/WriteApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<WriteRuntimeExceptionEvent>();

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());
}
Expand Down Expand Up @@ -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;
}
}
}
10 changes: 6 additions & 4 deletions Client/Writes/PointData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,14 @@ public PointData Timestamp(TimeSpan timestamp, WritePrecision timeUnit)
/// <returns></returns>
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);
}
Expand Down
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