Debian Codegraph @

cylc/flow/jinja/filters/strftime.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 """Filter for formatting ISO8601 datetime strings."""
17
18 from typing import Optional
19
20 from metomi.isodatetime.parsers import TimePointParser
21
22
23 def strftime(
24 iso8601_datetime: str,
25 strftime_str: str,
26 strptime_str: Optional[str] = None,
27 ):
28 """Format an :term:`ISO8601 datetime` string using an strftime string.
29
30 .. code-block:: cylc
31
32 {{ '10661004T08+01' | strftime('%H') }} # 00
33
34 It is also possible to parse non-standard date-time strings by passing a
35 strptime string as the second argument.
36
37 Args:
38 iso8601_datetime:
39 Any valid ISO8601 datetime as a string.
40 strftime_str:
41 A valid strftime string to format the output datetime.
42 strptime_str:
43 A valid strptime string defining the format of the provided
44 iso8601_datetime.
45
46 Return:
47 The result of applying the strftime to the iso8601_datetime as parsed
48 by the strptime string if provided.
49
50 Raises:
51 ISO8601SyntaxError: In the event of an invalid datetime string.
52 StrftimeSyntaxError: In the event of an invalid strftime string.
53
54 Python Examples:
55 >>> # Basic usage.
56 >>> strftime('2000-01-01T00Z', '%H')
57 '00'
58 >>> strftime('2000', '%H')
59 '00'
60 >>> strftime('2000', '%Y/%m/%d %H:%M:%S')
61 '2000/01/01 00:00:00'
62 >>> strftime('10661014T08+01', '%z') # Timezone offset.
63 '+0100'
64 >>> strftime('10661014T08+01', '%j') # Day of the year
65 '287'
66
67 >>> # Strptime.
68 >>> strftime('12,30,2000', '%m', '%m,%d,%Y')
69 '12'
70 >>> strftime('1066/10/14 08:00:00', '%Y%m%dT%H', '%Y/%m/%d %H:%M:%S')
71 '10661014T08'
72
73 >>> # Exceptions.
74 >>> strftime('invalid', '%H') # doctest: +NORMALIZE_WHITESPACE
75 Traceback (most recent call last):
76 <class 'metomi.isodatetime.exceptions.ISO8601SyntaxError'>
77 metomi.isodatetime.exceptions.ISO8601SyntaxError: Invalid ISO 8601 \
78 date representation: invalid
79 >>> strftime('2000', '%invalid') # doctest: +NORMALIZE_WHITESPACE
80 Traceback (most recent call last):
81 metomi.isodatetime.exceptions.StrftimeSyntaxError: Invalid \
82 strftime/strptime representation: %i
83 >>> strftime('2000', '%Y', '%invalid')
84 ... # doctest: +NORMALIZE_WHITESPACE
85 Traceback (most recent call last):
86 metomi.isodatetime.exceptions.StrftimeSyntaxError: Invalid \
87 strftime/strptime representation: %i
88
89 Jinja2 Examples:
90 .. code-block:: cylc
91
92 {% set START_CYCLE = '10661004T08+01' %}
93
94 {{START_CYCLE | strftime('%Y')}} # 1066
95 {{START_CYCLE | strftime('%m')}} # 10
96 {{START_CYCLE | strftime('%d')}} # 14
97 {{START_CYCLE | strftime('%H:%M:%S %z')}} # 08:00:00 +01
98 {{'12,30,2000' | strftime('%m', '%m,%d,%Y')}} # 12
99
100 """
101 if not strptime_str:
102 return TimePointParser().parse(iso8601_datetime).strftime(strftime_str)
103 return TimePointParser().strptime(iso8601_datetime, strptime_str).strftime(
104 strftime_str)