Skip to content

Commit

Permalink
Migrate over to using datetime.now(timezone.utc) (flyteorg#2270)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas J. Fan <[email protected]>
  • Loading branch information
thomasjpfan authored Mar 15, 2024
1 parent 4208da2 commit 3f45131
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion flytekit/bin/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def setup_execution(
domain=exe_domain,
name=exe_name,
),
execution_date=_datetime.datetime.utcnow(),
execution_date=_datetime.datetime.now(_datetime.timezone.utc),
stats=_get_stats(
cfg=StatsConfig.auto(),
# Stats metric path will be:
Expand Down
2 changes: 1 addition & 1 deletion flytekit/core/context_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ def initialize():
default_user_space_params = ExecutionParameters(
execution_id=WorkflowExecutionIdentifier.promote_from_model(default_execution_id),
task_id=_identifier.Identifier(_identifier.ResourceType.TASK, "local", "local", "local", "local"),
execution_date=_datetime.datetime.utcnow(),
execution_date=_datetime.datetime.now(_datetime.timezone.utc),
stats=mock_stats.MockStats(),
logging=user_space_logger,
tmp_dir=user_space_path,
Expand Down
6 changes: 4 additions & 2 deletions flytekit/core/mock_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ def __init__(self, mock_stats, metric, tags):
self._tags = tags

def __enter__(self):
self._timer = _datetime.datetime.utcnow()
self._timer = _datetime.datetime.now(_datetime.timezone.utc)

def __exit__(self, exc_type, exc_val, exc_tb):
self._mock_stats.gauge(self._metric, _datetime.datetime.utcnow() - self._timer, tags=self._tags)
self._mock_stats.gauge(
self._metric, _datetime.datetime.now(_datetime.timezone.utc) - self._timer, tags=self._tags
)
self._timer = None
4 changes: 2 additions & 2 deletions flytekit/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def wrapper(*args, **kwargs):
return wrapper

def __enter__(self):
self.start_time = datetime.datetime.utcnow()
self.start_time = datetime.datetime.now(datetime.timezone.utc)
self._start_wall_time = _time.perf_counter()
self._start_process_time = _time.process_time()
return self
Expand All @@ -323,7 +323,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
"""
from flytekit.core.context_manager import FlyteContextManager

end_time = datetime.datetime.utcnow()
end_time = datetime.datetime.now(datetime.timezone.utc)
end_wall_time = _time.perf_counter()
end_process_time = _time.process_time()

Expand Down
10 changes: 5 additions & 5 deletions flytekit/experimental/eager_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import inspect
import signal
from contextlib import asynccontextmanager
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from functools import partial, wraps
from typing import List, Optional

Expand Down Expand Up @@ -179,9 +179,9 @@ async def __call__(self, **kwargs):
self.async_stack.set_node(node)

poll_interval = self._poll_interval or timedelta(seconds=30)
time_to_give_up = datetime.max if self._timeout is None else datetime.utcnow() + self._timeout
time_to_give_up = datetime.max if self._timeout is None else datetime.now(timezone.utc) + self._timeout

while datetime.utcnow() < time_to_give_up:
while datetime.now(timezone.utc) < time_to_give_up:
execution = self.remote.sync(execution)
if execution.closure.phase in {WorkflowExecutionPhase.FAILED}:
raise EagerException(f"Error executing {self.entity.name} with error: {execution.closure.error}")
Expand All @@ -208,9 +208,9 @@ async def terminate(self):
)

poll_interval = self._poll_interval or timedelta(seconds=6)
time_to_give_up = datetime.max if self._timeout is None else datetime.utcnow() + self._timeout
time_to_give_up = datetime.max if self._timeout is None else datetime.now(timezone.utc) + self._timeout

while datetime.utcnow() < time_to_give_up:
while datetime.now(timezone.utc) < time_to_give_up:
execution = self.remote.sync(execution)
if execution.is_done:
break
Expand Down
4 changes: 2 additions & 2 deletions flytekit/remote/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -1661,9 +1661,9 @@ def wait(
:param sync_nodes: passed along to the sync call for the workflow execution
"""
poll_interval = poll_interval or timedelta(seconds=30)
time_to_give_up = datetime.max if timeout is None else datetime.utcnow() + timeout
time_to_give_up = datetime.max if timeout is None else datetime.now() + timeout

while datetime.utcnow() < time_to_give_up:
while datetime.now() < time_to_give_up:
execution = self.sync_execution(execution, sync_nodes=sync_nodes)
if execution.is_done:
return execution
Expand Down
4 changes: 2 additions & 2 deletions plugins/flytekit-deck-standard/tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
[
dict(
Name="foo",
Start=datetime.datetime.utcnow(),
Finish=datetime.datetime.utcnow() + datetime.timedelta(microseconds=1000),
Start=datetime.datetime.now(datetime.timezone.utc),
Finish=datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(microseconds=1000),
WallTime=1.0,
ProcessTime=1.0,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def to_python_value(
run_id = ge.core.run_identifier.RunIdentifier(
**{
"run_name": ge_conf.datasource_name + "_run",
"run_time": datetime.datetime.utcnow(),
"run_time": datetime.datetime.now(datetime.timezone.utc),
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def execute(self, **kwargs) -> Any:
run_id = ge.core.run_identifier.RunIdentifier(
**{
"run_name": self._datasource_name + "_run",
"run_time": datetime.datetime.utcnow(),
"run_time": datetime.datetime.now(datetime.timezone.utc),
}
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import uuid
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from unittest.mock import MagicMock, patch

import click
Expand Down Expand Up @@ -58,7 +58,7 @@ def test_get_gcp_secret_manager_secret_not_found():

def create_mock_token(aud: str, expires_in: timedelta = None):
"""Create a mock JWT token with a certain audience, expiration time, and random JTI."""
exp = datetime.utcnow() + expires_in
exp = datetime.now(timezone.utc) + expires_in
jti = "test_token" + str(uuid.uuid4())
payload = {"exp": exp, "aud": aud, "jti": jti}

Expand Down
4 changes: 2 additions & 2 deletions tests/flytekit/integration/remote/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ def test_monitor_workflow_execution(register):
)

poll_interval = datetime.timedelta(seconds=1)
time_to_give_up = datetime.datetime.utcnow() + datetime.timedelta(seconds=60)
time_to_give_up = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(seconds=60)

execution = remote.sync_execution(execution, sync_nodes=True)
while datetime.datetime.utcnow() < time_to_give_up:
while datetime.datetime.now(datetime.timezone.utc) < time_to_give_up:
if execution.is_done:
break

Expand Down
4 changes: 2 additions & 2 deletions tests/flytekit/unit/core/test_context_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import base64
import os
from datetime import datetime
from datetime import datetime, timezone
from pathlib import Path
from unittest.mock import Mock

Expand Down Expand Up @@ -251,7 +251,7 @@ def test_exec_params():
ep = ExecutionParameters(
execution_id=id_models.WorkflowExecutionIdentifier("p", "d", "n"),
task_id=id_models.Identifier(id_models.ResourceType.TASK, "local", "local", "local", "local"),
execution_date=datetime.utcnow(),
execution_date=datetime.now(timezone.utc),
stats=mock_stats.MockStats(),
logging=None,
tmp_dir="/tmp",
Expand Down
4 changes: 2 additions & 2 deletions tests/flytekit/unit/deck/test_deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def test_deck():
def test_timeline_deck():
time_info = dict(
Name="foo",
Start=datetime.datetime.utcnow(),
Finish=datetime.datetime.utcnow() + datetime.timedelta(microseconds=1000),
Start=datetime.datetime.now(datetime.timezone.utc),
Finish=datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(microseconds=1000),
WallTime=1.0,
ProcessTime=1.0,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/flytekit/unit/models/test_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_boolean_primitive():


def test_datetime_primitive():
dt = datetime.utcnow().replace(tzinfo=timezone.utc)
dt = datetime.now(timezone.utc)
obj = literals.Primitive(datetime=dt)
assert obj.integer is None
assert obj.boolean is None
Expand Down

0 comments on commit 3f45131

Please sign in to comment.