You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by ep...@apache.org on 2023/03/07 16:16:11 UTC

[airflow] 04/23: convert moment with timezone to UTC instead of raising an exception (#29606)

This is an automated email from the ASF dual-hosted git repository.

ephraimanierobi pushed a commit to branch v2-5-test
in repository https://gitbox.apache.org/repos/asf/airflow.git

commit e716b7509c789549ee43f060ebea00676dd67b07
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
    
    (cherry picked from commit 79c07e3fc5d580aea271ff3f0887291ae9e4473f)
---
 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 3967940a7e..6293be84d8 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)