diff --git a/docs/classic_client/snippets.py b/docs/classic_client/snippets.py index fa3aa3627..c6059409d 100644 --- a/docs/classic_client/snippets.py +++ b/docs/classic_client/snippets.py @@ -29,7 +29,7 @@ """ -import datetime +from datetime import datetime, timezone import pytest from google.api_core.exceptions import DeadlineExceeded @@ -39,7 +39,7 @@ from test_utils.system import unique_resource_id from test_utils.retry import RetryErrors -from google.cloud._helpers import UTC + from google.cloud.bigtable import Client from google.cloud.bigtable import enums @@ -57,8 +57,8 @@ STORAGE_TYPE = enums.StorageType.SSD LABEL_KEY = "python-snippet" LABEL_STAMP = ( - datetime.datetime.utcnow() - .replace(microsecond=0, tzinfo=UTC) + datetime.now(timezone.utc) + .replace(microsecond=0) .strftime("%Y-%m-%dt%H-%M-%S") ) LABELS = {LABEL_KEY: str(LABEL_STAMP)} diff --git a/docs/classic_client/snippets_table.py b/docs/classic_client/snippets_table.py index 893135275..1850e836b 100644 --- a/docs/classic_client/snippets_table.py +++ b/docs/classic_client/snippets_table.py @@ -29,7 +29,7 @@ """ -import datetime +from datetime import datetime, timezone import pytest from google.api_core.exceptions import TooManyRequests @@ -37,7 +37,6 @@ from test_utils.system import unique_resource_id from test_utils.retry import RetryErrors -from google.cloud._helpers import UTC from google.cloud.bigtable import Client from google.cloud.bigtable import enums from google.cloud.bigtable import column_family @@ -54,8 +53,8 @@ STORAGE_TYPE = enums.StorageType.SSD LABEL_KEY = "python-snippet" LABEL_STAMP = ( - datetime.datetime.utcnow() - .replace(microsecond=0, tzinfo=UTC) + datetime.now(timezone.utc) + .replace(microsecond=0) .strftime("%Y-%m-%dt%H-%M-%S") ) LABELS = {LABEL_KEY: str(LABEL_STAMP)} @@ -179,7 +178,7 @@ def test_bigtable_write_read_drop_truncate(): value = "value_{}".format(i).encode() row = table.row(row_key) row.set_cell( - COLUMN_FAMILY_ID, col_name, value, timestamp=datetime.datetime.utcnow() + COLUMN_FAMILY_ID, col_name, value, timestamp=datetime.now(timezone.utc) ) rows.append(row) response = table.mutate_rows(rows) @@ -270,7 +269,7 @@ def test_bigtable_mutations_batcher(): row_key = row_keys[0] row = table.row(row_key) row.set_cell( - COLUMN_FAMILY_ID, column_name, "value-0", timestamp=datetime.datetime.utcnow() + COLUMN_FAMILY_ID, column_name, "value-0", timestamp=datetime.now(timezone.utc) ) batcher.mutate(row) # Add a collections of rows @@ -279,7 +278,7 @@ def test_bigtable_mutations_batcher(): row = table.row(row_keys[i]) value = "value_{}".format(i).encode() row.set_cell( - COLUMN_FAMILY_ID, column_name, value, timestamp=datetime.datetime.utcnow() + COLUMN_FAMILY_ID, column_name, value, timestamp=datetime.now(timezone.utc) ) rows.append(row) batcher.mutate_rows(rows) @@ -759,7 +758,7 @@ def test_bigtable_batcher_mutate_flush_mutate_rows(): row_key = b"row_key_1" row = table.row(row_key) row.set_cell( - COLUMN_FAMILY_ID, COL_NAME1, "value-0", timestamp=datetime.datetime.utcnow() + COLUMN_FAMILY_ID, COL_NAME1, "value-0", timestamp=datetime.now(timezone.utc) ) # In batcher, mutate will flush current batch if it @@ -967,12 +966,12 @@ def test_bigtable_row_data_cells_cell_value_cell_values(): value = b"value_in_col1" row = Config.TABLE.row(b"row_key_1") row.set_cell( - COLUMN_FAMILY_ID, COL_NAME1, value, timestamp=datetime.datetime.utcnow() + COLUMN_FAMILY_ID, COL_NAME1, value, timestamp=datetime.now(timezone.utc) ) row.commit() row.set_cell( - COLUMN_FAMILY_ID, COL_NAME1, value, timestamp=datetime.datetime.utcnow() + COLUMN_FAMILY_ID, COL_NAME1, value, timestamp=datetime.now(timezone.utc) ) row.commit() @@ -1050,7 +1049,7 @@ def test_bigtable_row_setcell_rowkey(): cell_val = b"cell-val" row.set_cell( - COLUMN_FAMILY_ID, COL_NAME1, cell_val, timestamp=datetime.datetime.utcnow() + COLUMN_FAMILY_ID, COL_NAME1, cell_val, timestamp=datetime.now(timezone.utc) ) # [END bigtable_api_row_set_cell] diff --git a/samples/hello/main.py b/samples/hello/main.py index 7a193ba6f..b471771e4 100644 --- a/samples/hello/main.py +++ b/samples/hello/main.py @@ -28,7 +28,7 @@ from ..utils import wait_for_table # [START bigtable_hw_imports] -import datetime +from datetime import datetime, timezone from google.cloud import bigtable from google.cloud.bigtable import column_family @@ -88,7 +88,7 @@ def main(project_id, instance_id, table_id): row_key = f"greeting{i}".encode() row = table.direct_row(row_key) row.set_cell( - column_family_id, column, value, timestamp=datetime.datetime.utcnow(), + column_family_id, column, value, timestamp=datetime.now(timezone.utc), ) rows.append(row) table.mutate_rows(rows) diff --git a/samples/snippets/writes/write_batch.py b/samples/snippets/writes/write_batch.py index 8ad4b07a5..a583bb713 100644 --- a/samples/snippets/writes/write_batch.py +++ b/samples/snippets/writes/write_batch.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # [START bigtable_writes_batch] -import datetime +from datetime import datetime, timezone from google.cloud import bigtable from google.cloud.bigtable.batcher import MutationsBatcher @@ -25,7 +25,7 @@ def write_batch(project_id, instance_id, table_id): table = instance.table(table_id) with MutationsBatcher(table=table) as batcher: - timestamp = datetime.datetime.utcnow() + timestamp = datetime.now(timezone.utc) column_family_id = "stats_summary" rows = [ diff --git a/samples/snippets/writes/write_conditionally.py b/samples/snippets/writes/write_conditionally.py index 7fb640aad..b6f05fba7 100644 --- a/samples/snippets/writes/write_conditionally.py +++ b/samples/snippets/writes/write_conditionally.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # [START bigtable_writes_conditional] -import datetime +from datetime import datetime, timezone from google.cloud import bigtable from google.cloud.bigtable import row_filters @@ -24,7 +24,7 @@ def write_conditional(project_id, instance_id, table_id): instance = client.instance(instance_id) table = instance.table(table_id) - timestamp = datetime.datetime.utcnow() + timestamp = datetime.now(timezone.utc) column_family_id = "stats_summary" row_key = "phone#4c410523#20190501" diff --git a/samples/snippets/writes/write_simple.py b/samples/snippets/writes/write_simple.py index 1aa5a810f..fb7074bc5 100644 --- a/samples/snippets/writes/write_simple.py +++ b/samples/snippets/writes/write_simple.py @@ -14,7 +14,7 @@ # limitations under the License. # [START bigtable_writes_simple] -import datetime +from datetime import datetime, timezone from google.cloud import bigtable @@ -24,7 +24,7 @@ def write_simple(project_id, instance_id, table_id): instance = client.instance(instance_id) table = instance.table(table_id) - timestamp = datetime.datetime.utcnow() + timestamp = datetime.now(timezone.utc) column_family_id = "stats_summary" row_key = "phone#4c410523#20190501" diff --git a/tests/system/v2_client/_helpers.py b/tests/system/v2_client/_helpers.py index 95261879e..e792def15 100644 --- a/tests/system/v2_client/_helpers.py +++ b/tests/system/v2_client/_helpers.py @@ -12,12 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import datetime +from datetime import datetime, timezone import grpc from google.api_core import exceptions from google.cloud import exceptions as core_exceptions -from google.cloud._helpers import UTC from test_utils import retry @@ -41,7 +40,5 @@ def _retry_on_unavailable(exc): def label_stamp(): return ( - datetime.datetime.utcnow() - .replace(microsecond=0, tzinfo=UTC) - .strftime("%Y-%m-%dt%H-%M-%S") + datetime.now(timezone.utc).replace(microsecond=0).strftime("%Y-%m-%dt%H-%M-%S") ) diff --git a/tests/system/v2_client/test_data_api.py b/tests/system/v2_client/test_data_api.py index 579837e34..c012eb32a 100644 --- a/tests/system/v2_client/test_data_api.py +++ b/tests/system/v2_client/test_data_api.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import datetime +from datetime import datetime, timedelta, timezone import operator import pytest @@ -62,8 +62,8 @@ def rows_to_delete(): def test_table_read_rows_filter_millis(data_table): from google.cloud.bigtable import row_filters - end = datetime.datetime.now() - start = end - datetime.timedelta(minutes=60) + end = datetime.now() + start = end - timedelta(minutes=60) timestamp_range = row_filters.TimestampRange(start=start, end=end) timefilter = row_filters.TimestampRangeFilter(timestamp_range) 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) def _write_to_row(row1, row2, row3, row4): from google.cloud._helpers import _datetime_from_microseconds from google.cloud._helpers import _microseconds_from_datetime - from google.cloud._helpers import UTC from google.cloud.bigtable.row_data import Cell - timestamp1 = datetime.datetime.utcnow().replace(tzinfo=UTC) + timestamp1 = datetime.now(timezone.utc) timestamp1_micros = _microseconds_from_datetime(timestamp1) # Truncate to millisecond granularity. timestamp1_micros -= timestamp1_micros % 1000 timestamp1 = _datetime_from_microseconds(timestamp1_micros) # 1000 microseconds is a millisecond - timestamp2 = timestamp1 + datetime.timedelta(microseconds=1000) + timestamp2 = timestamp1 + timedelta(microseconds=1000) timestamp2_micros = _microseconds_from_datetime(timestamp2) - timestamp3 = timestamp1 + datetime.timedelta(microseconds=2000) + timestamp3 = timestamp1 + timedelta(microseconds=2000) timestamp3_micros = _microseconds_from_datetime(timestamp3) - timestamp4 = timestamp1 + datetime.timedelta(microseconds=3000) + timestamp4 = timestamp1 + timedelta(microseconds=3000) timestamp4_micros = _microseconds_from_datetime(timestamp4) if row1 is not None: diff --git a/tests/unit/v2_client/test_backup.py b/tests/unit/v2_client/test_backup.py index cc9251a35..a5d205af6 100644 --- a/tests/unit/v2_client/test_backup.py +++ b/tests/unit/v2_client/test_backup.py @@ -19,7 +19,6 @@ import pytest from ._testing import _make_credentials -from google.cloud._helpers import UTC PROJECT_ID = "project-id" INSTANCE_ID = "instance-id" @@ -38,7 +37,7 @@ def _make_timestamp(): - return datetime.datetime.utcnow().replace(tzinfo=UTC) + return datetime.datetime.now(datetime.timezone.utc) def _make_table_admin_client(): diff --git a/tests/unit/v2_client/test_cluster.py b/tests/unit/v2_client/test_cluster.py index 65ed47437..a21104549 100644 --- a/tests/unit/v2_client/test_cluster.py +++ b/tests/unit/v2_client/test_cluster.py @@ -420,7 +420,7 @@ def test_cluster_create(): from google.cloud.bigtable_admin_v2.types import instance as instance_v2_pb2 from google.cloud.bigtable.enums import StorageType - NOW = datetime.datetime.utcnow() + NOW = datetime.datetime.now(datetime.timezone.utc) NOW_PB = _datetime_to_pb_timestamp(NOW) credentials = _make_credentials() client = _make_client(project=PROJECT, credentials=credentials, admin=True) @@ -475,7 +475,7 @@ def test_cluster_create_w_cmek(): from google.cloud.bigtable_admin_v2.types import instance as instance_v2_pb2 from google.cloud.bigtable.enums import StorageType - NOW = datetime.datetime.utcnow() + NOW = datetime.datetime.now(datetime.timezone.utc) NOW_PB = _datetime_to_pb_timestamp(NOW) credentials = _make_credentials() client = _make_client(project=PROJECT, credentials=credentials, admin=True) @@ -535,7 +535,7 @@ def test_cluster_create_w_autoscaling(): from google.cloud.bigtable_admin_v2.types import instance as instance_v2_pb2 from google.cloud.bigtable.enums import StorageType - NOW = datetime.datetime.utcnow() + NOW = datetime.datetime.now(datetime.timezone.utc) NOW_PB = _datetime_to_pb_timestamp(NOW) credentials = _make_credentials() client = _make_client(project=PROJECT, credentials=credentials, admin=True) @@ -602,7 +602,7 @@ def test_cluster_update(): ) from google.cloud.bigtable.enums import StorageType - NOW = datetime.datetime.utcnow() + NOW = datetime.datetime.now(datetime.timezone.utc) NOW_PB = _datetime_to_pb_timestamp(NOW) credentials = _make_credentials() @@ -669,7 +669,7 @@ def test_cluster_update_w_autoscaling(): ) from google.cloud.bigtable.enums import StorageType - NOW = datetime.datetime.utcnow() + NOW = datetime.datetime.now(datetime.timezone.utc) NOW_PB = _datetime_to_pb_timestamp(NOW) credentials = _make_credentials() @@ -728,7 +728,7 @@ def test_cluster_update_w_partial_autoscaling_config(): ) from google.cloud.bigtable.enums import StorageType - NOW = datetime.datetime.utcnow() + NOW = datetime.datetime.now(datetime.timezone.utc) NOW_PB = _datetime_to_pb_timestamp(NOW) credentials = _make_credentials() @@ -812,7 +812,7 @@ def test_cluster_update_w_both_manual_and_autoscaling(): ) from google.cloud.bigtable.enums import StorageType - NOW = datetime.datetime.utcnow() + NOW = datetime.datetime.now(datetime.timezone.utc) NOW_PB = _datetime_to_pb_timestamp(NOW) credentials = _make_credentials() @@ -873,7 +873,7 @@ def test_cluster_disable_autoscaling(): from google.cloud.bigtable.instance import Instance from google.cloud.bigtable.enums import StorageType - NOW = datetime.datetime.utcnow() + NOW = datetime.datetime.now(datetime.timezone.utc) NOW_PB = _datetime_to_pb_timestamp(NOW) credentials = _make_credentials() client = _make_client(project=PROJECT, credentials=credentials, admin=True) diff --git a/tests/unit/v2_client/test_instance.py b/tests/unit/v2_client/test_instance.py index 712fab1f5..c5ef9c9b8 100644 --- a/tests/unit/v2_client/test_instance.py +++ b/tests/unit/v2_client/test_instance.py @@ -277,7 +277,7 @@ def _instance_api_response_for_create(): ) from google.cloud.bigtable_admin_v2.types import instance - NOW = datetime.datetime.utcnow() + NOW = datetime.datetime.now(datetime.timezone.utc) NOW_PB = _datetime_to_pb_timestamp(NOW) metadata = messages_v2_pb2.CreateInstanceMetadata(request_time=NOW_PB) type_url = "type.googleapis.com/{}".format( @@ -503,7 +503,7 @@ def _instance_api_response_for_update(): ) from google.cloud.bigtable_admin_v2.types import instance - NOW = datetime.datetime.utcnow() + NOW = datetime.datetime.now(datetime.timezone.utc) NOW_PB = _datetime_to_pb_timestamp(NOW) metadata = messages_v2_pb2.UpdateInstanceMetadata(request_time=NOW_PB) type_url = "type.googleapis.com/{}".format( diff --git a/tests/unit/v2_client/test_table.py b/tests/unit/v2_client/test_table.py index 1d183e2fb..6b31a5e23 100644 --- a/tests/unit/v2_client/test_table.py +++ b/tests/unit/v2_client/test_table.py @@ -1378,13 +1378,12 @@ def test_table_backup_factory_defaults(): def test_table_backup_factory_non_defaults(): import datetime - from google.cloud._helpers import UTC from google.cloud.bigtable.backup import Backup from google.cloud.bigtable.instance import Instance instance = Instance(INSTANCE_ID, None) table = _make_table(TABLE_ID, instance) - timestamp = datetime.datetime.utcnow().replace(tzinfo=UTC) + timestamp = datetime.datetime.now(datetime.timezone.utc) backup = table.backup( BACKUP_ID, cluster_id=CLUSTER_ID,