You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by ta...@apache.org on 2023/02/19 20:27:54 UTC
[airflow] branch main updated: convert moment with timezone to UTC instead of raising an exception (#29606)
This is an automated email from the ASF dual-hosted git repository.
taragolis pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 79c07e3fc5 convert moment with timezone to UTC instead of raising an exception (#29606)
79c07e3fc5 is described below
commit 79c07e3fc5d580aea271ff3f0887291ae9e4473f
Author: Hussein Awala <ho...@gmail.com>
AuthorDate: Sun Feb 19 21:27:44 2023 +0100
convert moment with timezone to UTC instead of raising an exception (#29606)
* convert moments with timezone to utc instead of raising an exception
* test DateTimeTrigger with different timezones
---
airflow/triggers/temporal.py | 4 +---
tests/triggers/test_temporal.py | 14 +++++++++++---
2 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/airflow/triggers/temporal.py b/airflow/triggers/temporal.py
index d7f8ab5299..e23e19f389 100644
--- a/airflow/triggers/temporal.py
+++ b/airflow/triggers/temporal.py
@@ -41,10 +41,8 @@ class DateTimeTrigger(BaseTrigger):
# Make sure it's in UTC
elif moment.tzinfo is None:
raise ValueError("You cannot pass naive datetimes")
- elif not hasattr(moment.tzinfo, "offset") or moment.tzinfo.offset != 0: # type: ignore
- raise ValueError(f"The passed datetime must be using Pendulum's UTC, not {moment.tzinfo!r}")
else:
- self.moment = moment
+ self.moment = timezone.convert_to_utc(moment)
def serialize(self) -> tuple[str, dict[str, Any]]:
return ("airflow.triggers.temporal.DateTimeTrigger", {"moment": self.moment})
diff --git a/tests/triggers/test_temporal.py b/tests/triggers/test_temporal.py
index 21a419065d..655910394f 100644
--- a/tests/triggers/test_temporal.py
+++ b/tests/triggers/test_temporal.py
@@ -61,14 +61,22 @@ def test_timedelta_trigger_serialization():
assert -2 < (kwargs["moment"] - expected_moment).total_seconds() < 2
+@pytest.mark.parametrize(
+ "tz",
+ [
+ pendulum.tz.timezone("UTC"),
+ pendulum.tz.timezone("Europe/Paris"),
+ pendulum.tz.timezone("America/Toronto"),
+ ],
+)
@pytest.mark.asyncio
-async def test_datetime_trigger_timing():
+async def test_datetime_trigger_timing(tz):
"""
Tests that the DateTimeTrigger only goes off on or after the appropriate
time.
"""
- past_moment = timezone.utcnow() - datetime.timedelta(seconds=60)
- future_moment = timezone.utcnow() + datetime.timedelta(seconds=60)
+ past_moment = pendulum.instance((timezone.utcnow() - datetime.timedelta(seconds=60)).astimezone(tz))
+ future_moment = pendulum.instance((timezone.utcnow() + datetime.timedelta(seconds=60)).astimezone(tz))
# Create a task that runs the trigger for a short time then cancels it
trigger = DateTimeTrigger(future_moment)