You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "SML0127 (via GitHub)" <gi...@apache.org> on 2025/05/25 09:56:19 UTC

[PR] Feature/support tz for date in timesensor [airflow]

SML0127 opened a new pull request, #51043:
URL: https://github.com/apache/airflow/pull/51043

   <!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at
   
      http://www.apache.org/licenses/LICENSE-2.0
   
    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
   
   This PR closes https://github.com/apache/airflow/issues/38337
   
   In the previous logic (using `datetime.datetime.today()`), if server's timezone is UTC, `target_datetime` for TimeSensor could be incorrectly.
   
   For example, when the user intended to use `Asia/Singapore` timezone and server timezone is `UTC`, taget time `2025-01-27 07:00:00+08:00` might be incorrectly converted to `2025-01-25 23:00:00 UTC` instead of `2025-01-26 23:00:00 UTC`.
   
   This happens because the local date '2025-01-26' is combined with the target time `07:00:00`, resulting in `2025-01-26 07:00:00` in local time. And then this datetime is converted to UTC using `convert_to_utc`, it becomes `2025-01-25 23:00:00 UTC`.
   
   To fix this, `pendulum.now(tz=str(self.dag.timezone))` is used instead of `datetime.datetime.today()`.
   
   
   All unit tests passed
   <img width="1440" alt="image" src="https://github.com/user-attachments/assets/e6c57e22-88fb-49ed-b4c2-409080f21ebc" />
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Feature/support tz for date in timesensor [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #51043:
URL: https://github.com/apache/airflow/pull/51043#issuecomment-2907728613

   Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
   Here are some useful points:
   - Pay attention to the quality of your code (ruff, mypy and type annotations). Our [pre-commits]( https://github.com/apache/airflow/blob/main/contributing-docs/08_static_code_checks.rst#prerequisites-for-pre-commit-hooks) will help you with that.
   - In case of a new feature add useful documentation (in docstrings or in `docs/` directory). Adding a new operator? Check this short [guide](https://github.com/apache/airflow/blob/main/airflow-core/docs/howto/custom-operator.rst) Consider adding an example DAG that shows how users should use it.
   - Consider using [Breeze environment](https://github.com/apache/airflow/blob/main/dev/breeze/doc/README.rst) for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
   - Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
   - Please follow [ASF Code of Conduct](https://www.apache.org/foundation/policies/conduct) for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
   - Be sure to read the [Airflow Coding style]( https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#coding-style-and-best-practices).
   - Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
   Apache Airflow is a community-driven project and together we are making it better 🚀.
   In case of doubts contact the developers at:
   Mailing List: dev@airflow.apache.org
   Slack: https://s.apache.org/airflow-slack
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add timezone support for date calculation in TimeSensor [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #51043:
URL: https://github.com/apache/airflow/pull/51043#issuecomment-2909793795

   Awesome work, congrats on your first merged pull request! You are invited to check our [Issue Tracker](https://github.com/apache/airflow/issues) for additional contributions.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add timezone support for date calculation in TimeSensor [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #51043:
URL: https://github.com/apache/airflow/pull/51043#discussion_r2106153941


##########
providers/standard/tests/unit/standard/sensors/test_time.py:
##########
@@ -58,6 +58,35 @@ def test_target_time_aware_dag_timezone(self):
             op = TimeSensor(task_id="test", target_time=aware_time)
             assert op.target_datetime.tzinfo == timezone.utc
 
+    @pytest.mark.parametrize(
+        "current_datetime, server_timezone",
+        [
+            ("2025-01-26 23:00:00", "UTC"),
+            ("2025-01-27 07:00:00", DEFAULT_TIMEZONE),

Review Comment:
   This test would break if someone changes `DEFAULT_TIMEZONE` without noticing it depends on this case relies on exactly 7+ hours of offset. It would be much better to create a timezone explicitly for this test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add timezone support for date calculation in TimeSensor [airflow]

Posted by "SML0127 (via GitHub)" <gi...@apache.org>.
SML0127 commented on PR #51043:
URL: https://github.com/apache/airflow/pull/51043#issuecomment-2907748657

   > @uranusjr Thank you for the kind and detiled comments. I've addressed the points you mentioned.
   
   unit test results
   <img width="1672" alt="image" src="https://github.com/user-attachments/assets/6f89bc90-d4de-4f9a-9e8c-39506b8d3975" />
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add timezone support for date calculation in TimeSensor [airflow]

Posted by "dwreeves (via GitHub)" <gi...@apache.org>.
dwreeves commented on PR #51043:
URL: https://github.com/apache/airflow/pull/51043#issuecomment-2910774919

   Thank you for resolving the issue! ❤️


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add timezone support for date calculation in TimeSensor [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #51043:
URL: https://github.com/apache/airflow/pull/51043#discussion_r2106153642


##########
providers/standard/src/airflow/providers/standard/sensors/time.py:
##########
@@ -88,8 +90,10 @@ def __init__(
 
         # Create a "date-aware" timestamp that will be used as the "target_datetime". This is a requirement
         # of the DateTimeTrigger
+
+        # Get date considering dag.timezone
         aware_time = timezone.coerce_datetime(
-            datetime.datetime.combine(datetime.datetime.today(), target_time, self.dag.timezone)
+            datetime.datetime.combine(pendulum.now(tz=str(self.dag.timezone)), target_time, self.dag.timezone)

Review Comment:
   This can simply be `datetime.datetime.now(self.dag.timezone)` without involving pendulum, nor calling `str()`.
   
   Note in particular that casting a timezone to str does not always work as you expect.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add timezone support for date calculation in TimeSensor [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #51043:
URL: https://github.com/apache/airflow/pull/51043#discussion_r2106255620


##########
providers/standard/src/airflow/providers/standard/sensors/time.py:
##########
@@ -88,8 +88,12 @@ def __init__(
 
         # Create a "date-aware" timestamp that will be used as the "target_datetime". This is a requirement
         # of the DateTimeTrigger
+
+        # Get date considering dag.timezone
         aware_time = timezone.coerce_datetime(
-            datetime.datetime.combine(datetime.datetime.today(), target_time, self.dag.timezone)
+            datetime.datetime.combine(
+                datetime.datetime.now(self.dag.timezone), target_time, self.dag.timezone
+            )

Review Comment:
   ```suggestion
               datetime.datetime.now(self.dag.timezone)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add timezone support for date calculation in TimeSensor [airflow]

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W merged PR #51043:
URL: https://github.com/apache/airflow/pull/51043


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add timezone support for date calculation in TimeSensor [airflow]

Posted by "SML0127 (via GitHub)" <gi...@apache.org>.
SML0127 commented on PR #51043:
URL: https://github.com/apache/airflow/pull/51043#issuecomment-2907746208

   @uranusjr Thank you for the kind and detiled comments. I've addressed the points you mentioned.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add timezone support for date calculation in TimeSensor [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #51043:
URL: https://github.com/apache/airflow/pull/51043#discussion_r2106255620


##########
providers/standard/src/airflow/providers/standard/sensors/time.py:
##########
@@ -88,8 +88,12 @@ def __init__(
 
         # Create a "date-aware" timestamp that will be used as the "target_datetime". This is a requirement
         # of the DateTimeTrigger
+
+        # Get date considering dag.timezone
         aware_time = timezone.coerce_datetime(
-            datetime.datetime.combine(datetime.datetime.today(), target_time, self.dag.timezone)
+            datetime.datetime.combine(
+                datetime.datetime.now(self.dag.timezone), target_time, self.dag.timezone
+            )

Review Comment:
   ```suggestion
               datetime.datetime.now(self.dag.timezone)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org