-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Fix and document macros and interpolation variables #25305
Changes from 6 commits
87b4014
bb01af6
2d0c21d
06e85b3
496d2f3
097f15f
3182ed4
3dbfe1b
de7ff32
dd92255
6e5c4c0
07d0d3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,15 @@ def timestamp(dt: Union[numbers.Number, str]): | |
if isinstance(dt, numbers.Number): | ||
return int(dt) | ||
else: | ||
return int(parser.parse(dt).replace(tzinfo=datetime.timezone.utc).timestamp()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. existing implementation breaks if the input string contains a tz There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
return _str_to_datetime(dt).astimezone(datetime.timezone.utc).timestamp() | ||
|
||
|
||
def _str_to_datetime(s: str) -> datetime.datetime: | ||
parsed_date = parser.isoparse(s) | ||
if not parsed_date.tzinfo: | ||
# Assume UTC if the input does not contain a timezone | ||
parsed_date = parsed_date.replace(tzinfo=datetime.timezone.utc) | ||
return parsed_date.astimezone(datetime.timezone.utc) | ||
|
||
|
||
def max(*args): | ||
|
@@ -97,7 +105,7 @@ def day_delta(num_days: int, format: str = "%Y-%m-%dT%H:%M:%S.%f%z") -> str: | |
return (datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=num_days)).strftime(format) | ||
|
||
|
||
def duration(datestring: str): | ||
def duration(datestring: str) -> datetime.timedelta: | ||
""" | ||
Converts ISO8601 duration to datetime.timedelta | ||
|
||
|
@@ -107,7 +115,7 @@ def duration(datestring: str): | |
return parse_duration(datestring) | ||
|
||
|
||
def format_datetime(dt: Union[str, datetime.datetime], format: str): | ||
def format_datetime(dt: Union[str, datetime.datetime], format: str) -> str: | ||
""" | ||
Converts datetime to another format | ||
|
||
|
@@ -116,7 +124,7 @@ def format_datetime(dt: Union[str, datetime.datetime], format: str): | |
""" | ||
if isinstance(dt, datetime.datetime): | ||
return dt.strftime(format) | ||
return parser.parse(dt).strftime(format) | ||
return _str_to_datetime(dt).strftime(format) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
|
||
|
||
_macros_list = [now_local, now_utc, today_utc, timestamp, max, day_delta, duration, format_datetime] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like
{{ now_utc().strftime('%Y-%m-%d') }}
is pretty common, can we add that too?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea. done!