Klashe
09/04/2025, 4:23 AMSendNotification(
block_document_id='292dde02-6be4-440a-bf5c-e05531645752',
subject='# Prefect flow run notification',
body='**Flow Run:** {{ flow.name }}/{{ flow_run.name }} \n**State: **{% if flow_run.state.name == "Completed" %}🟢 {% elif flow_run.state.name == "Failed" %}🔴 {% elif flow_run.state.name == "Crashed" %}🟠{% elif \
flow_run.state.name == "TimedOut" %}đź”´ {% endif %} {{ flow_run.state.name }} \n**Timestamp:** {{ flow_run.state.timestamp.strftime("%Y-%b-%d %I:%M %p") }} America/Phoenix\n**Flow Run UI:** [Click here. Need to be on \
VPN.](<http://10.10.4.100:4200/runs/flow-run/{{> flow_run.id }})\n{% if flow_run.state.message %}\n**State Message:** {{ flow_run.state.message }}\n{% endif %}\n```\nFlow ID: {{ flow_run.flow_id }} \nFlow Run ID: {{ flow_run.id\
}}\n```\n\n\n'
)
],
actions_on_trigger=[],
actions_on_resolve=[]
).create()
Any way I can convert the flow_run.state>timestamp to a different timezone than UTC?Marvin
09/04/2025, 4:23 AMMarvin
09/04/2025, 4:27 AM.astimezone()
in your template to convert UTC to the Prefect server’s local time.
Example:
*Timestamp:* {{ flow_run.state.timestamp.astimezone().strftime("%Y-%b-%d %I:%M %p %Z") }}
That will render in whatever timezone the server/container is configured for and include the timezone abbreviation.
- Specific timezone (e.g., America/Phoenix):
- If you control the server, set the container/host TZ to that zone (e.g., TZ=America/Phoenix
). Then the .astimezone()
call above will render in Phoenix time.
- Or, if you’re self-hosting Prefect Server, you can register a custom Jinja filter that uses Python’s zoneinfo
to convert to a specific IANA timezone:
Python (run at server startup, e.g., in your image/entrypoint):
from zoneinfo import ZoneInfo
from prefect.server.utilities.user_templates import register_user_template_filters
def to_tz(dt, tz_name="America/Phoenix", fmt="%Y-%b-%d %I:%M %p %Z"):
if dt is None:
return ""
return dt.astimezone(ZoneInfo(tz_name)).strftime(fmt)
register_user_template_filters({"to_tz": to_tz})
Then in your notification template:
*Timestamp:* {{ flow_run.state.timestamp|to_tz("America/Phoenix") }}
- If you’re on Prefect Cloud and can’t register filters or change server TZ:
- Compute the local-time string in your flow code and include it in the state message, then reference it in the template. For example, set flow_run.state.message
to something like “Local time: 2025-09-04 10:15 AM America/Phoenix” and include {{ flow_run.state.message }}
in the notification.
References:
- User template utilities (register_user_template_filters)
- Prefect-provided Jinja filters (no timezone filter built-in)
If you can share whether you’re using Prefect Cloud or a self-hosted server, I can suggest the most practical path and help with the exact setup.Klashe
09/04/2025, 4:41 AM