Skip to content

Commit

Permalink
refactor: to timezone specific datetime helper to avoid use deprecate…
Browse files Browse the repository at this point in the history
…d functions
  • Loading branch information
bednar committed May 2, 2024
1 parent 550ff26 commit 890cfc9
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 24 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ The batching is configurable by `write_options`:
| **exponential_base** | the base for the exponential retry delay, the next delay is computed using random exponential backoff as a random value within the interval `retry_interval * exponential_base^(attempts-1)` and `retry_interval * exponential_base^(attempts)`. Example for `retry_interval=5_000, exponential_base=2, max_retry_delay=125_000, total=5` Retry delays are random distributed values within the ranges of `[5_000-10_000, 10_000-20_000, 20_000-40_000, 40_000-80_000, 80_000-125_000]` | `2` |

``` python
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

import pandas as pd
import reactivex as rx
Expand Down Expand Up @@ -456,7 +456,7 @@ with InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org")
"""
Write Pandas DataFrame
"""
_now = datetime.utcnow()
_now = datetime.now(tz=timezone.utc)
_data_frame = pd.DataFrame(data=[["coyote_creek", 1.0], ["coyote_creek", 2.0]],
index=[_now, _now + timedelta(hours=1)],
columns=["location", "water_level"])
Expand Down Expand Up @@ -923,7 +923,7 @@ The last step is run a python script via: `python3 influx_cloud.py`.
Connect to InfluxDB 2.0 - write data and query them
"""

from datetime import datetime
from datetime import datetime, timezone

from influxdb_client import Point, InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
Expand All @@ -945,7 +945,7 @@ try:
"""
Write data by Point structure
"""
point = Point(kind).tag('host', host).tag('device', device).field('value', 25.3).time(time=datetime.utcnow())
point = Point(kind).tag('host', host).tag('device', device).field('value', 25.3).time(time=datetime.now(tz=timezone.utc))

print(f'Writing to InfluxDB cloud: {point.to_line_protocol()} ...')

Expand Down Expand Up @@ -1407,7 +1407,7 @@ The `influxdb_client.client.query_api_async.QueryApiAsync` supports retrieve dat
>
> async def main():
> async with InfluxDBClientAsync(url="http://localhost:8086", token="my-token", org="my-org") as client:
> start = datetime.utcfromtimestamp(0)
> start = datetime.fromtimestamp(0)
> stop = datetime.now()
> # Delete data with location = 'Prague'
> successfully = await client.delete_api().delete(start=start, stop=stop, bucket="my-bucket",
Expand Down
2 changes: 1 addition & 1 deletion examples/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async def main():
Delete data
"""
print(f"\n------- Delete data with location = 'Prague' -------\n")
successfully = await client.delete_api().delete(start=datetime.utcfromtimestamp(0), stop=datetime.now(),
successfully = await client.delete_api().delete(start=datetime.fromtimestamp(0), stop=datetime.now(),
predicate="location = \"Prague\"", bucket="my-bucket")
print(f" > successfully: {successfully}")

Expand Down
6 changes: 3 additions & 3 deletions examples/example.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import codecs
from datetime import datetime
from datetime import datetime, timezone

from influxdb_client import WritePrecision, InfluxDBClient, Point
from influxdb_client.client.write_api import SYNCHRONOUS

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

p = Point("my_measurement").tag("location", "Prague").field("temperature", 25.3).time(datetime.utcnow(),
WritePrecision.MS)
p = Point("my_measurement").tag("location", "Prague").field("temperature", 25.3) \
.time(datetime.now(tz=timezone.utc), WritePrecision.MS)
write_api = client.write_api(write_options=SYNCHRONOUS)

# write using point structure
Expand Down
5 changes: 3 additions & 2 deletions examples/influx_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Connect to InfluxDB 2.0 - write data and query them
"""

from datetime import datetime
from datetime import datetime, timezone

from influxdb_client import Point, InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
Expand All @@ -23,7 +23,8 @@
"""
Write data by Point structure
"""
point = Point(kind).tag('host', host).tag('device', device).field('value', 25.3).time(time=datetime.utcnow())
point = Point(kind).tag('host', host).tag('device', device).field('value', 25.3) \
.time(time=datetime.now(tz=timezone.utc))

print(f'Writing to InfluxDB cloud: {point.to_line_protocol()} ...')

Expand Down
2 changes: 1 addition & 1 deletion examples/logging_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def use_logger():
Point('my-measurement')
.tag('host', 'host1')
.field('temperature', 25.3)
.time(datetime.datetime.utcnow(), WritePrecision.MS)
.time(datetime.datetime.now(tz=datetime.timezone.utc), WritePrecision.MS)
)


Expand Down
4 changes: 2 additions & 2 deletions examples/write_structured_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import namedtuple
from dataclasses import dataclass
from datetime import datetime
from datetime import datetime, timezone

from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
Expand Down Expand Up @@ -37,7 +37,7 @@ class Car:
version="2021.06.05.5874",
pressure=125,
temperature=10,
timestamp=datetime.utcnow())
timestamp=datetime.now(tz=timezone.utc))
print(sensor)

"""
Expand Down
2 changes: 1 addition & 1 deletion influxdb_client/client/write/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from influxdb_client.client.util.date_utils import get_date_helper
from influxdb_client.domain.write_precision import WritePrecision

EPOCH = datetime.utcfromtimestamp(0).replace(tzinfo=timezone.utc)
EPOCH = datetime.fromtimestamp(0, tz=timezone.utc)

DEFAULT_WRITE_PRECISION = WritePrecision.NS

Expand Down
11 changes: 6 additions & 5 deletions tests/test_InfluxDBClientAsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import unittest
import os
from datetime import datetime
from datetime import datetime, timezone
from io import StringIO

import pytest
Expand Down Expand Up @@ -202,11 +202,11 @@ async def test_write_empty_data(self):
async def test_write_points_different_precision(self):
measurement = generate_name("measurement")
_point1 = Point(measurement).tag("location", "Prague").field("temperature", 25.3) \
.time(datetime.utcfromtimestamp(0), write_precision=WritePrecision.S)
.time(datetime.fromtimestamp(0, tz=timezone.utc), write_precision=WritePrecision.S)
_point2 = Point(measurement).tag("location", "New York").field("temperature", 24.3) \
.time(datetime.utcfromtimestamp(1), write_precision=WritePrecision.MS)
.time(datetime.fromtimestamp(1, tz=timezone.utc), write_precision=WritePrecision.MS)
_point3 = Point(measurement).tag("location", "Berlin").field("temperature", 24.3) \
.time(datetime.utcfromtimestamp(2), write_precision=WritePrecision.NS)
.time(datetime.fromtimestamp(2, tz=timezone.utc), write_precision=WritePrecision.NS)
await self.client.write_api().write(bucket="my-bucket", record=[_point1, _point2, _point3],
write_precision=WritePrecision.NS)
query = f'''
Expand All @@ -228,7 +228,8 @@ async def test_delete_api(self):
measurement = generate_name("measurement")
await self._prepare_data(measurement)

successfully = await self.client.delete_api().delete(start=datetime.utcfromtimestamp(0), stop=datetime.utcnow(),
successfully = await self.client.delete_api().delete(start=datetime.fromtimestamp(0),
stop=datetime.now(tz=timezone.utc),
predicate="location = \"Prague\"", bucket="my-bucket")
self.assertEqual(True, successfully)
query = f'''
Expand Down
6 changes: 3 additions & 3 deletions tests/test_MultiprocessingWriter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import unittest
from datetime import datetime
from datetime import datetime, timezone

from influxdb_client import WritePrecision, InfluxDBClient
from influxdb_client.client.util.date_utils import get_date_helper
Expand Down Expand Up @@ -53,7 +53,7 @@ def test_use_context_manager(self):
self.assertIsNotNone(writer)

def test_pass_parameters(self):
unique = get_date_helper().to_nanoseconds(datetime.utcnow() - datetime.utcfromtimestamp(0))
unique = get_date_helper().to_nanoseconds(datetime.now(tz=timezone.utc) - datetime.fromtimestamp(0, tz=timezone.utc))

# write data
with MultiprocessingWriter(url=self.url, token=self.token, org=self.org, write_options=SYNCHRONOUS) as writer:
Expand All @@ -69,4 +69,4 @@ def test_pass_parameters(self):
self.assertIsNotNone(record)
self.assertEqual("a", record["tag"])
self.assertEqual(5, record["_value"])
self.assertEqual(get_date_helper().to_utc(datetime.utcfromtimestamp(10)), record["_time"])
self.assertEqual(get_date_helper().to_utc(datetime.fromtimestamp(10, tz=timezone.utc)), record["_time"])
2 changes: 1 addition & 1 deletion tests/test_PandasDateTimeHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_parse_date(self):

def test_to_nanoseconds(self):
date = self.helper.parse_date('2020-08-07T06:21:57.331249158Z').replace(tzinfo=timezone.utc)
nanoseconds = self.helper.to_nanoseconds(date - datetime.utcfromtimestamp(0).replace(tzinfo=timezone.utc))
nanoseconds = self.helper.to_nanoseconds(date - datetime.fromtimestamp(0, tz=timezone.utc))

self.assertEqual(nanoseconds, 1596781317331249158)

Expand Down

0 comments on commit 890cfc9

Please sign in to comment.