Skip to content

Commit 698148e

Browse files
authored
Avoid use of deprecated datetime.utcnow(). (#34572)
* Avoid use of deprecated datetime.utcnow(). * lint
1 parent 38192de commit 698148e

File tree

6 files changed

+13
-11
lines changed

6 files changed

+13
-11
lines changed

sdks/python/apache_beam/examples/cookbook/bigtableio_it_test.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
try:
4242
from google.cloud._helpers import _datetime_from_microseconds
4343
from google.cloud._helpers import _microseconds_from_datetime
44-
from google.cloud._helpers import UTC
4544
from google.cloud.bigtable import row, column_family, Client
4645
except ImportError:
4746
Client = None
@@ -54,7 +53,7 @@
5453

5554
EXISTING_INSTANCES: list['google.cloud.bigtable.instance.Instance'] = []
5655
LABEL_KEY = 'python-bigtable-beam'
57-
label_stamp = datetime.datetime.utcnow().replace(tzinfo=UTC)
56+
label_stamp = datetime.datetime.now(datetime.timezone.utc)
5857
label_stamp_micros = _microseconds_from_datetime(label_stamp)
5958
LABELS = {LABEL_KEY: str(label_stamp_micros)}
6059

@@ -156,7 +155,7 @@ def _delete_old_instances(self):
156155

157156
def age_in_hours(micros):
158157
return (
159-
datetime.datetime.utcnow().replace(tzinfo=UTC) -
158+
datetime.datetime.now(datetime.timezone.utc) -
160159
(_datetime_from_microseconds(micros))).total_seconds() // 3600
161160

162161
CLEAN_INSTANCE = [

sdks/python/apache_beam/examples/online_clustering.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def __init__(self, checkpoints_path: str):
4242

4343
def process(self, model):
4444
# generate ISO 8601
45-
iso_timestamp = datetime.datetime.utcnow().strftime("%Y%m%dT%H%M%SZ")
45+
iso_timestamp = datetime.datetime.now(
46+
datetime.timezone.utc).strftime("%Y%m%dT%H%M%SZ")
4647
checkpoint_name = f'{self.checkpoints_path}/{iso_timestamp}.checkpoint'
4748
latest_checkpoint = f'{self.checkpoints_path}/latest.checkpoint'
4849
# rename previous checkpoint

sdks/python/apache_beam/io/gcp/bigtableio_it_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151

5252
def instance_prefix(instance):
53-
datestr = "".join(filter(str.isdigit, str(datetime.utcnow().date())))
53+
datestr = "".join(filter(str.isdigit, str(datetime.now(timezone.utc).date())))
5454
instance_id = '%s-%s-%s' % (instance, datestr, secrets.token_hex(4))
5555
assert len(instance_id) < 34, "instance id length needs to be within [6, 33]"
5656
return instance_id

sdks/python/apache_beam/metrics/cells.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import threading
2929
import time
3030
from datetime import datetime
31+
from datetime import timezone
3132
from typing import Iterable
3233
from typing import Optional
3334
from typing import Set
@@ -72,7 +73,7 @@ def get_cumulative(self):
7273

7374
def to_runner_api_monitoring_info(self, name, transform_id):
7475
if not self._start_time:
75-
self._start_time = datetime.utcnow()
76+
self._start_time = datetime.now(timezone.utc)
7677
mi = self.to_runner_api_monitoring_info_impl(name, transform_id)
7778
mi.start_time.FromDatetime(self._start_time)
7879
return mi

sdks/python/apache_beam/runners/dataflow/internal/apiclient.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import warnings
4747
from copy import copy
4848
from datetime import datetime
49+
from datetime import timezone
4950

5051
from apitools.base.py import encoding
5152
from apitools.base.py import exceptions
@@ -363,7 +364,7 @@ def _build_default_job_name(user_name):
363364
are removed. If necessary, the user_name is truncated to shorten
364365
the job name to 63 characters."""
365366
user_name = re.sub('[^-a-z0-9]', '', user_name.lower())
366-
date_component = datetime.utcnow().strftime('%m%d%H%M%S-%f')
367+
date_component = datetime.now(timezone.utc).strftime('%m%d%H%M%S-%f')
367368
app_user_name = 'beamapp-{}'.format(user_name)
368369
# append 8 random alphanumeric characters to avoid collisions.
369370
random_component = ''.join(
@@ -467,7 +468,7 @@ def __init__(self, options, proto_pipeline):
467468

468469
# Client Request ID
469470
self.proto.clientRequestId = '{}-{}'.format(
470-
datetime.utcnow().strftime('%Y%m%d%H%M%S%f'),
471+
datetime.now(timezone.utc).strftime('%Y%m%d%H%M%S%f'),
471472
random.randrange(9000) + 1000)
472473

473474
self.base64_str_re = re.compile(r'^[A-Za-z0-9+/]*=*$')

sdks/python/apache_beam/transforms/enrichment_handlers/bigtable_it_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,17 @@ def create_rows(table):
136136
column_family_id,
137137
product_id.encode(),
138138
str(item[product_id]),
139-
timestamp=datetime.datetime.utcnow())
139+
timestamp=datetime.datetime.now(datetime.timezone.utc))
140140
row.set_cell(
141141
column_family_id,
142142
product_name.encode(),
143143
item[product_name],
144-
timestamp=datetime.datetime.utcnow())
144+
timestamp=datetime.datetime.now(datetime.timezone.utc))
145145
row.set_cell(
146146
column_family_id,
147147
product_stock.encode(),
148148
str(item[product_stock]),
149-
timestamp=datetime.datetime.utcnow())
149+
timestamp=datetime.datetime.now(datetime.timezone.utc))
150150
row.commit()
151151

152152

0 commit comments

Comments
 (0)