Timestamps lose precision in Flux Query through C# API

Hi,

[This is my first forum post as I’m still somewhat new to influxdb!]

Whenever I run a query, the returned timestamps are rounded to seconds, but I need them to be more precise.

For example, here are two timestamps as they appear in influx.
2024-08-05T20:34:18.207Z
2024-08-05T20:34:18.827Z

But my Flux query in my code returns them both as

2024-08-05T20:34:18Z

I’m using a Flux query and the C# API. Here’s the query

string query = $"from(bucket:"{defaultBucket}") |> " +
$"range(start: -1d) |> " +
$"filter(fn: (r) => r._measurement == "wave_intensity" and r.run_id == "{runId}") |> " +
$“group(columns: ["_field"])”;

var queryApi = client.GetQueryApi();
var tables = queryApi.QueryAsync(query).Result;

In fact any query I run truncates the timestamp in the same way.

What can I do to retrieve the timestamp in the millisecond precision in which it exists in the database, and why is it being truncated in the first place?

BTW, this is the query in the Data Explorer that was used to get the data to see what the timestamp looked like. I’m only able to use SQL and not Flux.

SELECT *
FROM “wave_intensity”
WHERE
time >= now() - interval ‘1 day’

So could the issue by a Flux vs SQL thing?

Thanks!
Jason

@JasonJ55 AFAIK, the query API doesn’t truncate any timestamps. All timestamps are returned in nanosecond precision. Any truncation that happens is done client-side and is likely only done for display purposes.

Thanks @scott
In that case, what’s the standard way to get the actual timestamp and not a truncated display version. However I try and finesse “_time” in the FluxRecord, it’s only to the second. I can’t find any examples online of retrieving the timestamp.

AFAIK, clients shouldn’t manipulate any of the timestamps returned in a query. With the SQL query, are you using the C# client or are you using the Data Explorer to execute the query?

I’m using the C# client and a flux query. For the SQL query I use Data Explorer, and that gives the timestamp fully accurate timestamp. The reason I wanted the timestamp through the C# client, and accurate to at least the millisecond, is that I need to know the time when each datapoint was generated and we have many per second during an operation. I’ve solved this by adding a “ticks” field to the written data and use that to hold the time of each datapoint (or more specifically, for each point, the number of ticks elapsed since the start of the operation). Hence, I no longer need the timestamp but was nonetheless curious on how to retrieve it, given that it is, after all, time-series data.

Unfortunately I still haven’t found an answer to this problem.

A Flux query always has the timestamp to the second e.g.
2024-08-05T20:34:18Z

But I need it to be at least to the millisecond e.g.
2024-08-05T20:34:18.827Z

Days of online searching have not turned up anything useful.
Do I set something when I initialize the API client? (although I read that the default precision to nanosecond).
Should I abandon using Flux if this issue is baked in?

Any help would be greatly appreciated.

Thanks.

I’ve found one option that works.

The InfluxDB timestamp is a NodaTime.Instant. This object has no public members, so the exact time cannot be obtained; but it can be converted into a DateTime, through which the full time can be accessed.

NodaTime.Instant nodaTime = (NodaTime.Instant)record.GetValueByKey(“_time”);
DateTime dt = nodaTime.ToDateTimeUtc();
Debug.WriteLine(dt.TimeOfDay);

22:44:05.0521311

Hooray! Milliseconds (and beyond) at last!