Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_time is None when setting timezone through DateHelper #454

Closed
AaronCarton opened this issue Jun 13, 2022 · 4 comments
Closed

_time is None when setting timezone through DateHelper #454

AaronCarton opened this issue Jun 13, 2022 · 4 comments
Labels
documentation Improvements or additions to documentation
Milestone

Comments

@AaronCarton
Copy link

Steps to reproduce:

  1. set timezone as seen in:
    feat: add possibility to specify default timezone for datetimes without tzinfo #238
  2. query something similar as below
  3. returned tables have None as start/stop/time fields
date_utils.date_helper = DateHelper(timezone=tzlocal())
with InfluxDBClient(url=URL, token=TOKEN, org=ORG, timeout=25_000) as client:
   query= f'''import "date"
                   from(bucket: "{BUCKET}")
                      |> range(start: date.truncate(t: now(), unit: 1d), stop: now())
                      |> filter(fn: (r) => r._measurement == "Duiktank")
                      |> aggregateWindow(every: 1h, fn: sum)
                '''
   tables = client.query_api().query(query, org=ORG)

Expected behavior:
Query returns localized time/start/stop

Actual behavior:
Query returns None time/start/stop
image

Specifications:

  • Client Version: 1.29.1
  • InfluxDB Version:
  • Platform: Windows
@AaronCarton
Copy link
Author

Also worth noting that trying to use Flux's timezone function doesn't seem to work with this client either, as it throws an error stating invalid import path timezone

@bednar
Copy link
Contributor

bednar commented Jun 13, 2022

Hi @AaronCarton,

thanks for using our client.

Steps to reproduce:

  1. set timezone as seen in:
    feat: add possibility to specify default timezone for datetimes without tzinfo #238
  2. query something similar as below
  3. returned tables have None as start/stop/time fields
date_utils.date_helper = DateHelper(timezone=tzlocal())
with InfluxDBClient(url=URL, token=TOKEN, org=ORG, timeout=25_000) as client:
   query= f'''import "date"
                   from(bucket: "{BUCKET}")
                      |> range(start: date.truncate(t: now(), unit: 1d), stop: now())
                      |> filter(fn: (r) => r._measurement == "Duiktank")
                      |> aggregateWindow(every: 1h, fn: sum)
                '''
   tables = client.query_api().query(query, org=ORG)

Expected behavior: Query returns localized time/start/stop

Actual behavior: Query returns None time/start/stop image

Specifications:

  • Client Version: 1.29.1
  • InfluxDB Version:
  • Platform: Windows

The #238 is supposed only for writes... I will update the DateHelper docs to clarify how to deal with your requirements. Meanwhile you can use following piece of code:

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

Also worth noting that trying to use Flux's timezone function doesn't seem to work with this client either, as it throws an error stating invalid import path timezone

How looks your query? The following code works for me:

p = {"_start": datetime.timedelta(hours=-1),
     "_location": "Prague",
     "_desc": True,
     "_floatParam": 25.1,
     "_every": datetime.timedelta(minutes=5)
     }

tables = query_api.query('''
    import "timezone"

    timezone.location(name: "America/Los_Angeles")
    
    from(bucket:"my-bucket") |> range(start: _start)
        |> filter(fn: (r) => r["_measurement"] == "my_measurement")
        |> filter(fn: (r) => r["_field"] == "temperature")
        |> filter(fn: (r) => r["location"] == _location and r["_value"] > _floatParam)
        |> aggregateWindow(every: _every, fn: mean, createEmpty: true)        
        |> sort(columns: ["_time"], desc: _desc) 
''', params=p)

Regards

@bednar bednar added the documentation Improvements or additions to documentation label Jun 13, 2022
@AaronCarton
Copy link
Author

AaronCarton commented Jun 13, 2022

Thanks, this fixes part of the problem!

Although when I query data with start: date.truncate(t: now(), unit: 1d) it will still return data starting from 2 am instead of midnight (my timezone is 2 hours ahead of UTC)

I suspect this is because the now() I'm the Flux query still returns UTC instead of localized time? Which might be what https://docs.influxdata.com/flux/v0.x/stdlib/timezone/location/ would fix?

@AaronCarton
Copy link
Author

AaronCarton commented Jun 13, 2022

It seems that import "timezone" is only available on InfluxDB 2.1, and the server I'm querying is still running on 2.0.8, I'll close this issue then 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants