1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-11-16 10:38:19 +01:00

Add gitea_api.common.dt_sanitize() for sanitizing datetime strings

This commit is contained in:
2025-07-23 16:30:43 +02:00
parent 62c3eab524
commit 165cf14435
2 changed files with 14 additions and 0 deletions

View File

@@ -95,3 +95,16 @@ def edit_message(template: Optional[str] = None) -> str:
f.seek(0)
return f.read()
def dt_sanitize(date_time: str, tz_name: Optional[str] = "UTC"):
"""
Sanitize ``date_time`` string to "YYYY-MM-DD HH:MM" format.
Also convert it to the given time zone.
If ``tz_name`` is None, the time zone is the local time.
"""
import dateutil
dt = dateutil.parser.parse(timestr=date_time)
tz = dateutil.tz.gettz(tz_name)
return dt.astimezone(tz).strftime("%Y-%m-%d %H:%M")