influxdb-client-python icon indicating copy to clipboard operation
influxdb-client-python copied to clipboard

Timezone location ignored

Open aaalloc opened this issue 2 years ago • 5 comments

Specifications

  • Client Version: 1.36.1
  • InfluxDB Version: 2.7.1
  • Platform: Ubuntu 22.04 LTS

Code sample to reproduce problem

    query = ("""
            import "date"
            import "timezone"
            option location = timezone.location(name: "Europe/Paris")
            from(bucket: "bucket")
            |> range(start: -10m)
            |> filter(fn: (r) => r._measurement == "devices")
            |> filter(fn: (r) =>
                r._field == "temperature"
                or r._field == "humidity"
                or r._field == "devEUI"
                or r._field == "deviceName"
            )
            |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
            |> keep(columns: ["_time", "temperature", "humidity", "deviceName"])
            |> sort(columns: ["_time"], desc: false)
             """
    ) 


    csv_result = readerdb_influx.query_csv(query=query,
                                            dialect=Dialect(header=True, 
                                                            delimiter=",", 
                                                            comment_prefix="#", 
                                                            annotations=[],
                                                            date_time_format="RFC3339")
                                            )
    for csv_lines in csv_result:
        print(csv_lines)
        
    result = readerdb_influx.query(query=query)
    for table in result:
        print(table)
        for record in table.records:
            print(record.values)

Expected behavior

_time should been "+0200" timezone and not in "Z"

Actual behavior

['', 'result', 'table', '_time', 'deviceName', 'humidity', 'temperature'] ['', '_result', '0', '2023-07-10T08:04:15.983755716Z', 'RHF1S001-008', '61.3828125', '27.49101440429687'] ['', '_result', '1', '2023-07-10T08:03:52.513992459Z', 'RHF1S001-009', '61.87109375', '27.437388916015628'] FluxTable() columns: 6, records: 1 {'result': '_result', 'table': 0, '_time': datetime.datetime(2023, 7, 10, 8, 4, 15, 983755, tzinfo=tzutc()), 'deviceName': 'RHF1S001-008', 'humidity': 61.3828125, 'temperature': 27.49101440429687} FluxTable() columns: 6, records: 1 {'result': '_result', 'table': 1, '_time': datetime.datetime(2023, 7, 10, 8, 3, 52, 513992, tzinfo=tzutc()), 'deviceName': 'RHF1S001-009', 'humidity': 61.87109375, 'temperature': 27.437388916015628}

Additional info

I maybe did something wrong ?

aaalloc avatar Jul 10 '23 08:07 aaalloc

Hi @Siirko,

thanks for using our client.

How looks like the raw output of your query in InfluxDB UI?

Regards

bednar avatar Jul 10 '23 08:07 bednar

Hi @Siirko,

thanks for using our client.

How looks like the raw output of your query in InfluxDB UI?

Regards

With a simple request,

import "date"
import "timezone"
option location = timezone.location(name: "Europe/Paris")
from(bucket: "bucket")
  |> range(start: -1h)
  |> filter(fn: (r) => r["_measurement"] == "risinghf_uplink")
  |> filter(fn: (r) =>
                r._field == "temperature"
            )
  |> yield(name: "mean")

I got this image

But using InfluxQL with

> precision rfc3339
> SELECT "temperature" FROM "risinghf_uplink" tz('Europe/Paris')

The time is correctly formatted

aaalloc avatar Jul 10 '23 09:07 aaalloc

https://github.com/influxdata/influxdb-client-python/issues/454#issuecomment-1153858838

from influxdb_client.client.util import date_utils
from influxdb_client.client.util.date_utils import DateHelper
import dateutil.parser
from dateutil import tz

def parse_date(date_string: str):
    return dateutil.parser.parse(date_string).astimezone(tzlocal())

date_utils.date_helper = DateHelper()
date_utils.date_helper.parse_date = parse_date

This helped me to have correct result when using query() function, but not query_csv()

aaalloc avatar Jul 10 '23 09:07 aaalloc

The query_csv returns data as is in InfluxDB response. You have to use your transformation function for datetime columns.

bednar avatar Jul 10 '23 13:07 bednar

The query_csv returns data as is in InfluxDB response. You have to use your transformation function for datetime columns.

This is a "tricks", if I understand correctly what does

option location = timezone.location(name: "Europe/Paris")

in a flux query, I shouldn't be doing like I mentionned in previous answer and get correct date format

aaalloc avatar Jul 10 '23 13:07 aaalloc

option location = timezone.location(name: "Europe/Paris")

Hi @Siirko, sorry for the late.

Setting the location option does not shift timestamps to the specified timezone. All the location option does is change how window boundaries are defined when they cross a time shift (daylight savings, British summer time, etc.).

If you want to shift response from InfluxDB you have to use timeShift() function.

bednar avatar Jul 10 '24 03:07 bednar