Allow UNIX timestamp parsing in a Timezone
Use Case
I have a configuration:
[[inputs.file]]
files = ["/config/data.csv"]
csv_delimiter = ";"
csv_header_row_count = 1
csv_timestamp_column = "(s)"
csv_timestamp_format = "unix"
csv_timezone = "Europe/Stockholm"
data_format = "csv"
interval = "60s"
which is supposed to read a CSV file with metrics and ingest them.
The CSV file is below:
(s);Temp 1;Temp 2;Temp 3;Temp 4;Temp 5;Temp 6;Temp KB1 Tillopp;Temp 7;Temp 7 RH;Temp 8;Temp 9;Temp 10
1745482082;22.6;23.2;23.2;22.0;23.2;24.5;10.3;20.9;19.0;20.2;23.0;21.7
The reading part works well but there is an issue that the UNIX timestamp is not UTC but local time which causes the resulting metric timestamp to be in future.
I see that the CSV parser has parseTimestamp function which calls ParseTimestamp.
The ParseTimestamp always parses UNIX timestamp in UTC ( parseUnix ).
If this is a necessary feature, I can try to provide a PR.
Expected behavior
It would be nice to parse UNIX timestamps with respect to the provided Timezone similar to how it is done for non-timestamp formats so that the resulting metric time is correct and the behavior for both unix, unix_ms, unix_ns, unix_us and custom time formats are the same.
Actual behavior
UNIX timestamps are always parsed in UTC.
Additional info
An issue https://github.com/influxdata/telegraf/issues/12674 might be related.
@ErmakovDmitriy I'm not sure I'm getting what you are saying. A unix-timestamp (aka "seconds since epoch") is defined in UTC (see definition) so a timestamp does NOT have a timezone different than UTC!
So is this request to convert the UTC timestamp into a formatted local time then?
Hi @srebhan,
I am sorry for long reaction time.
Yes, I know that the UNIX timestamp is in UTC.
The problem is that we have some interestint piece of hardware which allows getting its metrics data via CSV as above but gives the timestamp almost as UNIX timestamp but in "local time". I suppose that the hardware just has no idea that such a thing as "Timezone" exists.
So, if I understand you well, if we can convert the timestamp to a formatted local time (with timezone info from the TZ database), I think, it will fix my problem.
Wouldn't setting the correct time (in UTC) on the device solve the actual problem?
If I understand you correctly, you mean something like:
Current time in my timezone: 10:00 (Sweden, currently UTC+2) UTC time: 08:00 The device should be set to 08:00 as UTC.
If what I wrote above reflects what you offered me to do, then I see one potential issue: the device will show on its display time "08:00" which will confuse people who use it.
Ah bummer, stupid device 😛
Anyway, it should be possible to use the Starlark processor to modify the metric timestamp by 2 hours..
And you are right, the referenced issue is indeed similar, so if you're willing to make a PR for that, that's welcome. (reference both as resolves)
Thank you for your help!
I will take a look, probably in 1 week or a bit later.
I will update this issue then.
Ah, didn't get informed anymore by you, but the PR indeed looks good.
I am sorry about it, I should've updated this issue when I had the PR prepared.