diff --git a/google/cloud/storage/client.py b/google/cloud/storage/client.py index 7d787a731..c85eda611 100644 --- a/google/cloud/storage/client.py +++ b/google/cloud/storage/client.py @@ -1613,10 +1613,10 @@ def generate_signed_post_policy_v4( Example: Generate signed POST policy and upload a file. + >>> import datetime >>> from google.cloud import storage - >>> import pytz >>> client = storage.Client() - >>> tz = pytz.timezone('America/New_York') + >>> tz = datetime.timezone(datetime.timedelta(hours=1), 'CET') >>> policy = client.generate_signed_post_policy_v4( "bucket-name", "blob-name", diff --git a/tests/unit/test__signing.py b/tests/unit/test__signing.py index 3eac70cc1..823e71355 100644 --- a/tests/unit/test__signing.py +++ b/tests/unit/test__signing.py @@ -44,6 +44,24 @@ def _utc_seconds(when): return int(calendar.timegm(when.timetuple())) +def _make_cet_timezone(): + try: + from datetime import timezone + + except ImportError: # Python 2.7 + from google.cloud._helpers import _UTC + + class CET(_UTC): + _tzname = "CET" + _utcoffset = datetime.timedelta(hours=1) + + return CET() + else: + from datetime import timedelta + + return timezone(timedelta(hours=1), name="CET") + + class Test_get_expiration_seconds_v2(unittest.TestCase): @staticmethod def _call_fut(expiration): @@ -81,13 +99,7 @@ def test_w_expiration_utc_datetime(self): self.assertEqual(self._call_fut(expiration_utc), utc_seconds) def test_w_expiration_other_zone_datetime(self): - from google.cloud._helpers import _UTC - - class CET(_UTC): - _tzname = "CET" - _utcoffset = datetime.timedelta(hours=1) - - zone = CET() + zone = _make_cet_timezone() expiration_other = datetime.datetime(2004, 8, 19, 0, 0, 0, 0, zone) utc_seconds = _utc_seconds(expiration_other) cet_seconds = utc_seconds - (60 * 60) # CET one hour earlier than UTC @@ -198,13 +210,8 @@ def test_w_expiration_utc_datetime(self): def test_w_expiration_other_zone_datetime(self): from google.cloud._helpers import UTC - from google.cloud._helpers import _UTC - - class CET(_UTC): - _tzname = "CET" - _utcoffset = datetime.timedelta(hours=1) - zone = CET() + zone = _make_cet_timezone() fake_utcnow = datetime.datetime(2004, 8, 19, 0, 0, 0, 0, UTC) fake_cetnow = fake_utcnow.astimezone(zone) delta = datetime.timedelta(seconds=10) diff --git a/tests/unit/test_bucket.py b/tests/unit/test_bucket.py index e3b770763..cc2466ac8 100644 --- a/tests/unit/test_bucket.py +++ b/tests/unit/test_bucket.py @@ -366,10 +366,10 @@ def test_ctor_defaults(self): def test_ctor_explicit_ubla(self): import datetime - import pytz + from google.cloud._helpers import UTC bucket = self._make_bucket() - now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC) + now = datetime.datetime.utcnow().replace(tzinfo=UTC) config = self._make_one( bucket, @@ -403,10 +403,10 @@ def test_ctor_explicit_pap(self): def test_ctor_explicit_bpo(self): import datetime - import pytz + from google.cloud._helpers import UTC bucket = self._make_bucket() - now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC) + now = datetime.datetime.utcnow().replace(tzinfo=UTC) config = pytest.deprecated_call( self._make_one, @@ -433,10 +433,10 @@ def test_ctor_ubla_and_bpo_enabled(self): def test_ctor_ubla_and_bpo_time(self): import datetime - import pytz + from google.cloud._helpers import UTC bucket = self._make_bucket() - now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC) + now = datetime.datetime.utcnow().replace(tzinfo=UTC) with self.assertRaises(ValueError): self._make_one( @@ -481,12 +481,12 @@ def test_from_api_repr_w_disabled(self): def test_from_api_repr_w_enabled(self): import datetime - import pytz + from google.cloud._helpers import UTC from google.cloud._helpers import _datetime_to_rfc3339 klass = self._get_target_class() bucket = self._make_bucket() - now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC) + now = datetime.datetime.utcnow().replace(tzinfo=UTC) resource = { "uniformBucketLevelAccess": { "enabled": True, @@ -2174,11 +2174,11 @@ def test_iam_configuration_policy_missing(self): def test_iam_configuration_policy_w_entry(self): import datetime - import pytz + from google.cloud._helpers import UTC from google.cloud._helpers import _datetime_to_rfc3339 from google.cloud.storage.bucket import IAMConfiguration - now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC) + now = datetime.datetime.utcnow().replace(tzinfo=UTC) NAME = "name" properties = { "iamConfiguration": { diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py index 4998c54f4..77a3030cd 100644 --- a/tests/unit/test_client.py +++ b/tests/unit/test_client.py @@ -1778,7 +1778,7 @@ def _create_hmac_key_helper( self, explicit_project=None, user_project=None, timeout=None, retry=None, ): import datetime - from pytz import UTC + from google.cloud._helpers import UTC from google.cloud.storage.hmac_key import HMACKeyMetadata project = "PROJECT" diff --git a/tests/unit/test_hmac_key.py b/tests/unit/test_hmac_key.py index 60d0c135b..59a2b221f 100644 --- a/tests/unit/test_hmac_key.py +++ b/tests/unit/test_hmac_key.py @@ -173,7 +173,7 @@ def test_state_setter_active(self): def test_time_created_getter(self): import datetime - from pytz import UTC + from google.cloud._helpers import UTC metadata = self._make_one() now = datetime.datetime.utcnow() @@ -183,7 +183,7 @@ def test_time_created_getter(self): def test_updated_getter(self): import datetime - from pytz import UTC + from google.cloud._helpers import UTC metadata = self._make_one() now = datetime.datetime.utcnow()