Skip to content

Commit 653af46

Browse files
authored
refactor: to timezone specific datetime helper to avoid use deprecated functions (influxdata#652)
1 parent b844a82 commit 653af46

File tree

11 files changed

+29
-24
lines changed

11 files changed

+29
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## 1.45.0 [unreleased]
22

3+
### Bug Fixes
4+
1. [#652](https://github.com/influxdata/influxdb-client-python/pull/652): Refactor to `timezone` specific `datetime` helpers to avoid use deprecated functions
5+
36
## 1.44.0 [2024-06-24]
47

58
### Features

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ The batching is configurable by `write_options`:
392392
| **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` |
393393

394394
``` python
395-
from datetime import datetime, timedelta
395+
from datetime import datetime, timedelta, timezone
396396

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

926-
from datetime import datetime
926+
from datetime import datetime, timezone
927927

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

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

@@ -1407,7 +1407,7 @@ The `influxdb_client.client.query_api_async.QueryApiAsync` supports retrieve dat
14071407
>
14081408
> async def main():
14091409
> async with InfluxDBClientAsync(url="http://localhost:8086", token="my-token", org="my-org") as client:
1410-
> start = datetime.utcfromtimestamp(0)
1410+
> start = datetime.fromtimestamp(0)
14111411
> stop = datetime.now()
14121412
> # Delete data with location = 'Prague'
14131413
> successfully = await client.delete_api().delete(start=start, stop=stop, bucket="my-bucket",

examples/asynchronous.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async def main():
7676
Delete data
7777
"""
7878
print(f"\n------- Delete data with location = 'Prague' -------\n")
79-
successfully = await client.delete_api().delete(start=datetime.utcfromtimestamp(0), stop=datetime.now(),
79+
successfully = await client.delete_api().delete(start=datetime.fromtimestamp(0), stop=datetime.now(),
8080
predicate="location = \"Prague\"", bucket="my-bucket")
8181
print(f" > successfully: {successfully}")
8282

examples/example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import codecs
2-
from datetime import datetime
2+
from datetime import datetime, timezone
33

44
from influxdb_client import WritePrecision, InfluxDBClient, Point
55
from influxdb_client.client.write_api import SYNCHRONOUS
66

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

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

1414
# write using point structure

examples/influx_cloud.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Connect to InfluxDB 2.0 - write data and query them
33
"""
44

5-
from datetime import datetime
5+
from datetime import datetime, timezone
66

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

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

examples/logging_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def use_logger():
4545
Point('my-measurement')
4646
.tag('host', 'host1')
4747
.field('temperature', 25.3)
48-
.time(datetime.datetime.utcnow(), WritePrecision.MS)
48+
.time(datetime.datetime.now(tz=datetime.timezone.utc), WritePrecision.MS)
4949
)
5050

5151

examples/write_structured_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections import namedtuple
22
from dataclasses import dataclass
3-
from datetime import datetime
3+
from datetime import datetime, timezone
44

55
from influxdb_client import InfluxDBClient
66
from influxdb_client.client.write_api import SYNCHRONOUS
@@ -37,7 +37,7 @@ class Car:
3737
version="2021.06.05.5874",
3838
pressure=125,
3939
temperature=10,
40-
timestamp=datetime.utcnow())
40+
timestamp=datetime.now(tz=timezone.utc))
4141
print(sensor)
4242

4343
"""

influxdb_client/client/write/point.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from influxdb_client.client.util.date_utils import get_date_helper
1111
from influxdb_client.domain.write_precision import WritePrecision
1212

13-
EPOCH = datetime.utcfromtimestamp(0).replace(tzinfo=timezone.utc)
13+
EPOCH = datetime.fromtimestamp(0, tz=timezone.utc)
1414

1515
DEFAULT_WRITE_PRECISION = WritePrecision.NS
1616

tests/test_InfluxDBClientAsync.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
import unittest
44
import os
5-
from datetime import datetime
5+
from datetime import datetime, timezone
66
from io import StringIO
77

88
import pytest
@@ -202,11 +202,11 @@ async def test_write_empty_data(self):
202202
async def test_write_points_different_precision(self):
203203
measurement = generate_name("measurement")
204204
_point1 = Point(measurement).tag("location", "Prague").field("temperature", 25.3) \
205-
.time(datetime.utcfromtimestamp(0), write_precision=WritePrecision.S)
205+
.time(datetime.fromtimestamp(0, tz=timezone.utc), write_precision=WritePrecision.S)
206206
_point2 = Point(measurement).tag("location", "New York").field("temperature", 24.3) \
207-
.time(datetime.utcfromtimestamp(1), write_precision=WritePrecision.MS)
207+
.time(datetime.fromtimestamp(1, tz=timezone.utc), write_precision=WritePrecision.MS)
208208
_point3 = Point(measurement).tag("location", "Berlin").field("temperature", 24.3) \
209-
.time(datetime.utcfromtimestamp(2), write_precision=WritePrecision.NS)
209+
.time(datetime.fromtimestamp(2, tz=timezone.utc), write_precision=WritePrecision.NS)
210210
await self.client.write_api().write(bucket="my-bucket", record=[_point1, _point2, _point3],
211211
write_precision=WritePrecision.NS)
212212
query = f'''
@@ -228,7 +228,8 @@ async def test_delete_api(self):
228228
measurement = generate_name("measurement")
229229
await self._prepare_data(measurement)
230230

231-
successfully = await self.client.delete_api().delete(start=datetime.utcfromtimestamp(0), stop=datetime.utcnow(),
231+
successfully = await self.client.delete_api().delete(start=datetime.fromtimestamp(0),
232+
stop=datetime.now(tz=timezone.utc),
232233
predicate="location = \"Prague\"", bucket="my-bucket")
233234
self.assertEqual(True, successfully)
234235
query = f'''

tests/test_MultiprocessingWriter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import unittest
3-
from datetime import datetime
3+
from datetime import datetime, timezone
44

55
from influxdb_client import WritePrecision, InfluxDBClient
66
from influxdb_client.client.util.date_utils import get_date_helper
@@ -53,7 +53,7 @@ def test_use_context_manager(self):
5353
self.assertIsNotNone(writer)
5454

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

5858
# write data
5959
with MultiprocessingWriter(url=self.url, token=self.token, org=self.org, write_options=SYNCHRONOUS) as writer:
@@ -69,4 +69,4 @@ def test_pass_parameters(self):
6969
self.assertIsNotNone(record)
7070
self.assertEqual("a", record["tag"])
7171
self.assertEqual(5, record["_value"])
72-
self.assertEqual(get_date_helper().to_utc(datetime.utcfromtimestamp(10)), record["_time"])
72+
self.assertEqual(get_date_helper().to_utc(datetime.fromtimestamp(10, tz=timezone.utc)), record["_time"])

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy