Skip to content

feat: support datetime formatting in output template - #554

Open
wasim-builds wants to merge 6 commits into
google:mainfrom
wasim-builds:fix/issue-535-datetime-formatting
Open

feat: support datetime formatting in output template#554
wasim-builds wants to merge 6 commits into
google:mainfrom
wasim-builds:fix/issue-535-datetime-formatting

Conversation

@wasim-builds

@wasim-builds wasim-builds commented Jul 19, 2026

Copy link
Copy Markdown

Closes #535

Summary

Adds datetime formatting support to the output template in file_name_utils.py. Users can now use arbitrary f-string expressions with datetime objects in file name patterns.

Example

Before:

pattern = "weather_{date}"
# output: weather_2026-07-31

After:

pattern = "weather_{date:%Y%m%d_%H%M}"
# output: weather_20260731_0000

Changes

  • weather_sp/splitter_pipeline/file_name_utils.py: support datetime formatting in f-string evaluation
  • weather_sp/splitter_pipeline/file_name_utils_test.py: add tests covering datetime format strings, fallback behavior, and edge cases

Verification

Tests cover:

  • Standard datetime format codes (%Y, %m, %d, %H, %M, %S)
  • Fallback to string representation when format is invalid
  • Preservation of existing non-dynamic template behavior

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the corresponding unit test in file_name_utils_test.py to validate the new datetime formatting feature.

@@ -56,7 +56,21 @@ def split_dims(self) -> t.List[str]:

def formatted_output_path(self, splits: t.Dict[str, str]) -> str:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test that the output-template is handled correctly regardless of whether it is used with single quotes, double quotes, or no quotes at all!

def formatted_output_path(self, splits: t.Dict[str, str]) -> str:
"""Construct output file name with formatting applied"""
return self.unformatted_output_path().format(*self.template_folders, **splits)
import datetime

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kindly move the import datetime statement to the very beginning of the file.


try:
# First, substitute the positional args (if any) to avoid invalid f-string syntax like {0}
formatted_positionals = template.format(*self.template_folders, **{k: '{'+k+'}' for k in splits.keys()})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

template.format(...) doesn't support nested braces or function calls like {datetime.strptime(...), so it will raise a ValueError. When this happens, it falls into the except block and tries to call template.format() again, which will crash the pipeline. please fix this!

@shail-parekh shail-parekh Jul 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check and update the other functions in the pipeline that will not work as expected when they encounter these type of output-template!

@wasim-builds

Copy link
Copy Markdown
Author

I've pushed the fixes! The import datetime has been moved to the top, the formatting template crash with nested function calls is resolved, and I've added unit tests for both datetime logic and quote handling. Thanks for the review!

@wasim-builds

Copy link
Copy Markdown
Author

I've gone ahead and fixed the pipeline extraction logic! I updated split_dims to safely parse the expressions using Python's ast module, which successfully ignores globals like datetime and properly extracts the correct dimension variables without breaking downstream pipeline splitters. The changes are pushed for review!

@wasim-builds

Copy link
Copy Markdown
Author

I've just pushed an additional commit to resolve the linter ruff failures (trailing whitespaces and line length limits). The CI should turn green now!

@mahrsee1997
mahrsee1997 requested a review from shail-parekh July 30, 2026 11:16
- Move imports (ast, datetime, re) to module level in file_name_utils.py
- Make formatted_output_path fallback safe: catch KeyError/ValueError/IndexError
  from str.format() and fall back to manual key substitution instead of crashing
- Handle datetime templates in should_skip: return False early when template
  contains datetime expressions (wildcard glob matching not reliable)
- Handle datetime templates in GribCopySplitter: catch format() errors and
  fall back to manual positional arg substitution
- Add test for wildcard fallback with datetime templates
@wasim-builds

Copy link
Copy Markdown
Author

Addressed all review feedback:

  1. ✅ Tests added for datetime formatting (test_split_dims_with_datetime, test_formatted_output_path_datetime, test_formatted_output_path_quotes)
  2. ✅ Tests cover single/double/no quotes
  3. ✅ Moved import datetime to module level (line 17)
  4. ✅ Fixed formatted_output_path fallback: str.format() errors are now caught and fall back to manual key substitution instead of crashing the pipeline
  5. ✅ Fixed should_skip to return early when template contains datetime expressions (wildcard glob matching not reliable for datetime templates)
  6. ✅ Fixed GribCopySplitter.split_data to handle datetime templates in format() fallback with manual positional arg substitution
  7. ✅ Added test for wildcard fallback with datetime templates

All changes are scoped to file_name_utils.py and file_splitters.py only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[weather-sp] : better formatting of date & time in output template

2 participants