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

_convert_timestamp assumes passed datetime to be UTC #73

Closed
gooney47 opened this issue Mar 16, 2020 · 5 comments · Fixed by #238
Closed

_convert_timestamp assumes passed datetime to be UTC #73

gooney47 opened this issue Mar 16, 2020 · 5 comments · Fixed by #238
Milestone

Comments

@gooney47
Copy link

This function here

def _convert_timestamp(timestamp, precision=DEFAULT_WRITE_PRECISION):
assumes a passed datetime object to be in UTC format if no timezone is specified. I find this to be highly misleading. From the moment on I set the local timezone when I install an OS I want my system to accept local timezone data from me if I don't specify otherwise.

@bednar bednar added this to the 1.6.0 milestone Mar 16, 2020
@gooney47
Copy link
Author

gooney47 commented Mar 16, 2020

Note that also strings are assumed to be in UTC if no timezone is specified in the string.

@bednar
Copy link
Contributor

bednar commented Mar 17, 2020

Hi @gooney47,

Thanks for using our client.

The InfluxDB 2 requires timestamp as UTC so we assume that a naive object datetime(2009, 11, 10, 23, 0, 0, 123456) is also in UTC.

These data point produce same LineProtocol:

Point.measurement("a")
     .field("val", 1)
     .time(datetime(2009, 11, 10, 23, 0, 0, 123456))
     .to_line_protocol()
a val=1i 1257894000123456000
Point.measurement("a")
     .field("val", 1)
     .time(1257894000123456000)
     .to_line_protocol()
a val=1i 1257894000123456000

If you want to use a OS timezone use something like that: datetime(2009, 11, 10, 23, 0, 0, 123456).astimezone(tzlocal()).

Regards

@gooney47
Copy link
Author

gooney47 commented Mar 17, 2020

I still find this highly misleading for me for instance I was wondering why I couldn't see any data after I added some with giving datetime with local time (no timezone specified). My timezone is +1. Now When I did a range query I couldn't see my data until I just for the sake of trial and error put a stop: 1d (which makes it so that the time range isn't stopped at the current time). The error could have been anywhere in my code at that point and that was just pure guessing since there were no errors.

Let's take a look at how datetimes should be worked with:
"Date and time objects may be categorized as “aware” or “naive.”

With sufficient knowledge of applicable algorithmic and political time adjustments, such as time zone and daylight saving time information, an aware object can locate itself relative to other aware objects. An aware object represents a specific moment in time that is not open to interpretation. 1

A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents Coordinated Universal Time (UTC), local time, or time in some other timezone is purely up to the program, just like it is up to the program whether a particular number represents metres, miles, or mass. Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality." (Source)

This means that naive objects should not be assumed as UTC times, they can really be anything. The proper solution here - in my opinion - is raising an exception if no timezone is specified. Just adding the information into the documentation does not fix the problem - at least in my case - as I wasn't checking the documentation of that function specifically as the error could have been anywhere (database setup, not understanding the query language and what not...).

@bednar
Copy link
Contributor

bednar commented Mar 18, 2020

@gooney47 thanks for detail info

@bednar
Copy link
Contributor

bednar commented May 5, 2021

The default timezone can be changed by:

from dateutil.tz import tzlocal

from influxdb_client import InfluxDBClient
from influxdb_client.client.util import date_utils
from influxdb_client.client.util.date_utils import DateHelper

date_utils.date_helper = DateHelper(timezone=tzlocal())

with InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org", debug=False) as client:
    ...

The issue is fixed in milestone 1.18.0.

If you would like to use dev version then install client by:

pip install git+https://github.com/influxdata/influxdb-client-python.git@master

@bednar bednar added this to the 1.18.0 milestone May 5, 2021
sanand0 added a commit to gramener/gramex that referenced this issue Nov 13, 2022
See influxdata/influxdb-client-python#73
We need a timezone aware datetime.

Python 3.3+ uses .aztimezone(None) to convert to local timezone
https://docs.python.org/3/library/datetime.html#datetime.datetime.astimezone
sanand0 added a commit to gramener/gramex that referenced this issue Nov 13, 2022
* REQ: Remove tzlocal as a dependency

Only InfluxDB requires this, we'll handle it later if needed.

* BUG: Ensure datetime is time-zone aware

See influxdata/influxdb-client-python#73
We need a timezone aware datetime.

Python 3.3+ uses .aztimezone(None) to convert to local timezone
https://docs.python.org/3/library/datetime.html#datetime.datetime.astimezone

* GIT: avoid merge conflict with sa-packaging

Co-authored-by: S Anand <root.node@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants