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

Format datetime as ISO8601 #188

Merged
merged 8 commits into from
Oct 3, 2023
16 changes: 9 additions & 7 deletions datashuttle/utils/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,26 +216,28 @@ def update_names_with_datetime(names: List[str]) -> None:
Format using key-value pair for bids, i.e. date-20221223_time-
"""
date = str(datetime.datetime.now().date().strftime("%Y%m%d"))
format_date = f"date-{date}"
date_with_key = f"date-{date}"

time_ = datetime.datetime.now().time().strftime("%H%M%S")
format_time = f"time-{time_}"
time_with_key = f"time-{time_}"

datetime_with_key = f"datetime-{date}T{time_}"

for i, name in enumerate(names):
if tags("datetime") in name: # must come first
# datetime conditional must come first.
if tags("datetime") in name:
name = add_underscore_before_after_if_not_there(
name, tags("datetime")
)
datetime_ = f"{format_date}_{format_time}"
names[i] = name.replace(tags("datetime"), datetime_)
names[i] = name.replace(tags("datetime"), datetime_with_key)

elif tags("date") in name:
name = add_underscore_before_after_if_not_there(name, tags("date"))
names[i] = name.replace(tags("date"), format_date)
JoeZiminski marked this conversation as resolved.
Show resolved Hide resolved
names[i] = name.replace(tags("date"), date_with_key)

elif tags("time") in name:
name = add_underscore_before_after_if_not_there(name, tags("time"))
names[i] = name.replace(tags("time"), format_time)
names[i] = name.replace(tags("time"), time_with_key)


def add_underscore_before_after_if_not_there(string: str, key: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@

html_favicon = "_static/logo_light.png"

# Cutomize the theme
# Customize the theme
html_theme_options = {
"icon_links": [
{
Expand Down
2 changes: 1 addition & 1 deletion tests/tests_integration/test_command_line_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ def convert_kwargs_to_cli(self, kwargs, sep="-"):
Take a list of key-value pairs that make up
the arguments we want to pass to CLI, and
put them in correct format. This involves
pre-pending "--argument_name" for non-positional
prepending "--argument_name" for non-positional
arguments, and wrapping paths in quotes.
"""
positionals = ["local_path", "central_path", "connection_method"]
Expand Down
2 changes: 1 addition & 1 deletion tests/tests_integration/test_filesystem_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def test_transfer_with_keyword_parameters(
(base_local / "rawdata" / sub / "ses*").as_posix()
)

datetime_regexp = r"date-\d\d\d\d\d\d\d\d_time-\d\d\d\d\d\d"
datetime_regexp = "datetime-\d{8}T\d{6}"

assert re.match(
"ses-001_" + datetime_regexp, sessions_in_path[0]
Expand Down
4 changes: 2 additions & 2 deletions tests/tests_integration/test_make_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ def test_datetime_flag_in_session(self, project):
)

# Convert the minutes to regexp as could change during test runtime
regexp_time = r"\d\d\d\d\d\d"
datetime_regexp = f"{date}_time-{regexp_time}"
regexp_time = r"\d{6}"
datetime_regexp = f"datetime-{date}T{regexp_time}"

assert all([re.search(datetime_regexp, name) for name in ses_names])
assert all([tags("time") not in name for name in ses_names])
Expand Down
8 changes: 6 additions & 2 deletions tests/tests_unit/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def test_datetime_string_replacement(self, key, underscore_position):
keywords with the date / time / datetime. Also, it will
pre/append underscores to the tags if they are not
already there (e.g if user input "sub-001@DATE").
Note cannot use regex \d{8} format because we are in an
f-string.
"""
start = "sub-001"
end = "other-tag"
Expand All @@ -34,13 +36,15 @@ def test_datetime_string_replacement(self, key, underscore_position):
regex = re.compile(rf"{start}_time-\d\d\d\d\d\d_{end}")
elif key == tags("datetime"):
regex = re.compile(
rf"{start}_date-\d\d\d\d\d\d\d\d_time-\d\d\d\d\d\d_{end}"
rf"{start}_datetime-\d\d\d\d\d\d\d\dT\d\d\d\d\d\d_{end}"
)

name_list = [name]
formatting.update_names_with_datetime(name_list)

assert re.search(regex, name_list[0]) is not None
assert (
re.search(regex, name_list[0]) is not None
), "datetime formatting is incorrect."

@pytest.mark.parametrize(
"prefix_and_names",
Expand Down
Loading