Skip to content

Commit 5806a0c

Browse files
floriankrbpre-commit-ci[bot]anaprietonem
authored
fix: fix frequency_to_string outputing 108000s (#216)
## Description Fixes #215 ***As a contributor to the Anemoi framework, please ensure that your changes include unit tests, updates to any affected dependencies and documentation, and have been tested in a parallel setting (i.e., with multiple GPUs). As a reviewer, you are also responsible for verifying these aspects and requesting changes if they are not adequately addressed. For guidelines about those please refer to https://anemoi.readthedocs.io/en/latest/*** By opening this pull request, I affirm that all authors agree to the [Contributor License Agreement.](https://github.com/ecmwf/codex/blob/main/Legal/contributor_license_agreement.md) --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ana Prieto Nemesio <[email protected]>
1 parent 1e08996 commit 5806a0c

File tree

2 files changed

+11
-28
lines changed

2 files changed

+11
-28
lines changed

src/anemoi/utils/dates.py

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -260,39 +260,21 @@ def frequency_to_string(frequency: datetime.timedelta) -> str:
260260
A string representation of the frequency.
261261
"""
262262

263-
frequency = frequency_to_timedelta(frequency)
263+
total_seconds = int(frequency.total_seconds())
264264

265-
total_seconds = frequency.total_seconds()
266265
if total_seconds < 0:
267266
return f"-{frequency_to_string(-frequency)}"
268-
assert int(total_seconds) == total_seconds, total_seconds
269-
total_seconds = int(total_seconds)
270267

271-
seconds = total_seconds
268+
if total_seconds % (24 * 3600) == 0:
269+
return f"{total_seconds // (24 * 3600)}d"
272270

273-
days = seconds // (24 * 3600)
274-
seconds %= 24 * 3600
275-
hours = seconds // 3600
276-
seconds %= 3600
277-
minutes = seconds // 60
278-
seconds %= 60
271+
if total_seconds % 3600 == 0:
272+
return f"{total_seconds // 3600}h"
279273

280-
if days > 0 and hours == 0 and minutes == 0 and seconds == 0:
281-
return f"{days}d"
274+
if total_seconds % 60 == 0:
275+
return f"{total_seconds // 60}m"
282276

283-
if days == 0 and hours > 0 and minutes == 0 and seconds == 0:
284-
return f"{hours}h"
285-
286-
if days == 0 and hours == 0 and minutes > 0 and seconds == 0:
287-
return f"{minutes}m"
288-
289-
if days == 0 and hours == 0 and minutes == 0 and seconds > 0:
290-
return f"{seconds}s"
291-
292-
if days > 0:
293-
return f"{total_seconds}s"
294-
295-
return str(frequency)
277+
return f"{total_seconds}s"
296278

297279

298280
def frequency_to_seconds(frequency: int | str | datetime.timedelta) -> int:

tests/test_frequency.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
def test_frequency_to_string() -> None:
1818
"""Test the frequency_to_string function for converting timedelta to string."""
1919
assert frequency_to_string(datetime.timedelta(hours=1)) == "1h"
20-
assert frequency_to_string(datetime.timedelta(hours=1, minutes=30)) == "1:30:00"
20+
assert frequency_to_string(datetime.timedelta(hours=1, minutes=30)) == "90m"
2121
assert frequency_to_string(datetime.timedelta(days=10)) == "10d"
22+
assert frequency_to_string(datetime.timedelta(hours=30)) == "30h"
2223
assert frequency_to_string(datetime.timedelta(minutes=10)) == "10m"
23-
assert frequency_to_string(datetime.timedelta(minutes=90)) == "1:30:00"
24+
assert frequency_to_string(datetime.timedelta(minutes=90)) == "90m"
2425

2526

2627
def test_frequency_to_timedelta() -> None:

0 commit comments

Comments
 (0)