Skip to content

Commit ac10262

Browse files
chore(samples): update timezone usage (#1284)
Some samples currently use `datetime.datetime.utcnow()`, which raises a deprecation warning. Update samples to use datetime.datetime.now instead Fixes b/457135528
1 parent 95bc5e8 commit ac10262

12 files changed

Lines changed: 43 additions & 50 deletions

File tree

packages/google-cloud-bigtable/docs/classic_client/snippets.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
3030
"""
3131

32-
import datetime
32+
from datetime import datetime, timezone
3333
import pytest
3434

3535
from google.api_core.exceptions import DeadlineExceeded
@@ -39,7 +39,7 @@
3939
from test_utils.system import unique_resource_id
4040
from test_utils.retry import RetryErrors
4141

42-
from google.cloud._helpers import UTC
42+
4343
from google.cloud.bigtable import Client
4444
from google.cloud.bigtable import enums
4545

@@ -57,8 +57,8 @@
5757
STORAGE_TYPE = enums.StorageType.SSD
5858
LABEL_KEY = "python-snippet"
5959
LABEL_STAMP = (
60-
datetime.datetime.utcnow()
61-
.replace(microsecond=0, tzinfo=UTC)
60+
datetime.now(timezone.utc)
61+
.replace(microsecond=0)
6262
.strftime("%Y-%m-%dt%H-%M-%S")
6363
)
6464
LABELS = {LABEL_KEY: str(LABEL_STAMP)}

packages/google-cloud-bigtable/docs/classic_client/snippets_table.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@
2929
3030
"""
3131

32-
import datetime
32+
from datetime import datetime, timezone
3333
import pytest
3434

3535
from google.api_core.exceptions import TooManyRequests
3636
from google.api_core.exceptions import ServiceUnavailable
3737
from test_utils.system import unique_resource_id
3838
from test_utils.retry import RetryErrors
3939

40-
from google.cloud._helpers import UTC
4140
from google.cloud.bigtable import Client
4241
from google.cloud.bigtable import enums
4342
from google.cloud.bigtable import column_family
@@ -54,8 +53,8 @@
5453
STORAGE_TYPE = enums.StorageType.SSD
5554
LABEL_KEY = "python-snippet"
5655
LABEL_STAMP = (
57-
datetime.datetime.utcnow()
58-
.replace(microsecond=0, tzinfo=UTC)
56+
datetime.now(timezone.utc)
57+
.replace(microsecond=0)
5958
.strftime("%Y-%m-%dt%H-%M-%S")
6059
)
6160
LABELS = {LABEL_KEY: str(LABEL_STAMP)}
@@ -179,7 +178,7 @@ def test_bigtable_write_read_drop_truncate():
179178
value = "value_{}".format(i).encode()
180179
row = table.row(row_key)
181180
row.set_cell(
182-
COLUMN_FAMILY_ID, col_name, value, timestamp=datetime.datetime.utcnow()
181+
COLUMN_FAMILY_ID, col_name, value, timestamp=datetime.now(timezone.utc)
183182
)
184183
rows.append(row)
185184
response = table.mutate_rows(rows)
@@ -270,7 +269,7 @@ def test_bigtable_mutations_batcher():
270269
row_key = row_keys[0]
271270
row = table.row(row_key)
272271
row.set_cell(
273-
COLUMN_FAMILY_ID, column_name, "value-0", timestamp=datetime.datetime.utcnow()
272+
COLUMN_FAMILY_ID, column_name, "value-0", timestamp=datetime.now(timezone.utc)
274273
)
275274
batcher.mutate(row)
276275
# Add a collections of rows
@@ -279,7 +278,7 @@ def test_bigtable_mutations_batcher():
279278
row = table.row(row_keys[i])
280279
value = "value_{}".format(i).encode()
281280
row.set_cell(
282-
COLUMN_FAMILY_ID, column_name, value, timestamp=datetime.datetime.utcnow()
281+
COLUMN_FAMILY_ID, column_name, value, timestamp=datetime.now(timezone.utc)
283282
)
284283
rows.append(row)
285284
batcher.mutate_rows(rows)
@@ -759,7 +758,7 @@ def test_bigtable_batcher_mutate_flush_mutate_rows():
759758
row_key = b"row_key_1"
760759
row = table.row(row_key)
761760
row.set_cell(
762-
COLUMN_FAMILY_ID, COL_NAME1, "value-0", timestamp=datetime.datetime.utcnow()
761+
COLUMN_FAMILY_ID, COL_NAME1, "value-0", timestamp=datetime.now(timezone.utc)
763762
)
764763

765764
# In batcher, mutate will flush current batch if it
@@ -967,12 +966,12 @@ def test_bigtable_row_data_cells_cell_value_cell_values():
967966
value = b"value_in_col1"
968967
row = Config.TABLE.row(b"row_key_1")
969968
row.set_cell(
970-
COLUMN_FAMILY_ID, COL_NAME1, value, timestamp=datetime.datetime.utcnow()
969+
COLUMN_FAMILY_ID, COL_NAME1, value, timestamp=datetime.now(timezone.utc)
971970
)
972971
row.commit()
973972

974973
row.set_cell(
975-
COLUMN_FAMILY_ID, COL_NAME1, value, timestamp=datetime.datetime.utcnow()
974+
COLUMN_FAMILY_ID, COL_NAME1, value, timestamp=datetime.now(timezone.utc)
976975
)
977976
row.commit()
978977

@@ -1050,7 +1049,7 @@ def test_bigtable_row_setcell_rowkey():
10501049

10511050
cell_val = b"cell-val"
10521051
row.set_cell(
1053-
COLUMN_FAMILY_ID, COL_NAME1, cell_val, timestamp=datetime.datetime.utcnow()
1052+
COLUMN_FAMILY_ID, COL_NAME1, cell_val, timestamp=datetime.now(timezone.utc)
10541053
)
10551054
# [END bigtable_api_row_set_cell]
10561055

packages/google-cloud-bigtable/samples/hello/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from ..utils import wait_for_table
2929

3030
# [START bigtable_hw_imports]
31-
import datetime
31+
from datetime import datetime, timezone
3232

3333
from google.cloud import bigtable
3434
from google.cloud.bigtable import column_family
@@ -91,7 +91,7 @@ def main(project_id, instance_id, table_id):
9191
row_key = f"greeting{i}".encode()
9292
row = table.direct_row(row_key)
9393
row.set_cell(
94-
column_family_id, column, value, timestamp=datetime.datetime.utcnow(),
94+
column_family_id, column, value, timestamp=datetime.now(timezone.utc),
9595
)
9696
rows.append(row)
9797
table.mutate_rows(rows)

packages/google-cloud-bigtable/samples/snippets/writes/write_batch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
# [START bigtable_writes_batch]
16-
import datetime
16+
from datetime import datetime, timezone
1717

1818
from google.cloud import bigtable
1919
from google.cloud.bigtable.batcher import MutationsBatcher
@@ -25,7 +25,7 @@ def write_batch(project_id, instance_id, table_id):
2525
table = instance.table(table_id)
2626

2727
with MutationsBatcher(table=table) as batcher:
28-
timestamp = datetime.datetime.utcnow()
28+
timestamp = datetime.now(timezone.utc)
2929
column_family_id = "stats_summary"
3030

3131
rows = [

packages/google-cloud-bigtable/samples/snippets/writes/write_conditionally.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
# [START bigtable_writes_conditional]
16-
import datetime
16+
from datetime import datetime, timezone
1717

1818
from google.cloud import bigtable
1919
from google.cloud.bigtable import row_filters
@@ -24,7 +24,7 @@ def write_conditional(project_id, instance_id, table_id):
2424
instance = client.instance(instance_id)
2525
table = instance.table(table_id)
2626

27-
timestamp = datetime.datetime.utcnow()
27+
timestamp = datetime.now(timezone.utc)
2828
column_family_id = "stats_summary"
2929

3030
row_key = "phone#4c410523#20190501"

packages/google-cloud-bigtable/samples/snippets/writes/write_simple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515

1616
# [START bigtable_writes_simple]
17-
import datetime
17+
from datetime import datetime, timezone
1818

1919
from google.cloud import bigtable
2020

@@ -24,7 +24,7 @@ def write_simple(project_id, instance_id, table_id):
2424
instance = client.instance(instance_id)
2525
table = instance.table(table_id)
2626

27-
timestamp = datetime.datetime.utcnow()
27+
timestamp = datetime.now(timezone.utc)
2828
column_family_id = "stats_summary"
2929

3030
row_key = "phone#4c410523#20190501"

packages/google-cloud-bigtable/tests/system/v2_client/_helpers.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import datetime
15+
from datetime import datetime, timezone
1616

1717
import grpc
1818
from google.api_core import exceptions
1919
from google.cloud import exceptions as core_exceptions
20-
from google.cloud._helpers import UTC
2120
from test_utils import retry
2221

2322

@@ -41,7 +40,5 @@ def _retry_on_unavailable(exc):
4140

4241
def label_stamp():
4342
return (
44-
datetime.datetime.utcnow()
45-
.replace(microsecond=0, tzinfo=UTC)
46-
.strftime("%Y-%m-%dt%H-%M-%S")
43+
datetime.now(timezone.utc).replace(microsecond=0).strftime("%Y-%m-%dt%H-%M-%S")
4744
)

packages/google-cloud-bigtable/tests/system/v2_client/test_data_api.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import datetime
15+
from datetime import datetime, timedelta, timezone
1616
import operator
1717

1818
import pytest
@@ -62,8 +62,8 @@ def rows_to_delete():
6262
def test_table_read_rows_filter_millis(data_table):
6363
from google.cloud.bigtable import row_filters
6464

65-
end = datetime.datetime.now()
66-
start = end - datetime.timedelta(minutes=60)
65+
end = datetime.now()
66+
start = end - timedelta(minutes=60)
6767
timestamp_range = row_filters.TimestampRange(start=start, end=end)
6868
timefilter = row_filters.TimestampRangeFilter(timestamp_range)
6969
row_data = data_table.read_rows(filter_=timefilter)
@@ -233,20 +233,19 @@ def test_table_read_row_large_cell(data_table, rows_to_delete, skip_on_emulator)
233233
def _write_to_row(row1, row2, row3, row4):
234234
from google.cloud._helpers import _datetime_from_microseconds
235235
from google.cloud._helpers import _microseconds_from_datetime
236-
from google.cloud._helpers import UTC
237236
from google.cloud.bigtable.row_data import Cell
238237

239-
timestamp1 = datetime.datetime.utcnow().replace(tzinfo=UTC)
238+
timestamp1 = datetime.now(timezone.utc)
240239
timestamp1_micros = _microseconds_from_datetime(timestamp1)
241240
# Truncate to millisecond granularity.
242241
timestamp1_micros -= timestamp1_micros % 1000
243242
timestamp1 = _datetime_from_microseconds(timestamp1_micros)
244243
# 1000 microseconds is a millisecond
245-
timestamp2 = timestamp1 + datetime.timedelta(microseconds=1000)
244+
timestamp2 = timestamp1 + timedelta(microseconds=1000)
246245
timestamp2_micros = _microseconds_from_datetime(timestamp2)
247-
timestamp3 = timestamp1 + datetime.timedelta(microseconds=2000)
246+
timestamp3 = timestamp1 + timedelta(microseconds=2000)
248247
timestamp3_micros = _microseconds_from_datetime(timestamp3)
249-
timestamp4 = timestamp1 + datetime.timedelta(microseconds=3000)
248+
timestamp4 = timestamp1 + timedelta(microseconds=3000)
250249
timestamp4_micros = _microseconds_from_datetime(timestamp4)
251250

252251
if row1 is not None:

packages/google-cloud-bigtable/tests/unit/v2_client/test_backup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import pytest
2020

2121
from ._testing import _make_credentials
22-
from google.cloud._helpers import UTC
2322

2423
PROJECT_ID = "project-id"
2524
INSTANCE_ID = "instance-id"
@@ -38,7 +37,7 @@
3837

3938

4039
def _make_timestamp():
41-
return datetime.datetime.utcnow().replace(tzinfo=UTC)
40+
return datetime.datetime.now(datetime.timezone.utc)
4241

4342

4443
def _make_table_admin_client():

packages/google-cloud-bigtable/tests/unit/v2_client/test_cluster.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ def test_cluster_create():
420420
from google.cloud.bigtable_admin_v2.types import instance as instance_v2_pb2
421421
from google.cloud.bigtable.enums import StorageType
422422

423-
NOW = datetime.datetime.utcnow()
423+
NOW = datetime.datetime.now(datetime.timezone.utc)
424424
NOW_PB = _datetime_to_pb_timestamp(NOW)
425425
credentials = _make_credentials()
426426
client = _make_client(project=PROJECT, credentials=credentials, admin=True)
@@ -475,7 +475,7 @@ def test_cluster_create_w_cmek():
475475
from google.cloud.bigtable_admin_v2.types import instance as instance_v2_pb2
476476
from google.cloud.bigtable.enums import StorageType
477477

478-
NOW = datetime.datetime.utcnow()
478+
NOW = datetime.datetime.now(datetime.timezone.utc)
479479
NOW_PB = _datetime_to_pb_timestamp(NOW)
480480
credentials = _make_credentials()
481481
client = _make_client(project=PROJECT, credentials=credentials, admin=True)
@@ -535,7 +535,7 @@ def test_cluster_create_w_autoscaling():
535535
from google.cloud.bigtable_admin_v2.types import instance as instance_v2_pb2
536536
from google.cloud.bigtable.enums import StorageType
537537

538-
NOW = datetime.datetime.utcnow()
538+
NOW = datetime.datetime.now(datetime.timezone.utc)
539539
NOW_PB = _datetime_to_pb_timestamp(NOW)
540540
credentials = _make_credentials()
541541
client = _make_client(project=PROJECT, credentials=credentials, admin=True)
@@ -602,7 +602,7 @@ def test_cluster_update():
602602
)
603603
from google.cloud.bigtable.enums import StorageType
604604

605-
NOW = datetime.datetime.utcnow()
605+
NOW = datetime.datetime.now(datetime.timezone.utc)
606606
NOW_PB = _datetime_to_pb_timestamp(NOW)
607607

608608
credentials = _make_credentials()
@@ -669,7 +669,7 @@ def test_cluster_update_w_autoscaling():
669669
)
670670
from google.cloud.bigtable.enums import StorageType
671671

672-
NOW = datetime.datetime.utcnow()
672+
NOW = datetime.datetime.now(datetime.timezone.utc)
673673
NOW_PB = _datetime_to_pb_timestamp(NOW)
674674

675675
credentials = _make_credentials()
@@ -728,7 +728,7 @@ def test_cluster_update_w_partial_autoscaling_config():
728728
)
729729
from google.cloud.bigtable.enums import StorageType
730730

731-
NOW = datetime.datetime.utcnow()
731+
NOW = datetime.datetime.now(datetime.timezone.utc)
732732
NOW_PB = _datetime_to_pb_timestamp(NOW)
733733

734734
credentials = _make_credentials()
@@ -812,7 +812,7 @@ def test_cluster_update_w_both_manual_and_autoscaling():
812812
)
813813
from google.cloud.bigtable.enums import StorageType
814814

815-
NOW = datetime.datetime.utcnow()
815+
NOW = datetime.datetime.now(datetime.timezone.utc)
816816
NOW_PB = _datetime_to_pb_timestamp(NOW)
817817

818818
credentials = _make_credentials()
@@ -873,7 +873,7 @@ def test_cluster_disable_autoscaling():
873873
from google.cloud.bigtable.instance import Instance
874874
from google.cloud.bigtable.enums import StorageType
875875

876-
NOW = datetime.datetime.utcnow()
876+
NOW = datetime.datetime.now(datetime.timezone.utc)
877877
NOW_PB = _datetime_to_pb_timestamp(NOW)
878878
credentials = _make_credentials()
879879
client = _make_client(project=PROJECT, credentials=credentials, admin=True)

0 commit comments

Comments
 (0)