Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit653af46

Browse files
authored
refactor: to timezone specific datetime helper to avoid use deprecated functions (#652)
1 parentb844a82 commit653af46

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 datetimeimport datetime, timedelta
395+
from datetimeimport datetime, timedelta, timezone
396396

397397
import pandasas pd
398398
import reactivexas 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 datetimeimport datetime
926+
from datetimeimport datetime, timezone
927927

928928
from influxdb_clientimport Point, InfluxDBClient
929929
from influxdb_client.client.write_apiimportSYNCHRONOUS
@@ -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 defmain():
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 = awaitclient.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=awaitclient.delete_api().delete(start=datetime.utcfromtimestamp(0),stop=datetime.now(),
79+
successfully=awaitclient.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
importcodecs
2-
fromdatetimeimportdatetime
2+
fromdatetimeimportdatetime,timezone
33

44
frominfluxdb_clientimportWritePrecision,InfluxDBClient,Point
55
frominfluxdb_client.client.write_apiimportSYNCHRONOUS
66

77
withInfluxDBClient(url="http://localhost:8086",token="my-token",org="my-org",debug=False)asclient:
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-
fromdatetimeimportdatetime
5+
fromdatetimeimportdatetime,timezone
66

77
frominfluxdb_clientimportPoint,InfluxDBClient
88
frominfluxdb_client.client.write_apiimportSYNCHRONOUS
@@ -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
fromcollectionsimportnamedtuple
22
fromdataclassesimportdataclass
3-
fromdatetimeimportdatetime
3+
fromdatetimeimportdatetime,timezone
44

55
frominfluxdb_clientimportInfluxDBClient
66
frominfluxdb_client.client.write_apiimportSYNCHRONOUS
@@ -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
frominfluxdb_client.client.util.date_utilsimportget_date_helper
1111
frominfluxdb_client.domain.write_precisionimportWritePrecision
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
importlogging
33
importunittest
44
importos
5-
fromdatetimeimportdatetime
5+
fromdatetimeimportdatetime,timezone
66
fromioimportStringIO
77

88
importpytest
@@ -202,11 +202,11 @@ async def test_write_empty_data(self):
202202
asyncdeftest_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
awaitself.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
awaitself._prepare_data(measurement)
230230

231-
successfully=awaitself.client.delete_api().delete(start=datetime.utcfromtimestamp(0),stop=datetime.utcnow(),
231+
successfully=awaitself.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
importos
22
importunittest
3-
fromdatetimeimportdatetime
3+
fromdatetimeimportdatetime,timezone
44

55
frominfluxdb_clientimportWritePrecision,InfluxDBClient
66
frominfluxdb_client.client.util.date_utilsimportget_date_helper
@@ -53,7 +53,7 @@ def test_use_context_manager(self):
5353
self.assertIsNotNone(writer)
5454

5555
deftest_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
withMultiprocessingWriter(url=self.url,token=self.token,org=self.org,write_options=SYNCHRONOUS)aswriter:
@@ -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"])

‎tests/test_PandasDateTimeHelper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_parse_date(self):
2323

2424
deftest_to_nanoseconds(self):
2525
date=self.helper.parse_date('2020-08-07T06:21:57.331249158Z').replace(tzinfo=timezone.utc)
26-
nanoseconds=self.helper.to_nanoseconds(date-datetime.utcfromtimestamp(0).replace(tzinfo=timezone.utc))
26+
nanoseconds=self.helper.to_nanoseconds(date-datetime.fromtimestamp(0,tz=timezone.utc))
2727

2828
self.assertEqual(nanoseconds,1596781317331249158)
2929

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp