diff --git a/hydra/core/utils.py b/hydra/core/utils.py index 4c58938b963..f9d572a6e7f 100644 --- a/hydra/core/utils.py +++ b/hydra/core/utils.py @@ -7,9 +7,9 @@ import warnings from contextlib import contextmanager from dataclasses import dataclass +from datetime import datetime from os.path import basename, dirname, splitext from pathlib import Path -from time import localtime, strftime from typing import Any, Dict, Optional, Sequence, Tuple, Union, cast from omegaconf import DictConfig, OmegaConf, open_dict, read_write @@ -151,7 +151,7 @@ def register(name: str, f: Any) -> None: pass # please add documentation when you add a new resolver - register("now", lambda pattern: strftime(pattern, localtime())) + register("now", lambda pattern: datetime.now().strftime(pattern)) register( "hydra", lambda path: OmegaConf.select(cast(DictConfig, HydraConfig.get()), path), diff --git a/news/1287.bugfix b/news/1287.bugfix new file mode 100644 index 00000000000..8c4a2d9b2f3 --- /dev/null +++ b/news/1287.bugfix @@ -0,0 +1 @@ +Add support for %f directive (microseconds) to the ${now:} resolver diff --git a/tests/test_hydra.py b/tests/test_hydra.py index 597cd3bd90b..691805e15a1 100644 --- a/tests/test_hydra.py +++ b/tests/test_hydra.py @@ -730,6 +730,24 @@ def test_local_run_workdir( ) +@pytest.mark.parametrize( + "task_config", + [ + ({"hydra": {"run": {"dir": "${now:%Y%m%d_%H%M%S_%f}"}}}), + ], +) +def test_run_dir_microseconds(tmpdir: Path, task_config: DictConfig) -> None: + cfg = OmegaConf.create(task_config) + assert isinstance(cfg, DictConfig) + integration_test( + tmpdir=tmpdir, + task_config=cfg, + overrides=[], + prints="str('%f' not in os.getcwd())", + expected_outputs="True", + ) + + def test_hydra_env_set_with_config(tmpdir: Path) -> None: cfg = OmegaConf.create({"hydra": {"job": {"env_set": {"foo": "bar"}}}}) integration_test( diff --git a/website/docs/configure_hydra/Intro.md b/website/docs/configure_hydra/Intro.md index 57c2eb68bdc..cefca7fb0fb 100644 --- a/website/docs/configure_hydra/Intro.md +++ b/website/docs/configure_hydra/Intro.md @@ -92,7 +92,7 @@ Hydra supports several [OmegaConf resolvers](https://github.com/facebookresearch **hydra**: Interpolates into the `hydra` config node. e.g. Use `${hydra:job.name}` to get the Hydra job name. **now**: Creates a string representing the current time using -[strftime](https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior). +[strftime](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior). e.g. for formatting the time you can use something like`${now:%H-%M-%S}`. **python_version**: Return a string representing the runtime python version by calling `sys.version_info`.