influxdata / influxdb-client-python

InfluxDB 2.0 python client
https://influxdb-client.readthedocs.io/en/stable/
MIT License
706 stars 185 forks source link

Timezone location does not work #647

Closed tendrillion closed 4 months ago

tendrillion commented 5 months ago

Specifications

Code sample to reproduce problem

    query_api = client.query_api()

    query = """
    import "date"
    import "timezone"
    option location = timezone.location(name: "Asia/Shanghai")
    from(bucket: "pandas")
    |> range(start: -2d)
    |> filter(fn: (r) => r["_measurement"] == "random")
    |> filter(fn: (r) =>r._field == "A")
    """
    tables = query_api.query(query, org="python")
    results = []

    for table in tables:
        for record in table.records:
            results.append((record.get_field(), record.get_value()))
            print(record)

Expected behavior

option location = timezone.location(name: "Asia/Shanghai") In the query result, the time should be in the local time zone

Actual behavior

Actual result: 1\ timezone:tzinfo=tzutc(),not the local timezone

FluxRecord() table: 0, {'result': '_result', 'table': 0, '_start': datetime.datetime(2024, 3, 29, 6, 28, 0, 629809, tzinfo=tzutc()), '_stop': datetime.datetime(2024, 3, 31, 6, 28, 0, 629809, tzinfo=tzutc()), '_time': datetime.datetime(2024, 3, 30, 1, 0, 25, tzinfo=tzutc()), '_value': 0.16534282957903657, '_field': 'B', '_measurement': 'random', 'tag': 'test_c'}

2\ 图片

In the source data, the time zone is the local time zone, but the query result is not the local time zone

3\ i start docker with -v /etc/localtime:/etc/localtime ,but it did not work also.

why timezone location does not work? How can the query result return the local time zone?

bednar commented 5 months ago

Hi @tendrillion,

Thank you for using our client and reaching out with your question.

The timezone.location operator in Flux is designed to set the timezone context for time-based functions, like range, window, and others within your Flux queries. This operator allows you to specify the timezone in which these operations should interpret the time values.

It's important to note that while you can set the timezone for the query's execution context, the actual data returned by InfluxDB queries is always in UTC timezone. Therefore, any transformation to adjust the results to your local timezone or any other timezone must be performed after you receive the data.

If you have any more questions or need further assistance, please don't hesitate to ask.

Regards.

tendrillion commented 5 months ago

Hi @tendrillion,

Thank you for using our client and reaching out with your question.

The timezone.location operator in Flux is designed to set the timezone context for time-based functions, like range, window, and others within your Flux queries. This operator allows you to specify the timezone in which these operations should interpret the time values.

It's important to note that while you can set the timezone for the query's execution context, the actual data returned by InfluxDB queries is always in UTC timezone. Therefore, any transformation to adjust the results to your local timezone or any other timezone must be performed after you receive the data.

If you have any more questions or need further assistance, please don't hesitate to ask.

Regards.

Please note that I have converted the data to the local time zone, GMT+8, before writing it to influxdb,, but the query result is not the local time zone。 图片

bednar commented 5 months ago

The data you retrieve from InfluxDB through queries is indeed in UTC by default. When you see timezone-adjusted results in the InfluxDB UI, it's the web interface applying those adjustments for display purposes, not the server altering the data before it's sent.

To work with query results in a different timezone within your Python application, you can utilize the DateHelper utility from the InfluxDB Python client. This tool allows you to convert datetime objects to your preferred timezone after receiving the data.

Here's an excerpt from the documentation on how to configure and use DateHelper for this purpose: DateHelper Documentation.

By integrating DateHelper into your application, you can seamlessly transform UTC results into any timezone you require, directly within your client code.