Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 1 addition & 37 deletions airflow/utils/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
from __future__ import annotations

from datetime import datetime, timedelta
from typing import TYPE_CHECKING, Collection
from typing import TYPE_CHECKING

from croniter import croniter

from airflow.typing_compat import Literal
from airflow.utils import timezone

cron_presets: dict[str, str] = {
Expand Down Expand Up @@ -123,41 +122,6 @@ def round_time(
# and this function returns start_date.


TimeUnit = Literal["days", "hours", "minutes", "seconds"]


def infer_time_unit(time_seconds_arr: Collection[float]) -> TimeUnit:
"""
Determine the most appropriate time unit for given durations (in seconds).

e.g. 5400 seconds => 'minutes', 36000 seconds => 'hours'
"""
if not time_seconds_arr:
return "hours"
max_time_seconds = max(time_seconds_arr)
if max_time_seconds <= 60 * 2:
return "seconds"
elif max_time_seconds <= 60 * 60 * 2:
return "minutes"
elif max_time_seconds <= 24 * 60 * 60 * 2:
return "hours"
else:
return "days"


def scale_time_units(time_seconds_arr: Collection[float], unit: TimeUnit) -> Collection[float]:
"""Convert an array of time durations in seconds to the specified time unit."""
if unit == "minutes":
factor = 60
elif unit == "hours":
factor = 60 * 60
elif unit == "days":
factor = 24 * 60 * 60
else:
factor = 1
return [x / factor for x in time_seconds_arr]


def parse_execution_date(execution_date_str):
"""Parse execution date string to datetime object."""
return timezone.parse(execution_date_str)
Expand Down
53 changes: 0 additions & 53 deletions tests/utils/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
# under the License.
from __future__ import annotations

from datetime import timedelta

import pytest
from dateutil.relativedelta import relativedelta

from airflow.utils import dates, timezone

Expand All @@ -37,53 +34,3 @@ def test_parse_execution_date(self):
)
with pytest.raises(ValueError):
dates.parse_execution_date(bad_execution_date_str)

def test_round_time(self):
rt1 = dates.round_time(timezone.datetime(2015, 1, 1, 6), timedelta(days=1))
assert timezone.datetime(2015, 1, 1, 0, 0) == rt1

rt2 = dates.round_time(timezone.datetime(2015, 1, 2), relativedelta(months=1))
assert timezone.datetime(2015, 1, 1, 0, 0) == rt2

rt3 = dates.round_time(
timezone.datetime(2015, 9, 16, 0, 0), timedelta(1), timezone.datetime(2015, 9, 14, 0, 0)
)
assert timezone.datetime(2015, 9, 16, 0, 0) == rt3

rt4 = dates.round_time(
timezone.datetime(2015, 9, 15, 0, 0), timedelta(1), timezone.datetime(2015, 9, 14, 0, 0)
)
assert timezone.datetime(2015, 9, 15, 0, 0) == rt4

rt5 = dates.round_time(
timezone.datetime(2015, 9, 14, 0, 0), timedelta(1), timezone.datetime(2015, 9, 14, 0, 0)
)
assert timezone.datetime(2015, 9, 14, 0, 0) == rt5

rt6 = dates.round_time(
timezone.datetime(2015, 9, 13, 0, 0), timedelta(1), timezone.datetime(2015, 9, 14, 0, 0)
)
assert timezone.datetime(2015, 9, 14, 0, 0) == rt6

def test_infer_time_unit(self):
assert dates.infer_time_unit([130, 5400, 10]) == "minutes"

assert dates.infer_time_unit([110, 50, 10, 100]) == "seconds"

assert dates.infer_time_unit([100000, 50000, 10000, 20000]) == "hours"

assert dates.infer_time_unit([200000, 100000]) == "days"

def test_scale_time_units(self):
# floating point arrays
arr1 = dates.scale_time_units([130, 5400, 10], "minutes")
assert arr1 == pytest.approx([2.1667, 90.0, 0.1667], rel=1e-3)

arr2 = dates.scale_time_units([110, 50, 10, 100], "seconds")
assert arr2 == pytest.approx([110.0, 50.0, 10.0, 100.0])

arr3 = dates.scale_time_units([100000, 50000, 10000, 20000], "hours")
assert arr3 == pytest.approx([27.7778, 13.8889, 2.7778, 5.5556], rel=1e-3)

arr4 = dates.scale_time_units([200000, 100000], "days")
assert arr4 == pytest.approx([2.3147, 1.1574], rel=1e-3)