Skip to content

Commit

Permalink
pre-commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinaHutter committed Sep 11, 2024
1 parent 75e3de9 commit 4af2dcb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 41 deletions.
73 changes: 49 additions & 24 deletions openeo_processes_dask/process_implementations/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import numpy as np


__all__ = [
"date_between",
"date_difference",
Expand All @@ -14,7 +13,10 @@ def datetime_from_str(date: str):
daytime = np.datetime64(date)
return daytime

def date_between(x: str, min: str, max: str, exclude_max: bool=False) -> Optional[bool]:

def date_between(
x: str, min: str, max: str, exclude_max: bool = False
) -> Optional[bool]:
x = datetime_from_str(x)
min = datetime_from_str(min)
max = datetime_from_str(max)
Expand All @@ -24,25 +26,39 @@ def date_between(x: str, min: str, max: str, exclude_max: bool=False) -> Optiona
return bool((x >= min) and (x <= max))


def date_difference(date1: str, date2: str, unit:Optional[str]="second") -> float:
def date_difference(date1: str, date2: str, unit: Optional[str] = "second") -> float:
date1 = datetime_from_str(date1)
date2 = datetime_from_str(date2)
units = {"millisecond": 1,
"second": 1000,
"minute": 1000*60,
"hour": 1000*60*60,
"day": 1000*60*60*24,
"week": 1000*60*60*24*7,
"month": "M",
"year": "Y"}
units = {
"millisecond": 1,
"second": 1000,
"minute": 1000 * 60,
"hour": 1000 * 60 * 60,
"day": 1000 * 60 * 60 * 24,
"week": 1000 * 60 * 60 * 24 * 7,
"month": "M",
"year": "Y",
}
if unit in units:
unit = units[unit]
if unit in ["M", "Y"]:
return float((date2.astype(f"datetime64[{unit}]") - date1.astype(f"datetime64[{unit}]")).astype(float))
return float(

Check warning on line 45 in openeo_processes_dask/process_implementations/dates.py

View check run for this annotation

Codecov / codecov/patch

openeo_processes_dask/process_implementations/dates.py#L45

Added line #L45 was not covered by tests
(
date2.astype(f"datetime64[{unit}]")
- date1.astype(f"datetime64[{unit}]")
).astype(float)
)
else:
# we do this, so the examples are fulfilled:
# we do this, so the examples are fulfilled:
# date_difference(date1 = "2020-01-01T00:00:00.0Z", date2 = "2020-01-01T00:00:15.5Z") -> 15.5
return float((date2.astype(f"datetime64[ms]") - date1.astype(f"datetime64[ms]")).astype(float)) / unit
return (
float(
(
date2.astype(f"datetime64[ms]") - date1.astype(f"datetime64[ms]")
).astype(float)
)
/ unit
)


def date_shift(date: str, value: int, unit: str) -> str:
Expand All @@ -53,20 +69,29 @@ def date_shift(date: str, value: int, unit: str) -> str:
date = date.split("+")[0]
else:
end = ""
units = {"millisecond": "ms",
"second": "s",
"minute": "m",
"hour": "h",
"day": "D",
"week": "W",
"month": "M",
"year": "Y"}
units = {
"millisecond": "ms",
"second": "s",
"minute": "m",
"hour": "h",
"day": "D",
"week": "W",
"month": "M",
"year": "Y",
}
if unit in units:
unit = units[unit]
if unit in ["M", "Y"]:
if len(date) > 7:
date_M = np.datetime64(date, "M")
day = int((np.datetime64(date, "D") - date_M.astype("datetime64[D]")).astype(int)) + 1
day = (
int(
(np.datetime64(date, "D") - date_M.astype("datetime64[D]")).astype(
int
)
)
+ 1
)
if " " in date:
time = "T" + date.split(" ")[-1]

Check warning on line 96 in openeo_processes_dask/process_implementations/dates.py

View check run for this annotation

Codecov / codecov/patch

openeo_processes_dask/process_implementations/dates.py#L96

Added line #L96 was not covered by tests
elif "T" in date:
Expand Down Expand Up @@ -98,4 +123,4 @@ def date_shift(date: str, value: int, unit: str) -> str:
result = str((date + np.timedelta64(value, unit)).astype(f"datetime64[{unit}]"))
else:
result = str((date + np.timedelta64(value, unit)).astype(date.dtype))
return result + end
return result + end
42 changes: 25 additions & 17 deletions tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,47 @@
date_shift,
)


def test_date_between():
assert not date_between(x = "2020-01-01", min = "2021-01-01", max = "2022-01-01")
assert not date_between(x="2020-01-01", min="2021-01-01", max="2022-01-01")


def test_date_difference():
assert date_difference(date1 = "2020-01-01T00:00:00.0Z", date2 = "2020-01-01T00:00:15.5Z") == 15.5
assert date_difference(date1 = "2020-01-01T00:00:00Z", date2 = "2020-01-01T01:00:00+01:00") == 0
assert date_difference(date1 = "2020-01-02", date2 = "2020-01-01") == -86400
assert date_difference(date1 = "2020-01-02", date2 = "2020-01-01", unit = "day") == -1
def test_date_difference():
assert (
date_difference(date1="2020-01-01T00:00:00.0Z", date2="2020-01-01T00:00:15.5Z")
== 15.5
)
assert (
date_difference(date1="2020-01-01T00:00:00Z", date2="2020-01-01T01:00:00+01:00")
== 0
)
assert date_difference(date1="2020-01-02", date2="2020-01-01") == -86400
assert date_difference(date1="2020-01-02", date2="2020-01-01", unit="day") == -1


def test_date_shift():

month_shift = date_shift(date = "2020-02-01T17:22:45Z", value = 6, unit = "month")
month_shift = date_shift(date="2020-02-01T17:22:45Z", value=6, unit="month")
assert month_shift == "2020-08-01T17:22:45Z"

day_shift = date_shift(date = "2021-03-31T00:00:00+02:00", value = -7, unit = "day")
day_shift = date_shift(date="2021-03-31T00:00:00+02:00", value=-7, unit="day")
assert day_shift == "2021-03-24T00:00:00+02:00"
year_shift = date_shift(date = "2020-02-29T17:22:45Z", value = 1, unit = "year")

year_shift = date_shift(date="2020-02-29T17:22:45Z", value=1, unit="year")
assert year_shift == "2021-02-28T17:22:45Z"

month_shift = date_shift(date = "2020-01-31", value = 1, unit = "month")
month_shift = date_shift(date="2020-01-31", value=1, unit="month")
assert month_shift == "2020-02-29"

second_shift = date_shift(date = "2016-12-31T23:59:59Z", value = 1, unit = "second")
second_shift = date_shift(date="2016-12-31T23:59:59Z", value=1, unit="second")
assert second_shift == "2017-01-01T00:00:00Z"

millisecond_shift = date_shift(date = "2018-12-31T17:22:45Z", value = 1150, unit = "millisecond")
millisecond_shift = date_shift(
date="2018-12-31T17:22:45Z", value=1150, unit="millisecond"
)
assert millisecond_shift == "2018-12-31T17:22:46.150Z"

hour_shift = date_shift(date = "2018-01-01", value = 25, unit = "hour")
hour_shift = date_shift(date="2018-01-01", value=25, unit="hour")
assert hour_shift == "2018-01-02"

hour_shift = date_shift(date = "2018-01-01", value = -1, unit = "hour")
assert hour_shift == "2017-12-31"
hour_shift = date_shift(date="2018-01-01", value=-1, unit="hour")
assert hour_shift == "2017-12-31"

0 comments on commit 4af2dcb

Please sign in to comment.