Skip to content

Commit fe3daea

Browse files
Use timezone aware datetimes (GoogleCloudPlatform#6587)
* Use timezone aware datetimes * fix: use datetime.isoformat() * Update custom_metric.py * Update custom_metric.py * Update custom_metric.py * Revert composer changes, add comment instead * fix header-check Co-authored-by: Leah Cole <[email protected]> Co-authored-by: Leah E. Cole <[email protected]>
1 parent 2f43955 commit fe3daea

File tree

43 files changed

+103
-82
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+103
-82
lines changed

AUTHORING_GUIDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ and 10`.
292292

293293
Always create timezone aware datetime objects. For libraries that use protobuf,
294294
omitting the timezone may lead to unexpected behavior when the datetime
295-
is converted to a protobuf tiemstamp.
295+
is converted to a protobuf timestamp.
296296

297297
```py
298298
import datetime

appengine/flexible/datastore/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def index():
4848
entity = datastore.Entity(key=ds.key('visit'))
4949
entity.update({
5050
'user_ip': user_ip,
51-
'timestamp': datetime.datetime.utcnow()
51+
'timestamp': datetime.datetime.now(tz=datetime.timezone.utc)
5252
})
5353

5454
ds.put(entity)

appengine/flexible/tasks/create_app_engine_queue_task.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def create_task(project, queue, location, payload=None, in_seconds=None):
6060

6161
if in_seconds is not None:
6262
# Convert "seconds from now" into an rfc3339 datetime string.
63-
d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds)
63+
d = datetime.datetime.now(tz=datetime.timezone.utc) + datetime.timedelta(seconds=in_seconds)
6464

6565
# Create Timestamp protobuf.
6666
timestamp = timestamp_pb2.Timestamp()

appengine/standard/mail/user_signup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def get(self):
8989
if code:
9090
record = ndb.Key(UserConfirmationRecord, code).get()
9191
# 2-hour time limit on confirming.
92-
if record and (datetime.datetime.now() - record.timestamp <
92+
if record and (datetime.datetime.now(tz=datetime.timezone.utc) - record.timestamp <
9393
datetime.timedelta(hours=2)):
9494
record.confirmed = True
9595
record.put()

appengine/standard_python3/building-an-app/building-an-app-2/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def fetch_times(limit):
5454
@app.route('/')
5555
def root():
5656
# Store the current access time in Datastore.
57-
store_time(datetime.datetime.now())
57+
store_time(datetime.datetime.now(tz=datetime.timezone.utc))
5858

5959
# Fetch the most recent 10 access times from Datastore.
6060
times = fetch_times(10)

appengine/standard_python3/building-an-app/building-an-app-3/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def root():
7575
# Record and fetch the recent times a logged-in user has accessed
7676
# the site. This is currently shared amongst all users, but will be
7777
# individualized in a following step.
78-
store_time(datetime.datetime.now())
78+
store_time(datetime.datetime.now(tz=datetime.timezone.utc))
7979
times = fetch_times(10)
8080

8181
return render_template(

appengine/standard_python3/building-an-app/building-an-app-4/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def root():
7373
claims = google.oauth2.id_token.verify_firebase_token(
7474
id_token, firebase_request_adapter)
7575

76-
store_time(claims['email'], datetime.datetime.now())
76+
store_time(claims['email'], datetime.datetime.now(tz=datetime.timezone.utc))
7777
times = fetch_times(claims['email'], 10)
7878

7979
except ValueError as exc:

appengine/standard_python3/pubsub/main_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def signer():
5151

5252
@pytest.fixture
5353
def fake_token(signer):
54-
now = calendar.timegm(datetime.datetime.utcnow().utctimetuple())
54+
now = calendar.timegm(datetime.datetime.now(tz=datetime.timezone.utc).utctimetuple())
5555
payload = {
5656
'aud': 'example.com',
5757
'azp': '1234567890',

blog/introduction_to_data_models_in_cloud_datastore/blog.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2015, Google, Inc.
1+
# Copyright 2015 Google, LLC.
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -43,7 +43,7 @@ def create_user(ds, username, profile):
4343

4444

4545
def create_post(ds, username, post_content):
46-
now = datetime.datetime.utcnow()
46+
now = datetime.datetime.now(tz=datetime.timezone.utc)
4747
key = path_to_key(ds, '{0}.user/{1}.post'.format(username, now))
4848
entity = datastore.Entity(key)
4949

@@ -57,7 +57,7 @@ def create_post(ds, username, post_content):
5757

5858

5959
def repost(ds, username, original):
60-
now = datetime.datetime.utcnow()
60+
now = datetime.datetime.now(tz=datetime.timezone.utc)
6161
new_key = path_to_key(ds, '{0}.user/{1}.post'.format(username, now))
6262
new = datastore.Entity(new_key)
6363

blog/introduction_to_data_models_in_cloud_datastore/wiki.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2015, Google, Inc.
1+
# Copyright 2015 Google, LLC.
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -37,7 +37,7 @@ def path_to_key(datastore, path):
3737

3838
def save_page(ds, page, content):
3939
with ds.transaction():
40-
now = datetime.datetime.utcnow()
40+
now = datetime.datetime.now(tz=datetime.timezone.utc)
4141
current_key = path_to_key(ds, '{}.page/current.revision'.format(page))
4242
revision_key = path_to_key(ds, '{}.page/{}.revision'.format(page, now))
4343

0 commit comments

Comments
 (0)