Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow timespan to be specified with common time units #8626

Merged
merged 9 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 9 additions & 14 deletions src/borg/helpers/parseformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,20 @@ def interval(s):
seconds_in_a_month = 31 * seconds_in_a_day
seconds_in_a_year = 365 * seconds_in_a_day
multiplier = dict(
S = 1,
M = seconds_in_a_minute,
H = seconds_in_an_hour,
d = seconds_in_a_day,
w = seconds_in_a_week,
m = seconds_in_a_month,
y = seconds_in_a_year,
y=seconds_in_a_year,
m=seconds_in_a_month,
w=seconds_in_a_week,
d=seconds_in_a_day,
H=seconds_in_an_hour,
M=seconds_in_a_minute,
S=1,
)

if s.endswith(tuple(multiplier.keys())):
number = s[:-1]
suffix = s[-1]
else:
raise argparse.ArgumentTypeError(
f'Unexpected time unit "{s[-1]}": choose from {", ".join(multiplier)}'
)
raise argparse.ArgumentTypeError(f'Unexpected time unit "{s[-1]}": choose from {", ".join(multiplier)}')

try:
seconds = int(number) * multiplier[suffix]
Expand Down Expand Up @@ -579,10 +577,7 @@ def with_timestamp(self, timestamp):
def location_validator(proto=None, other=False):
def validator(text):
try:
try:
loc = Location(text, other=other)
except Exception as e:
assert False
loc = Location(text, other=other)
except ValueError as err:
raise argparse.ArgumentTypeError(str(err)) from None
if proto is not None and loc.proto != proto:
Expand Down
23 changes: 13 additions & 10 deletions src/borg/testsuite/helpers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,15 +553,18 @@ def test_prune_split_no_archives():
assert kept_because == {}


@pytest.mark.parametrize("timeframe, num_hours", [
("5S", 5),
("2M", 120),
("1H", 3600),
("1d", 86_400),
("1w", 604_800),
("1m", 2_678_400),
("1y", 31_536_000)
])
@pytest.mark.parametrize(
"timeframe, num_hours",
ThomasWaldmann marked this conversation as resolved.
Show resolved Hide resolved
[
("5S", 5),
("2M", 2 * 60),
("1H", 60 * 60),
("1d", 24 * 60 * 60),
("1w", 7 * 24 * 68 * 60),
("1m", 31 * 24 * 68 * 60),
("1y", 365 * 24 * 68 * 60),
ThomasWaldmann marked this conversation as resolved.
Show resolved Hide resolved
],
)
def test_interval(timeframe, num_secs):
assert interval(timeframe) == num_secs

Expand All @@ -571,7 +574,7 @@ def test_interval(timeframe, num_secs):
[
("H", ('Unexpected number "": expected positive integer',)),
("-1d", ('Unexpected number "-1": expected positive integer',)),
("food", ('Unexpectedinterval number "foo": expected positive integer',)),
("food", ('Unexpected number "foo": expected positive integer',)),
],
)
def test_interval_time_unit(invalid_interval, error_tuple):
Expand Down
Loading