jinja2-time
jinja2-time copied to clipboard
How do I transform dates from a .json file?
What am I using?:
- Cookiecutter version: 1.7.3
- Template project url: hot-cookiecutters
- Python version: 3.10
- Operating System: macOS
Description:
I have a cookiecutter.json file with the following variables.
{
"cruise_number": "400",
"cruise_date_start": "YYYY-MM-DD",
"cruise_date_end": "YYYY-MM-DD",
"chief_scientist": "Chief Scientist Name",
"processor_first_name":"Your First Name" ,
"processor_last_name": "Your Last Name",
"created_on": "{% now 'local' %}” ,
"_extensions": ["jinja2_time.TimeExtension”]
}
I would like to parse/render the following in my 1.summary.md file.
# HOT-{{cookiecutter.cruise_number}} CTD Data Processing Report
Post-cruise processing of CTD data from the {{ cookiecutter.cruise_date_start.format('%Y-%m-%d') | strftime('%B %d, %Y’) }} to {{ cookiecutter.cruise_date_end.format('%Y-%m-%d') | strftime('%B %d, %Y') }}
The expected result from the following .json file would be:
{
"cruise_number": "400",
"cruise_date_start": “2022-10-30",
"cruise_date_end": “2022-11-04",
"chief_scientist": “Testing",
"processor_first_name”:”Fernando" ,
"processor_last_name": “Carvalho Pacheco",
"created_on": "{% now 'local' %}"
}
# HOT-400
Post-cruise processing of CTD data from the October 30th, 2022 to November 4th, 2022.
What I've run:
{{ cookiecutter.cruise_date_start.format('%Y-%m-%d') | strftime('%B %d, %Y’) }}
{{ cookiecutter.cruise_date_endformat('%Y-%m-%d') | strftime('%B %d, %Y’) }}
% Also:
{{ cookiecutter.cruise_date_start | iso8601_to_time | datetimeformat('%a, %B %d') }}
{{ cookiecutter.cruise_date_end | iso8601_to_time | datetimeformat('%a, %B %d') }}
{{ cookiecutter.cruise_date_start.strftime('%B %d, %Y') }}
% Also tried, without success, different combinations on cookiecutter.json:
"cruise_date_start": "YYYY-MM-DD",
"new_cruise_date_start": "{% {{cookiecutter.cruise_date_start}} 'utc', '%B %d, %Y' %}”,
"new_cruise_date_start": "{% {{cookiecutter.cruise_date_start}} , '%B %d, %Y' %}”,
"new_cruise_date_start": "{{cookiecutter.cruise_date_start.format('%B %d, %Y')}}”,
but I keep getting the following…
jinja2.exceptions.TemplateAssertionError: No filter named 'strftime'.
% or
jinja2.exceptions.TemplateAssertionError: No filter named ‘datetimeformat’.
%or
jinja2.exceptions.TemplateAssertionError: No filter named `iso8601_to_time`.
It feels that I should create a pre_gen_project.py hook with something like:
import sys
from datetime import datetime
def convert_date(cruise_date):
return datetime.fromisoformat(cruise_date).strftime('%B %d, %Y')
if __name__ == '__main__':
new_cruise_date_start = convert_date('{{cookiecutter.cruise_date_start}}')
The above will generate what I want (eg. new_cruise_date_start returns —> October 30, 2022) but now I need to find a way to pass that in the 1.summary.md file… because {{ cookiecutter.new_cruise_date_start }} will generate the following error Error message: ‘new_cruise_date_start' is undefined. Perhaps would need to write that ‘new_cruise_date_start’ in the cookiecutter.json file? But I don’t know. This doesn’t look right mainly because I feel that the "_extensions": ["jinja2_time.TimeExtension”] should solve this problem.
Any help or ideas on how to solve this would be much appreciated. Thanks!