influxdata / flux

Flux is a lightweight scripting language for querying databases (like InfluxDB) and working with data. It's part of InfluxDB 1.7 and 2.0, but can be run independently of those.

Home Page:https://influxdata.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

date.time does not work with time zones

ganko-pi opened this issue · comments

Details

  • operating system: 64-bit Windows 10 Pro 22H2 19045.3570
  • Flux version: v0.193.0 (queried with array.from(rows: [{version: runtime.version()}]))
  • installation via InfluxDB v2.7.1

Description

By running date.time with a set time zone the returned result is wrong.

Example

import "array"
import "date"
import "timezone"

time = 2023-11-14T12:00:00Z

array.from(rows: [{time: date.time(t: time)}])
    |> yield(name: "UTC")

option location = timezone.location(name: "America/Los_Angeles")

array.from(rows: [{time: date.time(t: time)}])
    |> yield(name: "America/Los_Angeles")

Result

query time
UTC 2023-11-14T12:00:00.000Z
America/Los_Angeles 2023-11-14T04:00:00.000Z

Expected behaviour

Both queries should return the set time 2023-11-14T12:00:00Z because it is an UTC time which is independent of a time zone.

Observed behaviour

The second query America/Los_Angeles returns 2023-11-14T04:00:00.000Z and is off by eight ours (which is the offset of the set time zone America/Los_Angeles).

@ganko-pi The only thing that the location option affects is how windows are created across time-shift boundaries (daylight savings, British summer time, ect.). It does not change actual timestamps.

To change actual timestamps, you can timeShift() to modify your timestamps by the offset of your timezone.

import "array"
import "date"
import "timezone"

time = 2023-11-14T12:00:00Z

array.from(rows: [{time: date.time(t: time)}])
    |> yield(name: "UTC")

array.from(rows: [{time: date.time(t: time)}])
    |> timeShift(columns: ["time"], duration: -8h) 
    |> yield(name: "America/Los_Angeles")
query time
UTC 2023-11-14T12:00:00Z
America/Los_Angeles 2023-11-14T04:00:00Z

I, as you, feel that the location option should be able to at least help modify timestamps. In this issue, I propose including a tzoffset property in the location record that represents the current offset of the specified timezone. You could then use that with timeShift() to automatically update timestamps based on the current timezone offset.

I am not sure if you understand the issue. A timestamp conform to RFC3339 ending with a Z is an UTC time. So the update of the location should not modify the returned time because it is still UTC (indicated by the Z).

@ganko-pi You're right. I did misunderstand the issue. I didn't read carefully enough. Thanks for clarifying.

This issue has had no recent activity and will be closed soon.