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

Duration filters #2682

Merged
merged 11 commits into from
Oct 29, 2018
Prev Previous commit
Next Next commit
Update duration filter tests and documentation.
Make requested changes from PR to CUG source, update CUG
source to account for filter consolidation, and add tests
as requested by the PR.
  • Loading branch information
Tim Whitcomb committed Oct 22, 2018
commit f2f35059f464c79166da5803fe6ce242706ef8d5
49 changes: 22 additions & 27 deletions doc/src/cylc-user-guide/cug.tex
Original file line number Diff line number Diff line change
Expand Up @@ -5208,48 +5208,43 @@ \subsubsection{Custom Jinja2 Filters}
\item \lstinline={{'1066/10/14 08:00:00' | strftime('%Y%m%dT%H', '%Y/%m/%d %H:%M:%S')}}= - 10661014T08
\end{myitemize}

\paragraph{duration\_to\_hours}

The ``duration\_to\_hours'' filter can be used to format ISO8601 duration
strings as a floating-point number of hours.

\lstset{language=suiterc}
\begin{lstlisting}
{% set CYCLE_INTERVAL = 'PT1D' %}
{{ CYCLE_INTERVAL | duration_to_hours }} # 24.0
{% set CYCLE_SUBINTERVAL = 'PT30M' %}
{{ CYCLE_SUBINTERVAL | duration_to_hours }} # 0.5
\end{lstlisting}

While the filtered value is a floating-point number, it is often required to
supply an integer to suite entities (e.g. environment variables) that require
it. This is accomplished by chaining filters:
\paragraph{duration\_as}

The ``duration\_as'' filter can be used to format ISO8601 duration
strings as a floating-point number of several different units. Units
for the conversion can be specified in a case-insensitive short or long
form:
\begin{myitemize}
\item \lstinline={{CYCLE_INTERVAL | duration_to_hours | int}}= - 24
\item \lstinline={{CYCLE_SUBINTERVAL | duration_to_hours | int}}= - 0
\item Seconds - ``s'' or ``seconds''
\item Minutes - ``m'' or ``minutes''
\item Hours - ``h`` or ``hours''
\item Days - ``d'' or ``days''
\item Weeks - ``w'' or ``weeks''
\end{myitemize}

\paragraph{duration\_to\_seconds}

The ``duration\_to\_seconds'' filter can be used to format ISO8601 duration
strings as a floating-point number of seconds.
Within the suite, this becomes

\lstset{language=suiterc}
\begin{lstlisting}
{% set CYCLE_INTERVAL = 'PT1D' %}
{{ CYCLE_INTERVAL | duration_to_seconds }} # 86400.0
{{ CYCLE_INTERVAL | duration_as('h') }} # 24.0
{% set CYCLE_SUBINTERVAL = 'PT30M' %}
{{ CYCLE_SUBINTERVAL | duration_as('hours') }} # 0.5
{% set CYCLE_INTERVAL = 'PT1D' %}
{{ CYCLE_INTERVAL | duration_as('s') }} # 86400.0
{% set CYCLE_SUBINTERVAL = 'PT30M' %}
{{ CYCLE_SUBINTERVAL | duration_to_seconds }} # 1800.0
{{ CYCLE_SUBINTERVAL | duration_as('seconds') }} # 1800.0
\end{lstlisting}

While the filtered value is a floating-point number, it is often required to
supply an integer to suite entities (e.g. environment variables) that require
supply an integer to suite entities (e.g.\ environment variables) that require
it. This is accomplished by chaining filters:

\begin{myitemize}
\item \lstinline={{CYCLE_INTERVAL | duration_to_seconds | int}}= - 86400
\item \lstinline={{CYCLE_SUBINTERVAL | duration_to_seconds | int}}= - 1800
\item \lstinline={{CYCLE_INTERVAL | duration_as('h') | int}}= - 24
\item \lstinline={{CYCLE_SUBINTERVAL | duration_as('h') | int}}= - 0
\item \lstinline={{CYCLE_INTERVAL | duration_as('s') | int}}= - 86400
\item \lstinline={{CYCLE_SUBINTERVAL | duration_as('s') | int}}= - 1800
\end{myitemize}


Expand Down
11 changes: 6 additions & 5 deletions tests/jinja2/09-custom-jinja2-filters/reference.log
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
2017-02-16T16:16:48Z INFO - Initial point: 01231212T1212Z
2017-02-16T16:16:48Z INFO - Final point: None
2017-02-16T16:16:48Z INFO - [strftime.01231212T1212Z] -triggered off []
2017-02-16T16:16:51Z INFO - [pad.01231212T1212Z] -triggered off ['strftime.01231212T1212Z']
2017-02-16T16:16:54Z INFO - [end.01231212T1212Z] -triggered off ['pad.01231212T1212Z']
2018-10-22T15:43:24Z INFO - Initial point: 01231212T1212Z
2018-10-22T15:43:24Z INFO - Final point: None
2018-10-22T15:43:24Z INFO - [strftime.01231212T1212Z] -triggered off []
2018-10-22T15:43:28Z INFO - [pad.01231212T1212Z] -triggered off ['strftime.01231212T1212Z']
2018-10-22T15:43:32Z INFO - [duration_as.01231212T1212Z] -triggered off ['pad.01231212T1212Z']
2018-10-22T15:43:36Z INFO - [end.01231212T1212Z] -triggered off ['duration_as.01231212T1212Z']
14 changes: 13 additions & 1 deletion tests/jinja2/09-custom-jinja2-filters/suite.rc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
initial cycle point = 01231212T1212
[[dependencies]]
[[[R1]]]
graph = strftime => pad => end
graph = strftime => pad => duration_as => end
[runtime]
[[strftime]]
script = test $TEST_1 == '00'; test $TEST_2 == '30'
Expand All @@ -16,5 +16,17 @@
TEST_2 = {{ '12-30-2000' | strftime('%d', '%m-%d-%Y') }}
[[pad]]
script = test {{ 42 | pad(3, 0) }} == '042'
[[duration_as]]
script = """
test $TEST_1 == '3600'
test $TEST_2 == '1'
test $TEST_3 == '1'
test $TEST_4 == '1'
"""
[[[environment]]]
TEST_1 = {{ 'PT1H' | duration_as('seconds') | int }}
TEST_2 = {{ 'P7D' | duration_as('w') | int }}
TEST_3 = {{ 'PT60M' | duration_as('hours') | int }}
TEST_4 = {{ 'PT60S' | duration_as('minutes') | int }}
[[end]]
script = true